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

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
gempir
  • 1,791
  • 4
  • 22
  • 46
  • you are asking for c# or php ? – Mostafiz May 10 '16 at 11:41
  • 1
    C#, I was just making an example because I know how its done in PHP – gempir May 10 '16 at 11:42
  • Why? Does the name of button is matter? No! I suspect you want to catch click event. Am i right? – Maciej Los May 10 '16 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 – gempir May 10 '16 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. – Maciej Los May 10 '16 at 12:05
  • Yes and I have that already. Let's say I want to highlight all button where a ship was once it has been destroyed, I can't use a click event then because the user didn't click on the other buttons this time. – gempir May 10 '16 at 12:07
  • So, you have to provide more information about the way you store such of data. At this moment, the soultion provided by Avi Turner seems to be the best. – Maciej Los May 10 '16 at 12:11

5 Answers5

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;...

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
  • I am in the Mainwindow : Window class but I am it's not the correct context is seems. – gempir May 10 '16 at 11:57
  • my program contains no definition for "Controls" I translated because its german – gempir May 10 '16 at 12:02
  • @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? – Maciej Los May 10 '16 at 12:24
  • 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. – gempir May 10 '16 at 12:31
  • @danielps1, if it's wpf project the way you access controls is different. You should mention that during posting question. See this: http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type – Maciej Los May 10 '16 at 12:54
  • Post the German error message. Meine Muttersprache ist Deutsch. – Peter Huber May 10 '16 at 17:27
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
                 }

              }
        }
    }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • 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? – gempir May 10 '16 at 11:45
  • 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 May 10 '16 at 11:51
  • @Draken you are correct, you can textbox name using my updated code and apply for some of textbox only – Mostafiz May 10 '16 at 12:00
  • I just thought it's redundant to have a seperate dictonary to get all the buttons eventhough I already know their names. – gempir May 10 '16 at 12:05
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;
}
Abdellah OUMGHAR
  • 3,627
  • 1
  • 11
  • 16
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?

Community
  • 1
  • 1
Maciej Los
  • 8,468
  • 1
  • 20
  • 35
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.

Peter Huber
  • 3,052
  • 2
  • 30
  • 42