views:

1278

answers:

7

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;
}
+2  A: 

If you have the string id value of a control then you can use FindControl to get a reference to the control.

Something like ...

Button btn = (Button)FindControl("some_id");
Peanut
+3  A: 

I think I understand what you're after -

You'll want to change your "magic" routine to something like:

private void magic(string currentCombo) {
    ComboBox box = this.Controls.Find(currentCombo) as ComboBox;
    if(box != null) {
        // You can do your "work" here...
        string currentText = box.Text;
    }
}
Reed Copsey
Should be a (ComboBox)-cast instead of using as ... you don't want to get a NullRef-exception instead of a Cast-exception
tanascius
I prefer this way, personally. I added a null check for you, though.
Reed Copsey
Yeah, with null-check I like it more, too :)
tanascius
+6  A: 

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;
}
Austin Salonen
how do I do that?, that seems to be an interesting approach.
zion
Tag can be anything. So when you set the Tag of your button to comboBoxA.Name just use comboBoxA instead.
Austin Salonen
So I can use comboBoxA.Text and that will retrieve the current contents and pass it on to the method;private void SQLButton(object sender, EventArgs e){ magic(((Button)sender).Tag);}
zion
thanks,this.button1.Tag = sourceComboBox.Text;Simple Solution
zion
That's code is it, thanks a LOT!
zion
Elegant Solution!
zion
+1  A: 

I don't know if its winforms or asp.net. I am assuming it to be winforms

You could use this.Controls(theNameofTheControl) instead of magic.

shahkalpesh
+1  A: 

Have a look at the Controls.Find Method, to get the instance of the Control using the name.

Mark Redman
A: 

If currentcombo paramter in magic function is the identifier for the control you are going to change then use Page's function FindControl:

string CurrentText = ((ComboBox)FindControl(currentcombo)).Text;

Page.FindControl() method searches the page naming container for a server control with the specified identifier.

Alex
FindControl doest not exist in the curren context? (am I missing a reference?using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;
zion
A: 

I love the way you can reassign, so to speak. I have 7 buttons and now, thanks to you, I only have 5 Events instead of 35 separate events. Really allowed me to modularize and centralize events. BTW, did this with the 7 ListViews, but sorta different and ContextMenu items. Thanks again. Really appreciate that tip.

Marshall Neill