Windows Develop Bookmark and Share   
 index > Windows Forms General > Super Simple Custom Control Alters Properties On Build When In TabControl
 

Super Simple Custom Control Alters Properties On Build When In TabControl

I'm using C# in VS2008 on a machine running Vista

How to replicate:

1. Create new Windows Forms Applicationproject named "WindowsFormsApplication1"
2. Add new User Control to WindowsFormsApplication1 named "UserControl1"
3. Add a Button to UserControl1 from the Visual Studio Toolbox named "button1"
4. In code view, add the following code to the "public partial class UserControl1 : UserControl":

public bool ShowButton
{
    get { return this.button1.Visible; }
    set 
    {
        if (this.button1.Visible != value)
        {
        this.button1.Visible = value;
        }
    }
}

5. Build the project and close the UserControl1
6. Add a TabControl to Form1 (by default Form1 was created when we created the project)
7. Add a UserControl1(named "userControl11")to TapPage1 of TabControl1 from the Visual Studio Toolbox
8. Add a UserControl1(named "userControl12")to TabPage2 of TabControl1from the Visual Studio Toolbox
9. Build the project and notice thatthe "ShowButton" property of userControl12has changed from True to False on its own when we built the project
10. Change the value of the "ShowButton" property of userControl12 to "True" (the button should re-appear)
11. Build the project and notice thatthe "ShowButton" property of userControl11 haschanged from True to False on its own when we built the project

Q: Why are these controls changing each other's properties when we build the project?

Here is a link to the project I created (to save you time): http://www.matthodan.com/WindowsFormsApplication1.zip
Here is a video of the problem: http://www.matthodan.com/ExampleMovie.swf.html
MattElpmis  Thursday, May 14, 2009 2:47 AM
The Visible property is special. The property getter for it doesn't return the last value you assigned to it, it returns True only if control is actually visible. Which it won't be if the UC isn't visible. Which it won't be while the designer is generating the code. Fix it like this:

private bool mVisible;

public bool ShowButton {
get { return mVisible; }
set { mVisible = this.button1.Visible = value; }
}

Be sure to initialize mVisible correctly in the constructor, it should match the value of button1.Visible.

Hans Passant.
  • Marked As Answer byMattElpmis Friday, May 15, 2009 3:31 PM
  •  
nobugz  Friday, May 15, 2009 11:06 AM

I'm still at a loss on this one. I tried removing the if statement, but no change. Here's the new code:

public bool ShowButton
{
    get { return this.button1.Visible; }
    set { this.button1.Visible = value; }
}

I feel like this should be a really simple fix. Just can't put my finger on what is wrong. Any replies would be extremely appreciated!

Thanks,
Matt
MattElpmis  Friday, May 15, 2009 2:31 AM
It is puzzling, you can try posting tohttps://connect.microsoft.com/VisualStudio
ChronusDOTNet  Friday, May 15, 2009 4:10 AM
The Visible property is special. The property getter for it doesn't return the last value you assigned to it, it returns True only if control is actually visible. Which it won't be if the UC isn't visible. Which it won't be while the designer is generating the code. Fix it like this:

private bool mVisible;

public bool ShowButton {
get { return mVisible; }
set { mVisible = this.button1.Visible = value; }
}

Be sure to initialize mVisible correctly in the constructor, it should match the value of button1.Visible.

Hans Passant.
  • Marked As Answer byMattElpmis Friday, May 15, 2009 3:31 PM
  •  
nobugz  Friday, May 15, 2009 11:06 AM

You can use google to search for other answers

Custom Search

More Threads

• BalloonTip Problems of C#
• microsoft visual Studio help needed please
• Programs Installed
• Windows Form move from one project to another
• Trouble adding SQL Server MDF to my WinForms project
• color
• Do I need to have both .Net Framework 1.1 and .NET Framework 2.0 installed on my computer?
• Difference between DotNet2.0 and DotNet3.0
• Child / Parent ACCESS !!
• user controls and windows forms