Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:26:46+00:00 2026-05-14T03:26:46+00:00

Here is a test framework to show what I am doing: create a new

  • 0

Here is a test framework to show what I am doing:

  1. create a new project
  2. add a tabbed control
  3. on tab 1 put a button
  4. on tab 2 put a check box
  5. paste this code for its code

(use default names for controls)

public partial class Form1 : Form
{
    private List<bool> boolList = new List<bool>();
    BindingSource bs = new BindingSource();
    public Form1()
    {
        InitializeComponent();
        boolList.Add(false);
        bs.DataSource = boolList;
        checkBox1.DataBindings.Add("Checked", bs, "");
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);

    }
    bool updating = false;
    private void button1_Click(object sender, EventArgs e)
    {
        updating = true;
        boolList[0] = true;
        bs.ResetBindings(false);
        Application.DoEvents();
        updating = false;
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (!updating)
            MessageBox.Show("CheckChanged fired outside of updating");
    }
}

The issue is if you run the program and look at tab 2 then press the button on tab 1 the program works as expected, however if you press the button on tab 1 then look at tab 2 the event for the checkbox will not fire untill you look at tab 2.

The reason for this is the controll on tab 2 is not in the “created” state, so its binding to change the checkbox from unchecked to checked does not happen until after the control has been “Created”.

checkbox1.CreateControl() does not do anything because according to MSDN

CreateControl does not create a
control handle if the control’s
Visible property is false. You can
either call the CreateHandle method or
access the Handle property to create
the control’s handle regardless of the
control’s visibility, but in this
case, no window handles are created
for the control’s children.

I tried getting the value of Handle(there is no public CreateHandle() for CheckBox) but still the same result.

Any suggestions other than have the program quickly flash all of my tabs that have data-bound check boxes when it first loads?

EDIT– per Jaxidian’s suggestion I created a new class

public class newcheckbox : CheckBox
{
    public new void CreateHandle()
    {
        base.CreateHandle();
    }
}

I call CreateHandle() right after updating = true same results as before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:26:46+00:00Added an answer on May 14, 2026 at 3:26 am

    I think I have a solution. The problem is not that you cannot create a Handle. You can do that by simply accessing the Handle get accessor on the Control. The problem is that WinForms does not create the control because it is not visible. As it turns out, behind the scenes, a System.Windows.Forms.Control has two overloads for CreateControl. The first, which is public, takes no parameters and it calls the second which is internal which takes a single boolean parameter: ignoreVisible which as the name implies allows the calling code to create the control even if it is not visible. The CreateControl method with no arguments passes false to this internal method which means that if the control is not visible, it is not created. So, the trick is to use Reflection to call the internal method. First, I created two methods for creating controls:

    private static void CreateControls( Control control )
    {
        CreateControl( control );
        foreach ( Control subcontrol in control.Controls )
        {
            CreateControl( subcontrol );
        }
    }
    private static void CreateControl( Control control )
    {
        var method = control.GetType().GetMethod( "CreateControl", BindingFlags.Instance | BindingFlags.NonPublic );
        var parameters = method.GetParameters();
        Debug.Assert( parameters.Length == 1, "Looking only for the method with a single parameter" );
        Debug.Assert( parameters[0].ParameterType == typeof ( bool ), "Single parameter is not of type boolean" );
    
        method.Invoke( control, new object[] { true } );
    }
    

    Now, we add a call to CreateControls for the second tab:

    public Form1()
    {
        InitializeComponent();
        boolList.Add( false );
        bs.DataSource = boolList;
        checkBox1.DataBindings.Add( "Checked", bs, "" );
        this.button1.Click += this.button1_Click;
        this.checkBox1.CheckedChanged += this.checkBox1_CheckedChanged;
    
        CreateControls( this.tabPage2 );
    }
    

    In addition, I added some debugging messages so I could see if the event fired:

    private void button1_Click( object sender, EventArgs e )
    {
        Debug.WriteLine( "button1_Click" );
        updating = true;
        boolList[0] = true;
        bs.ResetBindings( false );
        Application.DoEvents();
        updating = false;
    }
    
    private void checkBox1_CheckedChanged( object sender, EventArgs e )
    {
        Debug.WriteLine( "checkBox1_CheckedChanged" );
        if ( !updating )
        {
            Debug.WriteLine( "!updating" );
            MessageBox.Show( "CheckChanged fired outside of updating" );
        }
    }
    

    Now, whether you navigate to the second tab or not, clicking on the button on the first tab will fire the checkbox1_Changed event procedure. Given the design you provided, if you click on the button, it will not show the MessageBox because updating will be true. However, the Debug.WriteLine will show that it fired in the Output window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a test description, testing the Create New Widget use-case. Confirm that you
I have a Silverlight testing project using the Silverlight Unit Test Framework . I
Just some days ago I started looking into a unit test framework called check,
I am trying to create a test framework and not sure what is best
I often hear around here from test driven development people that having a function
Here's my test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { classpath:repositoryContextTest.xml }) @Transactional @TransactionConfiguration(defaultRollback = true) public
Here is my test code: <script type=text/javascript> YUI({ modules: { 'jquery': { fullpath: 'script/lib/jquery.min.js'
here is the test page http://www.studioteknik.com/html/test-portfolio.html I got no error, but no hover-slide effect...
Here is my test page (dont mind the layout right now) https://www.bcidaho.com/test_kalyani/employer-plans-test.asp i found
My test site here is working fine I believe (haven't tested it in all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.