locked
Assign values from checkbox checked RRS feed

  • Question

  • Hello ive an problem and i dont have knowledge to do it... 

    Ive 30 variables like:

    Dim a1 = "0AFF"

    Dim a2 = "FFFF"

    And so on...

    Ive 30 checkboxs each checkbox need to get an variable assigned. 

    For example checkbox 1 = a1, checkbox 2 = a2... 

    From this 30 checkboxs i only want be possible check 10.

    So what i need is when i check 10 random checkboxs in this example could be checkbox:

    5 9 22 11 3 25 28 30 1 2 i need to get the variable and perfom action in my case i will write to file the assigned variables of the selected check boxs using filestream with position. 

    Hope you can understand and thank you so much. 



    • Edited by extream87 Sunday, September 22, 2019 3:26 PM
    Sunday, September 22, 2019 3:25 PM

Answers

  • Hi

    Give this a try and see if it is anywhere close to what you are asking for.

    This is a stand alone example and if you wanttotry itout, start a new project and put a single Button1 at bottom of Form1, anchored left and bottom. Then copy/replace all Form1 code with this code.

    ' Form1 with a Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim testfilename As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFile.dat")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim adds As New List(Of Integer)({&H1, &H3, &H5, &H7, &H9, &HB, &HD, &HF, &H11, &H13, &H15, &H17, &H19, &H11B, &H1D, &H1F, &H21, &H23, &H25, &H27, &H29, &H2B, &H2D, &H2F, &H31, &H33, &H35, &H37, &H39, &H3B})
    
    		Dim x As Integer = 8
    		Dim y As Integer = 10
    		For i As Integer = 1 To 30
    			Dim cb As New CheckBox
    			With cb
    				.Name = "MyCB" & i.ToString
    				.Location = New Point(x, y)
    				.AutoSize = True
    				.Text = i.ToString
    				.Tag = adds(i - 1)
    				y += 24
    				Dim w As Integer = .Width + 4
    				If i Mod 10 = 0 Then
    					y = 10
    					x += w
    				End If
    			End With
    			AddHandler cb.CheckedChanged, AddressOf CheckedChanged
    			Controls.Add(cb)
    		Next
    	End Sub
    	Sub WriteValues()
    		Dim hex As String = "00FFFFFF3F00010000"
    
    		Dim bytes() As Byte = Enumerable.Range(0, hex.Length \ 2 - 1).Select(Function(n) Convert.ToByte(hex.Substring(n * 2, 2), 16)).ToArray()
    
    		Using file As New IO.FileStream(testfilename, IO.FileMode.OpenOrCreate)
    			For Each c As Control In Controls
    				If c.GetType = GetType(CheckBox) Then
    					If c.Name.StartsWith("MyCB") Then
    						If CType(c, CheckBox).Checked Then
    							file.Position = CInt(c.Tag)
    							file.Write(bytes, 0, bytes.Length)
    						End If
    					End If
    				End If
    			Next
    		End Using
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		WriteValues()
    	End Sub
    	Private Sub CheckedChanged(sender As Object, e As EventArgs)
    		Dim cb As CheckBox = CType(sender, CheckBox)
    		Dim chk As Integer = 0
    		For Each c As Control In Controls
    			If c.GetType = GetType(CheckBox) Then
    				If c.Name.StartsWith("MyCB") Then
    					If CType(c, CheckBox).Checked Then
    						chk += 1
    					End If
    				End If
    			End If
    		Next
    		If chk > 10 Then
    			cb.Checked = False
    		End If
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Friday, September 27, 2019 4:21 PM
    Sunday, September 22, 2019 8:45 PM

