C# Form controls - urgent please help

Results 1 to 2 of 2

Thread: C# Form controls - urgent please help

  1. #1
    Join Date
    Dec 1969

    Default C# Form controls - urgent please help

    Hi there,<BR> I have an empty Windows Form(C#). Now, when form is loaded, I will create some 64 buttons (by writing a loop in constructor). The problem now is : I have to be able to access any of these controls in any manner. In other words, say, during some calculation, I need button (with name say - btn10) &#039;s properties. I dont want to go through every existing control and see if its name matches what I wanted. <BR>Thanks.

  2. #2
    Join Date
    Dec 1969

    Default RE: C# Form controls - urgent please help

    Seems odd to me that you&#039;d be needing a handle to a totally random button, like, normally you&#039;d get a reference to the button because it was the one clicked and you&#039;d use the sender argument to grab your instance... but, here&#039;s one way to get what you need:<BR><BR>When you create the control, add a reference to the instance in an array:<BR><BR>// button array<BR> Button[] arr = new Button[100] ;<BR><BR> // create new buttons<BR> for(int i = 0; i &#060; 99; i++) {<BR> Button button = new Button() ;<BR> button.Name = i.ToString() ;<BR> // probably wire-up to an event handler here...<BR> arr[i] = button ; // add to array<BR> }<BR> this.Controls.AddRange(arr) ; // add to form<BR><BR><BR>Then, when you need to retrieve the control, you just grab a reference from the array:<BR><BR><BR>Button myButton = arr[buttonIndex] ;<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts