Solved

C# -- SuspendLayout() FAILS ?

Posted on 2009-07-15
3
452 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
  • 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 85

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 85

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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.net VBA word safemode 1 24
getting id from database 5 24
Visual Studio editor? 1 18
ASP.NET Web API or ASP.NET Core MVC? 3 31
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now