Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Convert a string to an object reference

Tags:

c#

types

casting

Basically a button's tag property is the name of an existing combobox which I need to dynamically reference. It's a generic function to handle multiple buttons. Help

private void SQLButton(object sender, EventArgs e)
{
    magic(((Button)sender).Tag.ToString());
}

private void magic(string currentcombo)
{
    string CurrentText = (ComboBox).(currentcombo).Text;
}

1 Answers

You can set the Tag property to the actual ComboBox and avoid your problem altogether.

//code when defining your button...
{
     sqlButton.Tag = comboBoxA;  //instead of comboBoxA.Name
}

private void SQLButton(object sender, EventArgs e)
{
    Button button = sender as Button;
    ComboBox comboBox = button.Tag as ComboBox;

    if (comboBox == null ) 
    {...}
    else
    {
        magic(comboBox);
    }
}

private void magic(ComboBox currentcombo)
{
    string CurrentText = currentcombo.Text;
}
like image 197
Austin Salonen Avatar answered Jul 12 '26 00:07

Austin Salonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!