• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

programatically generate checkbox

Hi Experts,
   I need to create checkbox in RUNTIME based on the criteria pooling from table which means that got 5 items it will display 5 checkboxes , if got 6 items it will display 6 checkboxes.
How can I do it?


Thanks,rgds,
KA
0
SAPKEN
Asked:
SAPKEN
1 Solution
 
Justin_WCommented:
Assuming you want to do so in an ASP.NET web form, you have 2 options:

1. Use the CheckBoxList control and set its DataSource.  It will display 1 CheckBox per item in the DataSource.

2. Dynamically create CheckBox controls using code.  This PAQ describes how to do this for LinkButtons:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21023551.html
You would do exactly the same thing for CheckBox controls instead of LinkButtons.

If you are doing this in a Windows Form, then you just instantiate CheckBox controls and add and position them on your form.
0
 
bramsquadCommented:
i got this from a PAQ, hopefully modded to what you need

       'this will be the locations
       dim x, y as integer

       x = 100
       y = 100


        For i = 0 To ControlCount - 1

            Dim ctrl As New System.Windows.Forms.CheckBox

            ctrl.Name = "CheckBox" + i.ToString()
            ctrl.Text = "CheckBox" + i.ToString()

            ctrl.Location = New System.Drawing.Point(x, y)
            'so its not one on top of the other
            y += 16

            'im assuming youre not needing event handlers...

            Controls.Add(x)                     ' Add the control to the collection of controls
        Next

~b
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

   Here's a sample, but isntead of reading the No of tables, it will read it from the user using InputBox, reading it from DB is easy using command object and call ExecuatScaler to return the No of CheckBoxes you want to create

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Num As Integer = InputBox("Enter No of check boxes", "CheckBoxes No")
        Dim I As Integer

        For I = 1 To Num
            Dim chk As New CheckBox
            chk.Left = 0
            chk.Top = 20 * I
            chk.Text = "CheckBox No " & I
            Me.Controls.Add(chk)
        Next
    End Sub

HTH

Regards,
Mohammed
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now