?
Solved

C# -- SuspendLayout() FAILS ?

Posted on 2009-07-15
3
Medium Priority
?
473 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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…
Suggested Courses

770 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