0

I have tons of Buttons named like this x0y1

How do I access the variable name dynamically so I could loop all names by xiy1 or so.

in PHP it would be like ${"myString" . $randomvar}

I can't use a list or array because the the button already exist defined through the xaml

7
  • you are asking for c# or php ? Commented May 10, 2016 at 11:41
  • 1
    C#, I was just making an example because I know how its done in PHP Commented May 10, 2016 at 11:42
  • Why? Does the name of button is matter? No! I suspect you want to catch click event. Am i right? Commented May 10, 2016 at 11:58
  • No the buttons are kind of like a display out of multiple buttons to display a simple game (Battleships) so when someone actually its something it should change other buttons aswell sometimes Commented May 10, 2016 at 12:00
  • Battleships... So, you want to catch click event to be able to get what field has been clicked. All you need to do is to create common click event for each button then to get field coordinatings this way: Button btn = (Button)sender; Now, you have access to the properties of clicked button. Commented May 10, 2016 at 12:05

5 Answers 5

1

You can use:

var textbox = 
   this.Controls.OfType<TextBox>().Where(txb => txb.Name == "myString").FirstOrDefault();

This assumes you are in the context of your form (this.Controls).

And of course, don't forget to add using System.Linq;...

Sign up to request clarification or add additional context in comments.

6 Comments

I am in the Mainwindow : Window class but I am it's not the correct context is seems.
my program contains no definition for "Controls" I translated because its german
@danielps1, It's no matter of language, "Controls" is a container for controls for Windows Form class. Is it Windows Form project or any other?
No I meant the error message was in german and that might be hard to understand. This is a wpf project btw. not a windows forms.
@danielps1, if it's wpf project the way you access controls is different. You should mention that during posting question. See this: stackoverflow.com/questions/974598/…
|
0

You can get all the textbox using this method

void AllTextBox(System.Windows.Forms.Control.ControlCollection ctrls)
    {
       foreach (Control ctrl in ctrls)
       {
          if (ctrl is TextBox)
              {
                 if (ctrl.Name == "textBox1")
                 {
                    // do your stuf with textbox
                 }

              }
        }
    }

4 Comments

I guess this could be an option. But this seems kinda annoying because I have to create a dictonary extra and even exclude some buttons because I don't want all of them. Is there no way of accessing them dynamically?
You don't need to create the dictionary yourself, you should be able to get a list of controls attached to a panel or similar. Use the system, if you know what parent panels you want to modify, you can iterate its child controls
@Draken you are correct, you can textbox name using my updated code and apply for some of textbox only
I just thought it's redundant to have a seperate dictonary to get all the buttons eventhough I already know their names.
0

You can create function that return control by name :

Control GetControlByName(string Name)
{
    foreach(Control control in this.Controls)
        if(c.Name == Name) return control ;

    return null;
}

or Function with a specific control like that :

Button GetButtonByName(string Name)
{
    foreach (Control c in this.Controls.OfType<Button>())
        if (c.Name == Name) return c;

    return null;
}

Comments

0

For wpf project...

Let's say you have a grid named MyGrid and there's lot of buttons on it. You want to refer to the button named x0y1:

var btn = MyGrid.Children.OfType<Button>().Where(x=>x.Name=="x0y1");

Note: above code should work for flat structure (one level deep only).

You can achieve the same by using code provided in this thread: How can I find WPF controls by name or type?

Comments

0

Just call FindName("elementName"). FindName searches through all child elements of a FrameworkElement. To access any button by its name as string in a window, call the FindName() method of the window !

If your code is in a class inheriting from Window, just use:

Button button = (Button)FindName("xiy1");

If you write the code in a class not inheriting from Window but FrameworkElement, which is unlikely, use:

Window window = Window.GetWindow(this);
Button button = (Button)window.FindName("xiy1");

Check the MSDN documentation about Namescopes for more information about limitations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.