Conditionally Show/Hide Windows Form Elements

11,866

Solution 1

First thing I would do is put a breakpoint in your code:

private void Payment_Load(object sender, EventArgs e)
{
   if (rdoMultChoice.Checked)  // <-- Put breakpoint here.

You want to know if Payment_Load is being executed.

Next, after you're certain it is being executed, check your code to see if there is anything that might be changing the label's visibility. it could be another piece of code which is changing the .Visible setting.

Solution 2

You probably need to add a listener to your control. Like this:

rdoMultChoise.CheckedChanged += Payment_Load;

Solution 3

Initially make all your controls Visible property to false and again make them visible on Form_Load().

Because whenever a form loads all controls get initialized and their state changes.

Share:
11,866
Daniel Melchior
Author by

Daniel Melchior

Updated on June 04, 2022

Comments

  • Daniel Melchior
    Daniel Melchior almost 2 years

    I am trying to make a simple Windows Form application that will show different options based on the values of preceding elements - for example I have four radio buttons at the top of the form, each one will show and hide elements various other elements within the form - essentially making several forms in one.

    I have this set up in a large conditional statement (this is only a small portion, but it is all similar):

    private void Payment_Load(object sender, EventArgs e)
      {
                if (rdoMultChoice.Checked)
                {
    
                    lblGroupBox1_MC.Visible = true;
                    lblGroupBox1_FITB.Visible = false;
                    lblGroupBox1_TF.Visible = false; 
                 // etc...
                }
                else if (rdoFillInBlank.Checked)
                {
                    lblGroupBox1_MC.Visible = false;
                    lblGroupBox1_FITB.Visible = true;
                    lblGroupBox1_TF.Visible = false;
                 // etc...
                }
    

    The problem is, when I run the application the form completely ignores these statements and appears to just make all of my elements visible.

    http://msdn.microsoft.com/en-us/library/754w18dd.aspx

    This link is sort of similar to my needs. I tried to adapt it to my situation but it didn't appear to work (can't guarantee that I did it correctly...).

    Seems like this should be a really simple thing to do but I am new to C# and only started using it this week - an assignment for a CS class. Just to rant, we are expected to develop "expertise" in 13 languages in 15 weeks! With very few resources provided by the university, so far the only expertise I have developed is in searching documentation and stack overflow! :)

  • Cody Gray
    Cody Gray about 12 years
    Yes, this is the most likely explanation. The Payment.Load event is not actually hooked up to your handler. This doesn't happen automatically in C# like it does in VB.NET.
  • Daniel Melchior
    Daniel Melchior about 12 years
    It appears that this is most likely the problem, could you explain the best way to link the event and listener? Should I do this within payment load or in my main()?
  • Cody Gray
    Cody Gray about 12 years
    @user: Do it in the constructor for the Payment form. That's the method with the signature Payment(). Right now, it probably contains a single line of code: the call to the InitializeComponent method.
  • Daniel Melchior
    Daniel Melchior about 12 years
    Thanks, I appreciate the assistance.