简体   繁体   中英

Conditionally Show/Hide Windows Form Elements

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! :)

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.

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

rdoMultChoise.CheckedChanged += Payment_Load;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM