CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2012
    Posts
    5

    Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    I am very new to programming, so please bare with me. I am using VB 2010 express and am trying to make a program for a project that generates lottery numbers in a specific way. What I have is two forms. Form one has two text boxes and a button. Form two has a checkedlistbox that is pre-populated with numbers from 1-56. What I am trying to do is have the program open to form1, the user presses button1 ("Select Numbers") which opens form2. From there they will select numbers they want to pick from (narrowing their pool of lotto picks to only numbers they want to use). Then when they hit select, I want the numbers they have checked to return back to the textbox1 on form1. HOWEVER, I also only want 5 numbers, randomly selected, from the pool of checked numbers the user selected on form2 to return to the textbox1 on form1..... I may be in wayyy over my head, but I have all of the forms designed and I have been close to getting the forms to at least communicate, but still far from doing what I have described I am trying to do. Any and all help would be greatly appreciated at this point!!!

  2. #2
    Join Date
    Mar 2012
    Posts
    5

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    Code I have been trying:

    Form1:
    Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim NumberSelect As Form2
    Private Property CheckedListBox1 As Form2
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Using dialogue As New Form2
    dialogue.ShowDialog()
    Me.TextBox1.Text = dialogue.checkedlistbox
    End Using
    End Sub

    Form2:
    Public Class Form2

    Public Property checkedlistbox As String
    Get
    Return Me.CheckedListBox1.Text
    End Get
    Set(value As String)
    Me.CheckedListBox1.Text = value
    End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If CheckedListBox1.CheckedItems.Count > 0 Then
    Form1.Show()
    Form1.TextBox1.Text = checkedlistbox
    Else
    MessageBox.Show("select numbers")
    End If
    End Sub

    ------
    This code will return the last item checked in the checkedlistbox to the textbox in form1. It will not return all checked items. I was trying to at least get that far before I even attempted to figure out how to take the list of checked items, randomize them, and only display five of those randomized numbers in the textbox on form1.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    Look up the RANDOM() command. You can pick from a start index to the end index. Keep another list of YES or NO answers for each entry. See the steps?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Mar 2012
    Posts
    5

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    I now have the code working to send all of the checked items in checkbox1 on form2 to textbox1 on form1. Which I wanted to get working before I moved on to try to learn how to now just get 5 of the numbers randomly from those selected on the checkedlistbox (i.e. If the user selects 20 numbers on the checkedlistbox on form2 and hits submit, the program will take 5 of those 20 selected randomly and return them to the textbox on form1). Here is how I have done that:

    Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim NumberSelect As Form2

    Private Property CheckedListBox1 As Form2
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Using dialogue As New Form2
    dialogue.ShowDialog()
    Me.TextBox1.Text = dialogue.TextBoxText
    End Using
    End Sub
    End Class

    Public Class Form2
    Public Property TextBoxText() As String
    Get
    Return Me.TextBox3.Text
    End Get
    Set(value As String)
    Me.TextBox3.Text = value
    End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Randomize()
    Dim i As Integer = 0
    ' Loop through all the selected items of the listbox and append them to the textbox text property
    For i = 0 To CheckedListBox1.CheckedItems.Count - 1
    TextBox3.Text &= CheckedListBox1.CheckedItems.Item(i).ToString() & " "
    Next
    End Sub
    End Class

    ----------------
    I have a little more knowledge of the Random() command, but still am lost in how to incorporate it to do what I am trying to do.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    i is the key. compute a random between 0 and max-1
    Code:
    TextBox3.Text &= CheckedListBox1.CheckedItems.Item(i)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Mar 2012
    Posts
    5

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    I'm not sure if I have made this as simple as you are suggesting, but you have at least sent me in the right direction. Now I have a code that is working to return 5 random numbers from the numbers that are checked back to the textbox1 on form1, but it always gives me one duplicate number (i.e "2, 53, 2, 42, 52"). What is the simplest way to prevent this code from choosing the same number from the pool twice, and why is it happening every time? Here is the new code:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Form1.TextBox1.Clear()

    Dim randomNumber As Integer

    Dim i As Integer = 0


    If Integer.TryParse(5, i) AndAlso i <= Me.CheckedListBox1.CheckedItems.Count Then
    Do While i <> 0
    randomNumber = Rnd.Next(0, Me.CheckedListBox1.CheckedItems.Count - 1)
    TextBox3.Text &= (Me.CheckedListBox1.CheckedItems(randomNumber)).ToString() & " "
    i -= 1 'not sure what this line does...
    Loop

    End If

    End Sub

  7. #7
    Join Date
    Mar 2012
    Posts
    5

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    Update: I thought I had found my issue with the repeating numbers. I thought I needed to define "randomnumber" as "New Integer", not just "Integer" AND add "If randomnumber = 0...." But that didn't work either, I am still getting duplicate numbers.
    I would also like to determine if there is a way to do something else, and please let me know if I need to start a new thread for it, but what I would like to do to complete this project is to find a way to have form2 not clear the checked boxes from the previous time it was used when it is opened again AND a way to be able to save the checked boxes so a user can recall their selections when they open the program... Thanks for all your help so far!

    The most recent code I am using to get the 5 random selections from the checked items is as follows:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Form1.TextBox1.Clear()

    Dim randomNumber As New Integer
    Dim i As Integer = 0
    If randomNumber = 0 AndAlso Integer.TryParse(5, i) AndAlso i <= Me.CheckedListBox1.CheckedItems.Count Then
    Do While i <> 0
    randomNumber = rnd.Next(0, Me.CheckedListBox1.CheckedItems.Count - 1)
    TextBox3.Text &= (Me.CheckedListBox1.CheckedItems(randomNumber)).ToString() & " "
    i -= 1
    Loop
    End If

    End Sub
    Last edited by crisstoph; April 5th, 2012 at 08:26 AM. Reason: Thought change worked, found out it didn't

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Visual Basic 2010 Express - Need Help Passing Data from CheckedListBox to TextBox

    Set up another field (of whatever TYPE you want. String would work)

    If they correspond to the choices, you set them all to 0 to start. Then, as you pick one, you check to make sure that it's 0 in the PICKED() field. If so, change to 1, and you have a unique pick.

    I use that method to pick cards...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured