Results 1 to 7 of 7

Thread: [SOVED! CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox etc?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation [SOVED! CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox etc?

    Hello!
    i tell your help for a task for several checkbox ( 15 in my situation). i suppose it could have a shortcut to gather all checkboxes in a line.
    for example:
    private checkbox(1,2,3,4,5,6,7 until 15).checkstatechanged ............etc

    and add this code:
    Code:
    dim n as integer
    
    if checkbox(n).checkstate = checkbox.checked then
    
    listbox1.items(n-1) = "true"
    
    end if
    sorry, for details i am a beginner in coding! if it is possible thank you in advance for your precious help!
    Last edited by danzey; Jun 26th, 2021 at 01:49 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    Just create a Sub that handles the CheckChanged event.

    Code:
       Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    
            MessageBox.Show(DirectCast(sender, CheckBox).Name)
        End Sub

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

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    Quote Originally Posted by wes4dbt View Post
    Just create a Sub that handles the CheckChanged event.

    Code:
       Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    
            MessageBox.Show(DirectCast(sender, CheckBox).Name)
        End Sub
    Note that the Handles clause here has multiple events listed. You can add as many as you like and they don't have to be on the same type of control or even the same event, as long as the signatures are compatible. You can get the IDE to generate an event handler like this for you automatically. In the designer, just select all the CheckBoxes by Ctrl+clicking or click+dragging, open the Properties window, click the Events window and then double-click the CheckedChanged event. Hey presto, you have your single method to handle multiple events. If you add more controls later, you can select the existing event handler from the drop-down list in the Properties window or you can edit the Handles clause manually.

    Note that you should probably change the name of the method, because it will be named after just one of the controls. I would likely change it to something like CheckBoxes_CheckChanged but there may be something else even more appropriate.
    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

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

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    You should almost certainly not being using the CheckState property in your code. You ONLY use that property if you have set ThreeState to True and you care about the Indeterminate state. Otherwise, there are only two states so you use the Checked property, which is True when checked and False when unchecked.
    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
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    Quote Originally Posted by wes4dbt View Post
    Just create a Sub that handles the CheckChanged event.

    Code:
       Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    
            MessageBox.Show(DirectCast(sender, CheckBox).Name)
        End Sub

    thank you so much for your answer!!!!!

    by the way, is this code right?
    Code:
    dim n as integer
    
    if checkbox(n).checkstate = checkbox.checked then
    
    listbox1.items(n-1) = "true"
    
    end if
    Last edited by danzey; Jun 25th, 2021 at 06:53 AM.

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

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    Quote Originally Posted by danzey View Post
    by the way, is this code right?
    Code:
    dim n as integer
    
    if checkbox(n).checkstate = checkbox.checked then
    
    listbox1.items(n-1) = "true"
    
    end if
    All code is right because all code does exactly what it does. If you would like us to tell you whether that code will do what you want it to do, you'd probably need to tell us what you actually want it to do. That said, why do you need to ask us at all? Why can't you run it and see what it does? If it does as you want then it's OK and if it doesn;t then you have something to look into and maybe ask us about. You never need to ask us what you can find out for yourself.

    That said, how can that code be of use when you never assign a value to n? Also, you have simply ignored what I said about CheckState.
    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
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et

    Quote Originally Posted by jmcilhinney View Post
    All code is right because all code does exactly what it does. If you would like us to tell you whether that code will do what you want it to do, you'd probably need to tell us what you actually want it to do. That said, why do you need to ask us at all? Why can't you run it and see what it does? If it does as you want then it's OK and if it doesn;t then you have something to look into and maybe ask us about. You never need to ask us what you can find out for yourself.

    That said, how can that code be of use when you never assign a value to n? Also, you have simply ignored what I said about CheckState.
    sorry, i didn't see and read your message . thank you for alll! i understood how i must code my work!

Tags for this Thread

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