Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having a problem creating a variable to check whether a button is still on a form or if it has been removed in order to open another form....please help.
here is the code snippet for the button:

C#
private void button6_Click(object sender, EventArgs e)
        {
            button6.Text = "4";
            if (t == "")
            {
                t = button6.Text;
            }
            else
            {
                if (w == "")
                {
                    w = button6.Text;
                    if (w == t)
                    {
                        this.Controls.Remove(button7);
                        this.Controls.Remove(button6);
                    }
                    else
                    {
                        foreach (Control ctrl in this.Controls)
                        {
                            if (ctrl is Button)
                            {
                                ctrl.ResetText();
                            }
                        }
                    }
                }
                else
                {
                    w = "";
                    t = "";
                    t = button6.Text;
                }
            }
        }
Posted
Updated 5-Sep-11 10:42am
Comments
Philippe Mori 5-Sep-11 20:06pm    
Humm... Removing button is probably not considered a good UI design... Why would remove buttons before opening another form. It is preferable to disable buttons that should not be used in most scenarios.
Sergey Alexandrovich Kryukov 5-Sep-11 21:16pm    
I think your advice is the only reasonable one so far... I would up-vote it.
--SA

if the button is a direct child of the form:

C#
bool isButtonOnForm = this.Controls.Contains(button6);


otherwise you will need a recursive method:

C#
public bool IsButtonFound(Control ctl, Button btn)
{
    if (!ctl.HasChildren){return false;}

    foreach (Control child in ctl.Controls)
    {
        if (child == btn){ return true;}
        if (IsButtonFound(child, btn)){ return true;}
    }

    return false;
}


this looks walks the control hierarchy until it finds the button (at which point all recursive calls return true all the way up to the root call) or until it runs out of controls
 
Share this answer
 
Comments
Wendelius 5-Sep-11 17:28pm    
The recursion is a very good addition, my 5
GParkings 6-Sep-11 4:10am    
to whoever voted 1: if you post a comment as to why you think badly of this solution i would be happy to amend it in order that any future readers of this question might be better served.
So do you want to know if a control exists on your form based on it's name? If that is the case I think one easy way is to use ContainsKey method. Something like:
C#
If (this.Controls.ContainsKey("button6") {
...
 
Share this answer
 
Comments
GParkings 5-Sep-11 17:23pm    
haha, you beat me to that one :) have 5
Wendelius 5-Sep-11 17:27pm    
Thanks :)
lethabo11 5-Sep-11 18:14pm    
Thanks Mika. Does the containsKey method only check the button's presence? How about when the button is not there?
lethabo11 5-Sep-11 17:47pm    
Thanks Mika. Does the containsKey method only check the button's presence? How about when the button is not there?
Wendelius 5-Sep-11 17:59pm    
Not sure what you mean with presence. If the button is removed from the forms controls collection, ContainsKey isn't finding it anymore.
Mika's answer seems perfectly fine. Here is an alternative though.
At the time of the removal of your button you could set a boolean field.
Later you would only need to check if it is true or false.
C#
private bool _btn6Removed;

private void Click(object sender, EventArgs e){
   this.Controls.Remove(button6);
   _btn6Removed = true;
}
//...
if (_btn6Removed){
//...
}
 
Share this answer
 
Comments
lethabo11 5-Sep-11 18:00pm    
Thank you
lethabo11 5-Sep-11 18:31pm    
your method is working fine but the problem is that i have 16 buttons that i have to remove from the form before i can set my condition which in this case will be opening another form...how do i go about it? i tried using the boolean field for each button but its not working.
Sander Rossel 5-Sep-11 18:37pm    
I guess the best approach is to simply delete all buttons, then set the boolean and check it.
this.Control.Remove(button1);
this.Control.Remove(button2);
this.Control.Remove(button3);
this.Control.Remove(button4);
...
_btnsRemoved = true;

if (_btnsRemoved){
//code
}

You could also use recursion to remove all Buttons in one Method, look at solution 2 for an example.
Solution 1 should also still work.
I think you'll get it working, good luck! :)
lethabo11 6-Sep-11 0:16am    
I can not remove the all buttons at the same as they are removed in pairs when they contain the same text. Two buttons have to be matched before they can be removed. Thanks a loy for your help, I will keep on trying...am a programming beginner.
lethabo11 6-Sep-11 0:10am    
how do i use your method for many buttons on the same form? i have 16 buttons that i have to remove.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900