Solved

programatically generate checkbox

Posted on 2004-08-12
3
566 Views
Last Modified: 2012-05-05
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
Comment
Question by:SAPKEN
3 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
ID: 11786242
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
 
LVL 8

Expert Comment

by:bramsquad
ID: 11786423
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
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11786436
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question