Dynamically accessing objects on a form  
Author Message
utkuozan





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

I have 12 buttons placed on a form simplye named as button1, button2, ...,button12. I want to change their properties in a loop. Is there any means of possibilities that I might reach their properties in such manner or somewhat alike in a single loop

for (i=1;i<=12;i++)

{

button+tostring(i).visible=false;

}




Visual C#9  
 
 
ahmedilyas





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

if you placed the buttons on a form via designer view or even programmatically created and added it into the Controls collection, you can access them via the Controls collection. Or if you know the names of the buttons and it was in the format of:

ButtonNumber

then you can still do that too.

 

foreach(Control curControl in this.Controls)

{

   if (currentControl.GetType == typeOf(Button))

   {

      //current control IS a button. Now modify its properties like so:

      currentControl.Visible = false;

   }

}

 

you could do it this way too:

 

for (int counter = 1; counter <= 12; counter++)

{

   Button theButton = this.Controls["Button" + counter.ToString()];

   if (theButton != null)

   {

      theButton.Visible = false;

   }

}

is this something you are after



 
 
utkuozan





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

At a first glance at the code I really think that you've got it. But unfortunately the code does not compile at all causing errors.Since I do not want to change the property of all objects (buttons in this case) I prefer the second code but while compilation this code gives a error saying that

"Connot implicitly convert type 'System.Windows.Forms.Controls' to 'System.Windows.Forms.Button'.  An explicit conversion exists. (Are you missing a cast )

Even if I trry the first code I get two errors:

* The name "typeOf" does not exist in the current context

* 'System.Windows.Forms.Button' is a 'type' but is used like a 'variable'

 

Is there a way that we can solve thi problem



 
 
ahmedilyas





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

hmmm interesting as I am able to compile and run it fine. I did notice however the 2 brackets () were missed out in the GetType part. The O in typeof was lowered, again its best if you look at the Intellisense and make sure that the case sensitivity/keyword matches

foreach(Control curControl in this.Controls)

{

if (currentControl.GetType() == typeof(Button))

{

//current control IS a button. Now modify its properties like so:

currentControl.Visible = false;

}

}

you could do it this way too:

for (int counter = 1; counter <= 12; counter++)

{

Button theButton = this.Controls["Button" + counter.ToString()];

if (theButton != null)

{

theButton.Visible = false;

}

}



 
 
danych





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

hi u can try to create an array of buttons

button[] myBunttonArray = new button[];

for(int i = 0;i<myButtonArray.Length;i++)

{

myButtonArrayIdea = new button();

}

and then address each button's pperties as u wish

I tried it and it works

regards



 
 
utkuozan





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

Thank you very much for your reply. This time the first code compiled and worked properly. The second was still giving error but solved the problem by casting the control to a Button, and now it is working fine. Thnak you very much. Now the code looks like:

for (int counter = 1; counter <= 12; counter++)

{

Button theButton = (Button)this.Controls["button" + counter.ToString()];

if (theButton != null)

{

theButton.Visible = false;

}

}



 
 
utkuozan





PostPosted: Visual C# Language, Dynamically accessing objects on a form Top

hi u can try to create an array of buttons

button[] myBunttonArray = new button[];

also thank you very much for your reply. Using an array of buttons was already being used by our project, but for codeing issues we had to convert it. Again thank you very much for your consideration.