C# Find visible button by tag name and perform click (Compact Framework)

854 views Asked by At

I am developing an application for a WindowsCE device. Within my application I have five Panels, in which only one of them is visible at time.

Each panel has many different components, however, one of those many components is a button which has an unique Tag property set to btnOK.

Keeping in mind that each Panel has one of those unique buttons, and each of those buttons has a different function:

How can I get my "Enter" KeyPress event to find the Visible OK button and perform a click?

1

There are 1 answers

0
SteveFerg On

try creating a function:

private Control FindControl(Control parent, string ctlName)
{
    foreach(Control ctl in parent.Controls)
    {
        if(ctl.Name.Equals(ctlName))
        {
            return ctl;
        }

        FindControl(ctl, ctlName);                     
    }
    return null;
}

use the function like this:

Control ctl = FindControl(this, "btnOK");
if (ctl != null)
{
    btnOK_Click(this, new ButtonEventArgs());
}

this assumes your button click event looks like this...

button1_Click(object sender, ButtonEventArgs e)
{
}