0

Hi Sorry if the answer has been given already, but I have searched high and low for this and have drawn a blank.

I have a grid of buttons that disappear when clicked. This is good. The buttons are named tile1, tile2, tile3 etc... their numbers are contiguous.

Their click_handlers currently each manually 'disappear' the sending buttons like this...:

tile1.visible = false

I am looking for a simple way to reference the tile names and numbers so I can

  1. disappear the buttons in groups by referencing their names and numbers (ie disappear the 1st 3 in one command (or loop) followed by the next 2 together followed by the next 6 together... you get the picture).
  2. Create a reset loop that resets the buttons' visibility property from within the code without having to manually type every button name.

So in an ideal world it might look something like...:

for (i=1; i<=MAX_TILE_NUMBER; ++i)
   {
    string currentTile = "tile" + i
    currentTile.visible = false
   }

I can feel it in my bones that there is an easy way to concatenate the string portion of the button name with the index "i" in my for loop and use this to reference the button controls one by one or in groups but I have not yet found it. please save me from imminent hair-rending insanity.

Many thanks.

Thanks

JB

3
  • what platform we are talking about here, winform, wpf, asp.net, etc? Commented Feb 1, 2014 at 13:16
  • Have you not found my answer helpful? Have you even tried it? If you found any of the provided answers useful, you should mark it as accepted by clicking a green mark below an answer's vote counter. Commented Mar 3, 2014 at 12:37
  • Sorry, We've had an Ofsted inspction which threw things off by a couple of weeks. I'll give your dictionary approach a go & let you know. Thanks for the reply. I do appreciate the time you took. Commented Mar 3, 2014 at 20:42

2 Answers 2

1

Just put your buttons to a collection, a dictionary should fit best for you.

var buttons = new Dictionary<string, Button> { { "tile1", tile1 }, ... };
buttons.Add("tile2", tile2);
etc.

and then operate with this dictionary. For example, make the first three buttons invisible

var firstThree = buttons.Take(3);
foreach (var b in firstThree)
   b.Value.Visible = false;

You can pick buttons however you wish by forming LINQ queries on this dictionary.

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

Comments

0

There is function to find control by name

see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find%28VS.80%29.aspx

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.