Solved

C# -- SuspendLayout() FAILS ?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

11 Experts available now in Live!

Get 1:1 Help Now