All replies

  • Hello,

    If possible use a CheckedListBox e.g.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' feed mocked data for CheckedListBox
            Dim nameList As New List(Of Person) From
                    {
                    New Person() With {.Id = 1, .Name = "Karen"},
                    New Person() With {.Id = 2, .Name = "Jim"},
                    New Person() With {.Id = 3, .Name = "Bob"},
                    New Person() With {.Id = 4, .Name = "Anne"},
                    New Person() With {.Id = 5, .Name = "Burt"},
                    New Person() With {.Id = 6, .Name = "Mike"},
                    New Person() With {.Id = 7, .Name = "Frank"}
                    }
    
            CheckedListBox1.DataSource = nameList
            CheckedListBox1.CheckOnClick = True
    
        End Sub
        ' how many items may be checked
        Private maxNumberOfCheckedItems As Integer = 3
        ''' <summary>
        ''' Logic to restrict how many items may be checked
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _
            Handles CheckedListBox1.ItemCheck
    
            Dim items = CType(sender, CheckedListBox)
    
            If items.CheckedItems.Count > (maxNumberOfCheckedItems - 1) Then
                e.NewValue = CheckState.Unchecked
            End If
        End Sub
        ''' <summary>
        ''' Get checked items
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub GetCheckedButton_Click(sender As Object, e As EventArgs) _
            Handles GetCheckedButton.Click
    
            If CheckedListBox1.Items.Count > 0 Then
                Dim nameList As New List(Of Person)
                For index As Integer = 0 To CheckedListBox1.Items.Count - 1
                    If CheckedListBox1.GetItemChecked(index) Then
                        nameList.Add(CType(CheckedListBox1.Items(index), Person))
                    End If
                Next
    
                For Each person As Person In nameList
                    Console.WriteLine(person)
                Next
            Else
                MessageBox.Show("No people are selected")
            End If
        End Sub
    End Class
    Public Class Person
        Public Property Id() As Integer
        Public Property Name() As String
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 22, 2019 4:15 PM
  • Thanks mate i understand but i have 2 questions:

    Dim hex = "00FFFFFF3F00010000"
    
    Dim bytes = Enumerable.Range(0, hex.Length \ 2 - 1).Select(Function(n) Convert.ToByte(hex.Substring(n * 2, 2), 16)).ToArray()
     
    Using file As New FileStream(...)
        file.Position = &H39A
        file.Write(bytes, 0, bytes.Length)
    End Using

    1) Im gonna use this to write the variables on my first post ive mentioned dim a1, dim a2... In your code will be something like person.id1.name.

    Where i insert this on code? 

    2) Since i will check 10 checkboxs each 10 checkboxs i need to specify fs.Position and will be different from each other. How i do it?

    Hope you understand. 


    • Edited by extream87 Sunday, September 22, 2019 4:31 PM
    Sunday, September 22, 2019 4:30 PM
  • Thanks mate i understand but i have 2 questions:

    Dim hex = "00FFFFFF3F00010000"
    
    Dim bytes = Enumerable.Range(0, hex.Length \ 2 - 1).Select(Function(n) Convert.ToByte(hex.Substring(n * 2, 2), 16)).ToArray()
     
    Using file As New FileStream(...)
        file.Position = &H39A
        file.Write(bytes, 0, bytes.Length)
    End Using

    1) Im gonna use this to write the variables on my first post ive mentioned dim a1, dim a2... In your code will be something like person.id1.name.

    Where i insert this on code? 

    2) Since i will check 10 checkboxs each 10 checkboxs i need to specify fs.Position and will be different from each other. How i do it?

    Hope you understand. 


    I gave you a pattern as a suggestion then I see you edited the initial post and then in this post added more information. Sorry, if I saw this I would had replied to the added requirements but have no time for this as I'm in the middle of writing a complex article for Microsoft TechNet.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 22, 2019 7:17 PM
  • Ok no problem mate i understand. Thanks for all your help. 
    Sunday, September 22, 2019 8:42 PM
  • Hi

    Give this a try and see if it is anywhere close to what you are asking for.

    This is a stand alone example and if you wanttotry itout, start a new project and put a single Button1 at bottom of Form1, anchored left and bottom. Then copy/replace all Form1 code with this code.

    ' Form1 with a Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim testfilename As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFile.dat")
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim adds As New List(Of Integer)({&H1, &H3, &H5, &H7, &H9, &HB, &HD, &HF, &H11, &H13, &H15, &H17, &H19, &H11B, &H1D, &H1F, &H21, &H23, &H25, &H27, &H29, &H2B, &H2D, &H2F, &H31, &H33, &H35, &H37, &H39, &H3B})
    
    		Dim x As Integer = 8
    		Dim y As Integer = 10
    		For i As Integer = 1 To 30
    			Dim cb As New CheckBox
    			With cb
    				.Name = "MyCB" & i.ToString
    				.Location = New Point(x, y)
    				.AutoSize = True
    				.Text = i.ToString
    				.Tag = adds(i - 1)
    				y += 24
    				Dim w As Integer = .Width + 4
    				If i Mod 10 = 0 Then
    					y = 10
    					x += w
    				End If
    			End With
    			AddHandler cb.CheckedChanged, AddressOf CheckedChanged
    			Controls.Add(cb)
    		Next
    	End Sub
    	Sub WriteValues()
    		Dim hex As String = "00FFFFFF3F00010000"
    
    		Dim bytes() As Byte = Enumerable.Range(0, hex.Length \ 2 - 1).Select(Function(n) Convert.ToByte(hex.Substring(n * 2, 2), 16)).ToArray()
    
    		Using file As New IO.FileStream(testfilename, IO.FileMode.OpenOrCreate)
    			For Each c As Control In Controls
    				If c.GetType = GetType(CheckBox) Then
    					If c.Name.StartsWith("MyCB") Then
    						If CType(c, CheckBox).Checked Then
    							file.Position = CInt(c.Tag)
    							file.Write(bytes, 0, bytes.Length)
    						End If
    					End If
    				End If
    			Next
    		End Using
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		WriteValues()
    	End Sub
    	Private Sub CheckedChanged(sender As Object, e As EventArgs)
    		Dim cb As CheckBox = CType(sender, CheckBox)
    		Dim chk As Integer = 0
    		For Each c As Control In Controls
    			If c.GetType = GetType(CheckBox) Then
    				If c.Name.StartsWith("MyCB") Then
    					If CType(c, CheckBox).Checked Then
    						chk += 1
    					End If
    				End If
    			End If
    		Next
    		If chk > 10 Then
    			cb.Checked = False
    		End If
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Friday, September 27, 2019 4:21 PM
    Sunday, September 22, 2019 8:45 PM
  • leshay thank you so much.
    I've changed a little your code and works like a charm.
    Friday, September 27, 2019 4:21 PM
  • leshay thank you so much.
    I've changed a little your code and works like a charm.

    Hi

    You are welcome - good to see you have an answer.


    Regards Les, Livingston, Scotland

    Friday, September 27, 2019 5:07 PM