Solved

programatically generate checkbox

Posted on 2004-08-12
3
565 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now