Hi there folks,

kinda new too C#

and want too show or hide a button using a checkbox
and after trying i just gave up haha

so here is the quastion! how do you do that
because i am really lost here..

would show you the code.
but i kinda delete it because i was mad -.-

Edit:

i dindt mention i want the checkbox on form 2 and the button on form 1.

Recommended Answers

All 15 Replies

not mad anymore :D

so i rewrote the code up too the point i got stuck..

CheckBox checkbox1; 

        private void button1_Click(object sender, EventArgs e)
        {
           if (checkbox1.CheckState == CheckState.Checked) {
	    
	    button1.Enabled = true;
           }
	else {
	   
	    button1.Enabled = false;
	}

well i think it has something too do that it doesnt really checksate because when i start the application i dont get error's or warnings.

so i hope someone can help me.

Hi Fiascor,

Firstly the first form needs to locate the second form. This can be done using a Form collection.

Application.OpenForms

We get only the Form2 instances using

Application.OpenForms.OfType<Form2>()

If there is a chance there will be more than one then use something like

Application.OpenForms.OfType<Form2>().First()

The button and its properties is not publically visible from Form2 by default so we change a setting on it:
Modifiers: Public.

So, putting it all together, on Form1 we need to use something like

try
            {
                Application.OpenForms.OfType<Form2>().First().cmdButton.Visible = checkBox1.Checked;
            }
            catch { }

Nicholas

You should use button1.Hide(); or button1.Show(); instead of setting button1.Enable to true or false.
Use true and false for the CheckBox Enabled property.

Member Avatar for nssltd

Yes ddanbe is right if you use button1.Enable it will only seem to grey the button out not hide it.

i think this will work,

int a=checkboxname.checkstate;
if(a==1)//that is checked then
{
  button.show();
} 
else
{
 button.hide();
}

Yes ddanbe is right if you use button1.Enable it will only seem to grey the button out not hide it.

this will work
button.hide();

In the first post it says:

"i dindt mention i want the checkbox on form 2 and the button on form 1. "

....

hi guys and maybe a girl(don't know),

I really appreciate the reply's you gave..

to N4JRY a.k.a Nicholas..

I saw the code i think i get it don't get it too work yet but that may be the lack
of experience and your correct about the fact i want the checkbox on form 2 and the button on form 1 its like a config menu form 2 :D

school want me to make something like this but i only have about 3 hours of experiencing so the hick ups are a lot :P

to isha india ill try that now and i hope ill get it too work..

to ddanbe i have tried that already but it dint seem to work i think form 1 doesnt checkstate or doesnt save it for some reason..

so thanks guys and maybe a girl haha..

ill do my best because i need the grade :D

Member Avatar for nssltd

this will work
button.hide();

Yes DDanbe has said that too.

I saw the code i think i get it don't get it too work yet but that may be the lack
of experience and your correct about the fact i want the checkbox on form 2 and the button on form 1 its like a config menu form 2

Hi,

The problem may be with the naming of the forms. I have intentionally named my forms Form1 and Form2 (default names). Form1 determines the state of the button on Form2. Thus the code to locate the form with the button contains the form name Form2. This is not the same on your example so a quick change and you should be away. (Remember to make the button public or it wont be accessible..)

As you are a student there is another approach you can take:
First make something visible across the application that sets the visibility:

public void VisibleToggle(bool controlVisiblity)
        {
            cmdButton.Visible = controlVisiblity;
        }

Loop through the forms collection and find all instances of the required form - important if there is more than one. Once found then set the visibility.

foreach (var fm in Application.OpenForms)
            {
                if (fm.GetType() == typeof(Form2))
                {
                    Form2 fm2 = (Form2)fm;
                    fm2.VisibleToggle(checkBox1.Checked);
                }
            }

N

tnx n4rjy,

for your help only got one hill too climb the problem is that it says checkbox1 doesnt exist in current context dont know wat to about that yet.

oh i used you first solution you mentiont i jsut dindt knew where to put the diffrent code's found out that it has to go in program.cs haha

ill try and find it out myself but if you know how, all advice is welcome

Fiascor

Consider the context of each part.
The check box has an event that is local to a form - its checked value is also accessable on that form.
The command button can be made public so accessable anywhere - as is the forms collection.
I think you should have enough to peice it together.
Kind regards,
N

back to topic question:

//constructor:
        public Form1()
        {
            this.button1.Visible = false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
                this.button1.Visible = true;
            else
                this.button1.Visible = false;
        }
private void ChkSave_CheckedChanged(object sender, EventArgs e)
    {
        if (chkSave.Checked==true)
        {
            btnSave.Visible = true;
        }
        else
        {
            btnSave.Visible=false;
        }
    }

Your answer is 9 years too late, adds nothing to the discussion, and is one of the worst pieces of code I have seen in some time. You obviously need to study boolean values some more becuase testing a boolean for true is such a novice mistake.
The whole of your tortuous lines 3 - 10 are just a horrible way to code

btnSave.Visible = chkSave.Checked;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.