Solved

C# -- SuspendLayout() FAILS ?

Posted on 2009-07-15
3
467 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 auto inserted code 12 88
c#, case, if 4 37
PowerShell:  Use of subproperties in a Select statement 7 68
Need Help With GDI+ in VB.Net 8 40
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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