Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi!

im trying to create a public method that will control the properties of a button inside a form. For example, when i call the method;

1. find all button inside the form.
2. find a button with Name = "btnSave".. (example)
3. finally, change the properties of that particular button.

i do have multiple forms, some of this forms contains a button with the following Names;
btnSave, btnSearch, btnUpdate, btnDelete.

my sample code cannot identify what type of control thats why i cant call some properties of Button. Please help.. newbie..

What I have tried:

public static void getButtonImage(Form f)
        {
            try
            {
                foreach (Control item in f.Controls)
                {

                    if (item.Name == "btnsave")
                    {
                        item.BackgroundImage = Image.FromFile(@".\images\saveLogo.png");
                        item.BackgroundImageLayout = ImageLayout.Zoom;
                        item.Text = "";

                    }
                    else if (item.Name == "btnupdate")
                    {
                        item.BackgroundImage = Image.FromFile(@".\images\updateLogo.png");
                        item.BackgroundImageLayout = ImageLayout.Zoom;
                        item.Text = "";

                    }
                    else if (item.Name == "btnsearch")
                    {
                        item.BackgroundImage = Image.FromFile(@".\images\searchLogo.png");
                        item.BackgroundImageLayout = ImageLayout.Zoom;
                        item.Text = "";

                    }
                    else if (item.Name == "btndelete")
                    {
                        item.BackgroundImage = Image.FromFile(@".\images\deleteLogo.png");
                        item.BackgroundImageLayout = ImageLayout.Zoom;
                        item.Text = "";

                    }
                }

                

            }
            catch (Exception)
            {

            }
        }
Posted
Updated 9-Apr-18 23:23pm
v2

Start like this:
C#
foreach (Control c in f.Controls)
   {
   Button b = c as Button;
   if (b != null)
      {
      ...
      }
   }
This eliminates all Controls that are not Buttons, and allows you to use all Button properties on the ones that are.

Do yourself a favour: don't "swallow" exceptions.
At the very least log them somewhere before you ignore them, so that if you need teh information you can get access to it - if only in debugging.
C#
...
catch (Exception ex)
   {
   Debug.WriteLine(ex.Message);
   }
Swallowing exceptions means you don't even know there is a problem until it becomes too late and makes it very, very hard to fix them as you have discarded teh info that would tell you what happened!
 
Share this answer
 
v2
Comments
Maciej Los 10-Apr-18 5:24am    
5ed!
akosisugar 10-Apr-18 5:58am    
hi sir! thank u for the idea.. problem solve!
OriginalGriff 10-Apr-18 6:00am    
You're welcome!
There's two ways of doing this. If you genuinely want to look through all controls you can use "as" to cast the control from the base "Control" type to its more specific type of "Button"

if (item.Name == "btnsave")
{
    Button b = item as Button;

    if (b != null)
    {
        b.BackgroundImage = Image.FromFile(@".\images\saveLogo.png");
        b.BackgroundImageLayout = ImageLayout.Zoom;
        b.Text = "";

        // you can now access Button specific functions and properties
        b.PerformClick();
    }

}


"as" will return null if the cast fails, ie if item isn't a Button. You can also use "is" if you just want to check it is a Button.

If you only want to loop through the buttons rather than all controls then use this

foreach (Button item in f.Controls.OfType<Button>())


Now "item" is already of type Button so no need to use "as"
 
Share this answer
 
v2
Comments
Maciej Los 10-Apr-18 5:24am    
5ed!
First of all...
Note that some of Windows controls might be a containers[^] for other controls. What does it mean to you? You have to create recursive function/method .

Second of all...
You can check what kind of control it is...
C#
if(item is Button)
{
    //this is a button!
}


See: is (C# Reference) | Microsoft Docs[^]

Good luck!
 
Share this answer
 
v2

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