Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 915
  • Last Modified:

Textbox array

I have a form and like to fill in the textbox, mainly to display data in a grid of 24 row by 5 column.

I can manually place the label on the form 24*5 of them, but surly, there must be better way!,

Below is the example program (which does not work), can anyone suggest how to do this that work for this case
 
                  for(int y=0;y<=10;y++)
                  {
                        this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                        this.label1.Location = new System.Drawing.Point(128+(y*50), 104);
                        this.label1.Name = "label"+y.ToString();
                        this.label1.TabIndex = 0;
                        this.label1.Text = "label"+y.ToString();
                  }

Thanks
0
riscy
Asked:
riscy
  • 3
  • 3
  • 2
  • +2
1 Solution
 
riscyAuthor Commented:
I also need to add text from the array as well next to the label
0
 
Arthur_WoodCommented:
VB.NET does NOT suuport Control Arrays, as VB 6 did.

you should think of an alternate way to show your information.  Perhaps you could use a Grid control, as these are lables, and thus not 'interactive' (the user can't change the Label directly.

AW

0
 
Arthur_WoodCommented:
sorry, should have said .NET in general does not support Control Arrays, as you appear to be using C#.  The other comment holds though...think of an alterantive way to display your information.

AW
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
purplepomegraniteCommented:
You can dynamically add controls to the form via code.  Example code is here: http://www.devx.com/vb/Article/16410/0/page/4
0
 
nayernaguibCommented:
I'm afraid it *is* possible to create an array of Labels or TextBoxes or any other object. Check this out:

_______________________________________________________________

Label []lbl=new Label[11];
for(int y=0;y<=10;y++)
{
      lbl[y]=new Label();
      lbl[y].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      lbl[y].Width=50;
      lbl[y].Location = new System.Drawing.Point(128+(y*50), 104);
      lbl[y].Name = "label"+y.ToString();
      lbl[y].TabIndex = 0;
      lbl[y].Text = "label"+y.ToString();
      this.Controls.Add(lbl[y]);
}

_______________________________________________________________

_________________

  Nayer Naguib
0
 
purplepomegraniteCommented:
Duuh, I didn't notice it was c# either!!

Still, this Technet article covers it all pretty well: http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskaddingcontrolstoform.asp
0
 
purplepomegraniteCommented:
But nayernaguib's code above covers it even better!
0
 
netcoolCommented:
Hi

if you want to create control array of label box u can use this code in VB

This code is for textbox change text to label, but place a textbox text1 in the form and set index property as 0

Private MyTextboxArray As Object
Private Sub Command1_Click()
For i = 1 To 10
    For c = 1 To 5
     With addTextbox
          .Top = Text1(0).Height * i + Text1(0).Top
          .Left = Text1(0).Width * c + Text1(0).Left
          .Visible = True
          .SetFocus
          .Text = Text1(Index)
      End With
      Next c
Next i
End sub

Private Sub Form_Initialize()
Set MyTextboxArray = Me.Controls("Text1")
Text1(0).Visible = False
End Sub

Public Function addTextbox() As TextBox
   Dim n As Integer
   n = MyTextboxArray.ubound + 1
   Load MyTextboxArray(n)
   Set addTextbox = MyTextboxArray(n)
End Function

if you want to but text in the textbox u can use this

Private Sub Command2_Click()
Text1(1).Text = "hello"
Text1(2).Text = "welcome"
Text1(3).Text = "hai"
Text1(20).Text = "hello"
End Sub

0
 
Arthur_WoodCommented:
a VB6 Control Array is not quite the same thing as an array of controls.  All I was sying was that VB.NET does NOT have an automatic 'control array' mechanism, the way VB 6 did.  Yes, it is possible to create an Array of controls, and to manually add the Event Handler (Control.AddHandler....), it is just a lot more work, and for the beginner, it can be overwhelming.

netcool, you are showing code for VB 6, not VB.NET, and definitely NOT C#.  The original question is clearly C#, so we're working in the .NET realm, and VB 6 control arrays don't apply.  .NET does not support code like

Text1(1).Text = "hello"
Text1(2).Text = "welcome"
Text1(3).Text = "hai"
Text1(20).Text = "hello"


That is not valid in .NET

AW
0
 
riscyAuthor Commented:
Thank for the quick response, I tested the nayernaguib solution and found to work very nicely in this case

I will later modify simple the code to work for two diemension array.

One final problem:-

            private Label[] label = new Label[25];
            #region Windows Form Designer generated code
            private void InitializeComponent()
            {
                  this.SuspendLayout();      
                  for (int i=0 ; i <= 23 ; i++)
                  {
                        label[i]=new Label();
                        label[i].Width=64;
                        label[i].Height=20;
                        label[i].Left=100;
                        label[i].Top=100+(22*i);
                        label[i].BackColor=Color.Gainsboro;
                        label[i].BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
                        label[i].Text="300.0mV";  
                        this.Controls.Add(label[i]);
                  }
                  }
In Visual Studio, When i view the window form, ie test.cs[Design], it generates the error with red cross because it does not understand how to place the lable based on above code, clearly it is not at the "right" place, any suggestion where to put it elsewhere?.

I also found it possible to add event into the array as well.

Riscy
0
 
nayernaguibCommented:
Seems like Visual Studio .NET Designer does not process loops or conditions at design time to display forms.

Your code needs compilation to be processed. However, the Designer will happily display a form with the following code added to the InitializeComponent() method (given that lbl Label is already declared):

______________________________________________________

this.SuspendLayout();    
lbl=new Label();
lbl.Width=64;
lbl.Height=20;
lbl.Left=100;
lbl.Top=100;
lbl.BackColor=Color.Gainsboro;
lbl.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
lbl.Text="300.0mV";  
this.Controls.Add(lbl);

______________________________________________________

You can add your code to the Form_Load event handler, but the components added will not be shown on the form at design-time. You can use an empty test form to view any modifications without taking long time to compile.

________________

  Nayer Naguib
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now