הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, January 21, 2009 10:17 AM
Hi all,
In my project, i need to create an array of checkbox in the windows form using vb.net? How to create array of checkbox? Please help me.
Thanks in advance.Regards, Suresh R (MCA, TCE)
All replies (6)
Thursday, January 22, 2009 1:49 AM ✅Answered
Here's a more complete reply.
Public Class Form1 |
' A list to keep track of the TextBoxes you Added |
Dim checkBoxes As New List(Of CheckBox) |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
' The default offset for checkboxes that Visual Studio uses |
' when you put one check box under the other. |
Const checkBoxVerticleOffset As Integer = 22 |
' The text that will be displayed next to your text box |
Dim checkBoxLabels As New List(Of String) |
checkBoxLabels.Add("Green is keen!") |
checkBoxLabels.Add("Yellow is mellow!") |
checkBoxLabels.Add("Blue is cool!") |
' Note: Since I am adding the checkboxes to the list checkBoxes, checkBoxes.Count |
' is the number of the current CheckBox. I use this fact below. |
For Each checkBoxLabel As String In checkBoxLabels |
' Instantiate a new text box for each name |
Dim newCheckBox As CheckBox |
newCheckBox = New CheckBox |
' A Default Name. You probably don't really need this, but it might come in handy |
newCheckBox.Name = "CheckBox" & CStr(checkBoxes.Count) |
' Set the text displayed next to the check box |
newCheckBox.Text = checkBoxLabel |
' You will have to add the new text box to the form or |
' a container control of some sort to display it. I'm adding it to a GroupBox |
GroupBox1.Controls.Add(newCheckBox) |
' Auto size the text portion of the control. |
newCheckBox.AutoSize = True |
' This is one way that you can make sure that the check boxes don't overlap when you add them to |
' the container control |
newCheckBox.Location = New System.Drawing.Point(6, 18 + checkBoxes.Count * checkBoxVerticleOffset) |
' Tabs through the boxes in the proper order |
newCheckBox.TabIndex = checkBoxes.Count |
' Just makes the textboxes look right |
newCheckBox.UseVisualStyleBackColor = True |
' And don't forget to add your new check box to your list! |
checkBoxes.Add(newCheckBox) |
Next |
End Sub |
End Class |
If you really need the checkboxes in an Array as opposed to a list, you can then call checkBoxes.ToArray().
-Hazelrah
Wednesday, January 21, 2009 11:18 AM
Dim chk1 As New CheckBox, chk2 As New CheckBox |
Dim chkBoxArray As CheckBox() = {chk1, chk2} |
S/W Engineer http://glotech.co.in
Thursday, January 22, 2009 2:01 AM
if you don't mind me asking why in an array?
Thursday, January 22, 2009 4:12 AM
I may be wrong but my assumption that Suresh said 'array' in general terms, meaning something like "a set of widgets aligned in an orderly fasion", not the Array type in .NET
-Hazelrah
Thursday, January 22, 2009 9:26 AM
i need to use checkboxes repeatedly in my project, tat is why i need array of checkbox. Its uesful one Hazelrah.
But there is one problem..... If i add new checkbox in that array at run time, it does not updated properly.
The scenaio is that if i add new checkbox names "Rose is sweet" at run time by clicking a button, from tat moment or next time wen i load the same form, it should be loaded along with nes checkbox.
Can anyone help me...
Thanks in advance.Regards, Suresh R (MCA, TCE)
Sunday, January 25, 2009 1:25 AM
Make sure that you aren't forgetting this line. This is the one that will actually make the Control show up.
' You will have to add the new text box to the form or |
' a container control of some sort to display it. I'm adding it to a GroupBox |
GroupBox1.Controls.Add(newCheckBox) |
Also, be aware, that I'm using a GroupBox as an example. You can add a control to a Form, a Panel and several other container Controls.
-Hazelrah