Results 1 to 14 of 14

Thread: [2008] Place checkbox on form at runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    [2008] Place checkbox on form at runtime

    Basically what I want to do is read multiple values from an access database and place that value in a groupbox on the form.

    Can anyone explain how I can do this and give me some code examples?

    This is what I have so far:

    Code:
            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\t4c.mdb")
            Dim adapter As New OleDbDataAdapter("SELECT * FROM Spells", conn)
            Dim dt As New DataSet("Spells")
            adapter.Fill(dt, "Spells")

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    You may be better off using a DataGridView. You can just bind your data to the grid and a Boolean column will automatically generate a column of check boxes.

    If you really do want to use a GroupBox and add CheckBox controls manually then I'd probably suggest using a TableLayoutPanel. You can loop through the rows of the table and, for each one, create a CheckBox and add it to the TLP. Once you're done you can use the size of the TLP to resize the GroupBox if needed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Place checkbox on form at runtime

    I second the suggestion for using a grid, especially if there are a largely unknown number of fields. After all, there is only so much that you can put into a groupbox and still have it look even half way decent. The TableLayoutPanel is ok, but still not my favorite solution for a small number of checkboxes (for a large number, my favorite solution would be a grid, listview, listbox, or other).

    I strongly feel that the best way to make a good looking form is at design time. It's far easier to put controls on a form then, too. Therefore, add the controls that you need and hide them all at design time. You can then show just the controls needed, but you know that they are all positioned correctly.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    To add to Shaggy's list of possible controls, and something I probably should have mentioned myself, is the CheckedListBox. It's purpose is specifically to display a list, of indeterminate length, of items that can be checked or unchecked. Does that sound anything like what you need? It does to me.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Place checkbox on form at runtime

    The checkedlistbox sounds perfect..

    But how can I tell in code if an item is checked?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    Quote Originally Posted by rhijaen
    The checkedlistbox sounds perfect..

    But how can I tell in code if an item is checked?
    Have you read the MSDN documentation for the CheckedListBox class?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Place checkbox on form at runtime

    Yes I read it and figured it out..now I have another problem

    DirectCast(ActiveControl, CheckedListBox).checked = False

    This worked when it was

    DirectCast(ActiveControl, CheckedBox).checked = False

    But now it doesn't with checkedlistbox.

    What is the proper conversion?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    A CheckBox is just one box that can be checked or not, so it having a Checked property that is either True or False makes sense. Ask yourself whether it makes sense for a CheckedListBox to have such a property? A CheckedListBox can contain any number of items, all of which might be checked or not. How could a single property convey that? Logic would dictate that there must be a member that will tell you whether an individual item is checked or not.

    Now, go back and read the documentation. If you haven't read the member listing then you haven't read the documentation properly. Every class topic has a link to the list of members for the class, or you can just get there straight from the index. I'm assuming that you haven't read that topic because the answer to your question is fairly obvious once you have. Answers that are obvious from the documentation are not answers that I'm prepared to give.

    This may seem harsh but the sooner you learn to get the information you need from documentation the better it is for you. You'll solve your own problems faster and you'll learn more. Every time someone here is given an answer that they could easily have found for themselves, I consider a learning opportunity to have been missed. People learning is the whole reason I post here.

    One final note. You might also consider having a quick look down the Intellisense listing of members too. For the most their purpose is relatively clear from their name and it certainly is the case here.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Place checkbox on form at runtime

    Okay..

    DirectCast(ActiveControl, CheckedListBox).SetItemChecked(, False)

    What goes before the comma? I'm completely stumped.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    Have you read the documentation for the SetItemChecked method? Firstly, it tells you what the first parameter represents. Secondly, it provides a code example that uses the method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Place checkbox on form at runtime

    Yes, I have.

    You're supposed to use the index..but I have no index. I want the currently selected index to uncheck itself.

    I would normally use something like chkListAir.SelectedIndex but I have multiple checkedlistboxes.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    You already know how to get a reference to the active CheckedListBox because you're already doing it:
    vb.net Code:
    1. DirectCast(ActiveControl, CheckedListBox)
    So, simply assign that reference to a local variable and then you can use that variable as much as you like:
    vb.net Code:
    1. Dim activeCheckedListBox As CheckedListBox = DirectCast(ActiveControl, CheckedListBox)
    2.  
    3. activeCheckedListBox.SetItemChecked(activeCheckedListBox.SelectedIndex, False)
    One point to note is that using DirectCast like this will throw an exception if it ever gets executed when the active control is not a CheckedListBox. If there's any possibility of that then you should use TryCast instead, then test for a null reference.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Place checkbox on form at runtime

    This doesn't work when checking a checkedlistbox and freezes when you uncheck it.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Place checkbox on form at runtime

    Quote Originally Posted by rhijaen
    This doesn't work when checking a checkedlistbox and freezes when you uncheck it.
    Um, cobblers. It works alright so, if it's not working for you, you must be doing something wrong. As you've chosen not to show us what you are doing by posting some code or describing exactly what steps you are performing, it's basically impossible for us to say what the issue would be.

    My first guess would be that the ActiveControl is not a CheckedListBox. For instance, if you're executing this code in the Click event handler of a Button then the Button will be the ActiveControl, not the CheckedListBox.

    To see what I'm talking about follow these steps:

    1. Create a new WinForms project.
    2. Add a ToolStrip to the form.
    3. Add a button to the ToolStrip.
    4. Add two CheckedListBoxes to the form.
    5. Add several items to each CheckedListBox.
    6. Add the following code:
    Code:
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Me.ToggleSelectedItem()
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click
        Me.ToggleSelectedItem()
    End Sub
    
    Private Sub ToggleSelectedItem()
        Dim activeCheckedListBox As CheckedListBox = TryCast(Me.ActiveControl, CheckedListBox)
    
        If activeCheckedListBox Is Nothing Then
            MessageBox.Show("A CheckedListBox is not currently active.")
        Else
            Dim selectedIndex As Integer = activeCheckedListBox.SelectedIndex
    
            If selectedIndex = -1 Then
                MessageBox.Show("An item is not currently selected.")
            Else
                activeCheckedListBox.SetItemChecked(selectedIndex, _
                                                    Not activeCheckedListBox.GetItemChecked(selectedIndex))
            End If
        End If
    End Sub
    Now run the project and play around with selecting various items and clicking the buttons. You'll notice that, as long as an item is selected, clicking the button on the ToolStrip will toggle its Checked state. That's because doing so doesn't remove focus from the CheckedListBox. You'll also see that using the regular Button doesn't work. That's because clicking a Button will focus it, thus the Button becomes the ActiveControl.
    Last edited by jmcilhinney; Sep 23rd, 2008 at 11:37 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width