-1

I mean, all button names I have are like "btActionXX", being 'XX' 2 numbers taked from Row and Column from a Matrix. For this numbers, I have strings like:

string btName = "btAction";
string ending = "01";

Is there some way to use ' "btName" + ending ' (parsed "btAction01") to use properties from a Button named as this? Something like Button.fromName("btName"+ending); (this not works) that returns a Button object, or somthing like this?

SomeBody
  • 7,515
  • 2
  • 17
  • 33
  • 2
    Is this WinForms, WPF or some other GUI framework? – SomeBody Feb 23 '21 at 08:45
  • I think this is something you want: https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type – Presi Feb 23 '21 at 08:46
  • Does this answer your question? [How can I find WPF controls by name or type?](https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type) – phuzi Feb 23 '21 at 08:48
  • @phuzi from the information provided, how do you know it is WPF related? – Cleptus Feb 23 '21 at 08:51
  • 1
    What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! - – TaW Feb 23 '21 at 10:24
  • 1
    Winforms: If you are really looking for the Name property and not the varaiblename yopu can either use a controls.Where linq to search for typeof Button and Name= yourname or you can use a [recursive function](https://stackoverflow.com/questions/32022010/how-to-draw-a-semi-transparent-rectangle-on-panel-containing-some-user-controls/32023219#32023219) to collect all controls. Note that dynamically created controls do by default not have a Name set and also the Name is not unique. Example: `var btns = yourParentContainer.Controls.OfType – TaW Feb 23 '21 at 10:24
  • Sorry, this is for Windows Forms. – Sergio Quinteiro Feb 24 '21 at 12:05
  • @TaW Okey this can works. I'll try this way as soon as possible. Thanks! – Sergio Quinteiro Feb 24 '21 at 12:06

1 Answers1

2

Please take a look at this thread: Get a Windows Forms control by name in C# There is API method called Find. Quote from the Microsoft documentation: "Searches for controls by their Name property and builds an array of all the controls that match." I think this is what you're looking for.

Rafi
  • 21
  • 2