Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > Adding Multiple Checkboxes on FormLoad Depending on Amount of Items In Database


Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2008, 06:55 PM
Sal Sal is offline
Newcomer
 
Join Date: Aug 2008
Location: Australia - Sydney
Posts: 16
Default Adding Multiple Checkboxes on FormLoad Depending on Amount of Items In Database


Hey my first post so be gentle,

The Title says it all Adding Multiple Checkboxes on FormLoad Depending on Amount of Items In Database.

I have gotten this far:
Private Sub frmMakePurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Database connections
mdacItems = New dacItems
Dim dt As DataTable
Dim dr As DataRow
dt = mdacItems.searchItems("", "", "", -1)
Dim pos As Integer = 0

For i As Integer = 0 To dt.Rows.Count - 1
dr = dt.Rows(i)
'' My problem is the variable name i need it to change as the loop goes through
Dim chk1 As New CheckBox()
chk1 .Text = dr("itemcode")
chk1 .Size = New Size(200, 20)
chk1 .Location = New Point(10, pos + 15)
Me.gbItems.Controls.Add(chk1)

Next

End Sub
Reply With Quote
  #2  
Old 10-20-2008, 09:48 PM
Targe Targe is offline
Contributor
 
Join Date: Nov 2006
Posts: 615
Default

Code:
Dim chk As New CheckBox() With chk .ID = "chk" & i.ToString() .Text = dr("itemcode") .Size = New Size(200, 20) .Location = New Point(10, pos + 15) End With Me.gbItems.Controls.Add(chk)
Reply With Quote
  #3  
Old 10-20-2008, 10:57 PM
Sal Sal is offline
Newcomer
 
Join Date: Aug 2008
Location: Australia - Sydney
Posts: 16
Default

Thanks alot Targe, forgot about the ID/Name
Reply With Quote
  #4  
Old 10-21-2008, 09:26 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'm pretty sure he meant the Name property, but I can't remember if ASP .NET has an ID property as well.

One thing I don't like about using just the name of the control is it makes things complicated if the check boxes are scattered across group boxes. An alternative would be to use a control array. These are detailed in a thread in the tutor's corner but it's easy to make a quick example.
Code:
Class YourForm

    Private _checkBoxes() As CheckBox

    Private Sub YourForm_Load(...) Handles Me.Load
        ' Get data...
        
        ' Initialize the array
        ReDim _boxes(dt.Rows.Count - 1)

        For i As Integer = 0 to dt.Rows.Count - 1
            _boxes(i) =  New CheckBox()
            _boxes(i).Text = dr("itemcode")
            _boxes(i).Size = New Size(200, 20)
            _boxes(i).Location = New Point(10, pos + 15)
            Me.gbItems.Controls.Add(_boxes(i))
        Next
    End Sub
End Class
The difference is subtle, but cuts out the aggravation of needing a cast. Let's say you're writing a function to see if a given checkbox is checked. Here's how you'd do it if you were relying on just the Name property:
Code:
Private Function GetCheckStatus(ByVal index As Integer) As Boolean
    Dim name As String = String.Format("chk{0}", index)
    Dim control As Control = Me.Controls("name")
    
    If control Is Nothing Then
        ' Hmmm.. bad name?
    End If
    
    Dim checkBox As CheckBox = TryCast(control, CheckBox)
    
    If checkBox Is Nothing Then
        ' cast failed
    End If
    
    Return checkBox.Checked
End Function
Notice that there's two error conditions: either there's no control with the indicated name, or the control with that name isn't a check box. All we want to do is return the value of a checkbox, but we had to write 3 lines of code and at least 4 lines of error checking to do it. If you have container controls such as a group box that contain check boxes, this gets more complicated as you have to recurse over every control's children.

Using a control array is simpler:
Code:
Private Function GetCheckStatus(ByVal index As Integer) As Boolean
    If index < 0 OrElse index > _boxes.Length Then
        Throw New ArgumentOutOfRangeException("index")
    End If
    
    Return _boxes(index).Checked
End Sub
Here, the only error conditions are an invalid index. We know the array exists because it's created during Load. We know that only CheckBox controls are in the array because of its type. So, we only have to write 1 line of code and 2 lines of error checking. It doesn't matter if the check boxes are in container controls; the array gives us direct access.

*edit* Also, you may want to consider using a FlowLayoutPanel to contain the checkboxes. You wouldn't have to calculate the location in this case because this panel is designed to arrange several controls for you. It handles overflow fairly well, too.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 10-21-2008, 10:54 PM
Targe Targe is offline
Contributor
 
Join Date: Nov 2006
Posts: 615
Default

Name...is that what it is? I deal with ASP all day, whoops! The control array is definitely the preferred thing to do. Quite useful for iterator patterns!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->