Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# -- SuspendLayout() FAILS ?

Posted on 2009-07-15
3
Medium Priority
?
476 Views
Last Modified: 2013-12-17
I created a new WindowsAPP, added the below "SuspendLayout" so the "textBox1_TextChanged" EVENT does not run during the FormLoad "public Form1()", ran the app, and the "MessageBox" still displays during the form load.

How can I get the "MessageBox" to only display after formLoad ?
----------------------------------------------------------
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.SuspendLayout();
            textBox1.Text = "Test";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("text changed");
        }
    }
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24861478

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.TextChanged -= new EventHandler(textBox1_TextChanged);
            textBox1.Text = "Test";
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("text changed");
        }
    }

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24861547
SuspendLayout() suppresses events affecting the LAYOUT of the control (such as size, position, etc)...

You could toggle a boolean flag in the Shown() event of your form like this:


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "Test";
        }
 
        private Boolean Loading = true;
 
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Loading = false;
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.Loading) return;
 
            MessageBox.Show("text changed");
        }
 
    }

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24861587
Another option....

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!this.IsHandleCreated) return;

            MessageBox.Show("text changed");
        }
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question