Results 1 to 13 of 13

Thread: How to simplify and compress code?

  1. #1

    Thread Starter
    New Member TheTveit's Avatar
    Join Date
    May 2021
    Posts
    5

    How to simplify and compress code?

    I have been using Visual Basic for about two years now and I would rate my skills at below average but good enough to do what i want.
    In the programs I make, I have a tendency to e.g. have multiple buttons that do mostly the same thing but with a difference of

    "when button1 is clicked then label1.text = 1" and "when button2 is clicked then label2.text = 2".

    But I was wondering if there's a way to simplify this in a way of just having "when buttonX is clicked then labelX.text = X" and switching the X with the value thats needed in code?

    It might sound silly but i have no idea how i would do this and I also dont know how to formulate this idea so i can search it up on the web.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,428

    Re: How to simplify and compress code?

    In .NET it's possible, but it may not always be the right choice.
    What you're looking to do is combine Event Handlers. If you look at your current code, you'll notice two things:
    First, on the end of it, there's what's known as a Handles clause. That tells you what events that method handles. They're usually in the form of object.event. Sometimes it reflects the name of the method that follows the object_event construct, but that's really a hold over. The name of the event handler itself doesn't matter as long as it is a valid method name. The nice thing about this Handles clause is that you can add the events of other controls to it, just separate them by commas.
    Code:
    .. Handles Command1.Click, Command2.Click, Command3.click, ...
    Now your event handler can handle the click events of multiple buttons. Easy peasy.
    Now comes the obvious question of "Well, how do I know which button was clicked?" Look to the left... see that first parameter to the method? Sender? Yeah... that's how. .NET is kind enough to send you the object that is raising the event for you. So now, you can either cast it as a Command to get all the properties of the Command Button (recommended), or you can simply use it as is, and just use the basic properties. But either way, you can then check the .Name property and see which one was clicked, and then use that in your If...Then...Else statements to then set what ever it is you need to set.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How to simplify and compress code?

    As tg says, you can handle multiple events with a single method. If you then proceed to use a Select Case or If statements to do different things for each event though, it's pointless. You need to devise a way to actually make the code more succinct. Using the example you mentioned yourself, you might start by setting the Tag properties of Button1, Button2 and Button3 to 1, 2 and 3 respectively and then doing something like this:
    vb.net Code:
    1. Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    2.     Dim buttons = {Button1, Button2, Button3}
    3.     Dim labels = {Label1, Label2, Label3}
    4.     Dim btn = DirectCast(sender, Button)
    5.     Dim lbl = labels(Array.IndexOf(buttons, btn))
    6.  
    7.     lbl.Text = CStr(btn.Tag)
    8. End Sub
    The point of using a single event handler is that you do the same thing regardless of which object raises the event.

    It's also worth noting that you can get the designer to generate a common event handler for you. Start by selecting multiple controls in the designer, open the Properties window, click the Events button and then double-click the desired event. Job done. You might want to change the name of the generated method to something less specific though. Note that you can also select an existing method in the drop-down for an event in the properties window and add an event to its Handles clause that way.

  4. #4

    Thread Starter
    New Member TheTveit's Avatar
    Join Date
    May 2021
    Posts
    5

    Re: How to simplify and compress code?

    I'm new here so i'm not sure if i'm replying to both of you here.
    One section of the code is:

    Code:
    Private Sub chb_en_data_1_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_1.CheckedChanged
            If chb_en_data_1.Checked = True Then
                tb_data_e_1.Enabled = True
                tb_data_n_1.Enabled = True
                chb_en_data_2.Enabled = True
            ElseIf chb_en_data_1.Checked = False Then
                Call F1()
            End If
        End Sub
    I have 20 checkboxes where the same code is written, except that the numbers are corresponding to the number of the checkbox.
    This is for a program that changes specified values in text that the user writes in to another form. The checkboxes are there to verify how much the user wants to change.
    The Call functions are there to disable and remove the input from the textboxes if the user uncheck the checkboxe(s).

    Code:
    Public Sub F1()
            tb_data_e_1.Text = ""
            tb_data_n_1.Text = ""
            'tb_data_e_1.Enabled = False
            'tb_data_n_1.Enabled = False
            Call F2()
        End Sub
        Public Sub F2()
            chb_en_data_2.Enabled = False
            chb_en_data_2.Checked = False
            tb_data_e_2.Text = ""
            tb_data_n_2.Text = ""
            tb_data_e_2.Enabled = False
            tb_data_n_2.Enabled = False
            Call F3()
        End Sub
    The "chb_en_data_X.Enabled = False and chb_en_data_X.Checked = False is removed from F1 since input one should always be enabled. The reason function 1 (F1) calls function 2 is that if e.g. checkbox 1 is uncheck, checkbox2 - 20 should also be unchecked and disabled to visualize what the user can do.

    Attachment 181416

  5. #5

    Thread Starter
    New Member TheTveit's Avatar
    Join Date
    May 2021
    Posts
    5

    Re: How to simplify and compress code?

    Full code if you want to take a look at it to understand my poorly worded question.

    Form2:

    Code:
    Public Class frm_data_set
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            chb_en_data_2.Enabled = False
            chb_en_data_3.Enabled = False
            chb_en_data_4.Enabled = False
            chb_en_data_5.Enabled = False
            chb_en_data_6.Enabled = False
            chb_en_data_7.Enabled = False
            chb_en_data_8.Enabled = False
            chb_en_data_9.Enabled = False
            chb_en_data_10.Enabled = False
            chb_en_data_11.Enabled = False
            chb_en_data_12.Enabled = False
            chb_en_data_13.Enabled = False
            chb_en_data_14.Enabled = False
            chb_en_data_15.Enabled = False
            chb_en_data_16.Enabled = False
            chb_en_data_17.Enabled = False
            chb_en_data_18.Enabled = False
            chb_en_data_19.Enabled = False
            chb_en_data_20.Enabled = False
    
            tb_data_e_2.Enabled = False
            tb_data_e_3.Enabled = False
            tb_data_e_4.Enabled = False
            tb_data_e_5.Enabled = False
            tb_data_e_6.Enabled = False
            tb_data_e_7.Enabled = False
            tb_data_e_8.Enabled = False
            tb_data_e_9.Enabled = False
            tb_data_e_10.Enabled = False
            tb_data_e_11.Enabled = False
            tb_data_e_12.Enabled = False
            tb_data_e_13.Enabled = False
            tb_data_e_14.Enabled = False
            tb_data_e_15.Enabled = False
            tb_data_e_16.Enabled = False
            tb_data_e_17.Enabled = False
            tb_data_e_18.Enabled = False
            tb_data_e_19.Enabled = False
            tb_data_e_20.Enabled = False
    
            tb_data_n_2.Enabled = False
            tb_data_n_3.Enabled = False
            tb_data_n_4.Enabled = False
            tb_data_n_5.Enabled = False
            tb_data_n_6.Enabled = False
            tb_data_n_7.Enabled = False
            tb_data_n_8.Enabled = False
            tb_data_n_9.Enabled = False
            tb_data_n_10.Enabled = False
            tb_data_n_11.Enabled = False
            tb_data_n_12.Enabled = False
            tb_data_n_13.Enabled = False
            tb_data_n_14.Enabled = False
            tb_data_n_15.Enabled = False
            tb_data_n_16.Enabled = False
            tb_data_n_17.Enabled = False
            tb_data_n_18.Enabled = False
            tb_data_n_19.Enabled = False
            tb_data_n_20.Enabled = False
        End Sub
    
        Private Sub CheckBox13_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_14.CheckedChanged
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_data_clr.Click
            chb_en_data_1.Checked = False
            chb_en_data_2.Checked = False
            chb_en_data_3.Checked = False
            chb_en_data_4.Checked = False
            chb_en_data_5.Checked = False
            chb_en_data_6.Checked = False
            chb_en_data_7.Checked = False
            chb_en_data_8.Checked = False
            chb_en_data_9.Checked = False
            chb_en_data_10.Checked = False
            chb_en_data_11.Checked = False
            chb_en_data_12.Checked = False
            chb_en_data_13.Checked = False
            chb_en_data_14.Checked = False
            chb_en_data_15.Checked = False
            chb_en_data_16.Checked = False
            chb_en_data_17.Checked = False
            chb_en_data_18.Checked = False
            chb_en_data_19.Checked = False
            chb_en_data_20.Checked = False
        End Sub
        Private Sub btn_data_ena_Click(sender As Object, e As EventArgs) Handles btn_data_ena.Click
            chb_en_data_1.Checked = True
            chb_en_data_2.Checked = True
            chb_en_data_3.Checked = True
            chb_en_data_4.Checked = True
            chb_en_data_5.Checked = True
            chb_en_data_6.Checked = True
            chb_en_data_7.Checked = True
            chb_en_data_8.Checked = True
            chb_en_data_9.Checked = True
            chb_en_data_10.Checked = True
            chb_en_data_11.Checked = True
            chb_en_data_12.Checked = True
            chb_en_data_13.Checked = True
            chb_en_data_14.Checked = True
            chb_en_data_15.Checked = True
            chb_en_data_16.Checked = True
            chb_en_data_17.Checked = True
            chb_en_data_18.Checked = True
            chb_en_data_19.Checked = True
            chb_en_data_20.Checked = True
    
            chb_en_data_1.Enabled = True
            chb_en_data_2.Enabled = True
            chb_en_data_3.Enabled = True
            chb_en_data_4.Enabled = True
            chb_en_data_5.Enabled = True
            chb_en_data_6.Enabled = True
            chb_en_data_7.Enabled = True
            chb_en_data_8.Enabled = True
            chb_en_data_9.Enabled = True
            chb_en_data_10.Enabled = True
            chb_en_data_11.Enabled = True
            chb_en_data_12.Enabled = True
            chb_en_data_13.Enabled = True
            chb_en_data_14.Enabled = True
            chb_en_data_15.Enabled = True
            chb_en_data_16.Enabled = True
            chb_en_data_17.Enabled = True
            chb_en_data_18.Enabled = True
            chb_en_data_19.Enabled = True
            chb_en_data_20.Enabled = True
        End Sub
        'knapper skru på 1 - 15, med call
        Private Sub chb_en_data_1_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_1.CheckedChanged
            If chb_en_data_1.Checked = True Then
                tb_data_e_1.Enabled = True
                tb_data_n_1.Enabled = True
                chb_en_data_2.Enabled = True
            ElseIf chb_en_data_1.Checked = False Then
                Call F1()
            End If
        End Sub
    
        Private Sub chb_en_data_2_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_2.CheckedChanged
            If chb_en_data_2.Checked = True Then
                tb_data_e_2.Enabled = True
                tb_data_n_2.Enabled = True
                chb_en_data_3.Enabled = True
            ElseIf chb_en_data_2.Checked = False Then
                Call F2()
            End If
        End Sub
    
        Private Sub chb_en_data_3_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_3.CheckedChanged
            If chb_en_data_3.Checked = True Then
                tb_data_e_3.Enabled = True
                tb_data_n_3.Enabled = True
                chb_en_data_4.Enabled = True
            ElseIf chb_en_data_3.Checked = False Then
                Call F3()
            End If
        End Sub
    
        Private Sub chb_en_data_4_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_4.CheckedChanged
            If chb_en_data_4.Checked = True Then
                tb_data_e_4.Enabled = True
                tb_data_n_4.Enabled = True
                chb_en_data_5.Enabled = True
            ElseIf chb_en_data_4.Checked = False Then
                Call F4()
            End If
        End Sub
    
        Private Sub chb_en_data_5_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_5.CheckedChanged
            If chb_en_data_5.Checked = True Then
                tb_data_e_5.Enabled = True
                tb_data_n_5.Enabled = True
                chb_en_data_6.Enabled = True
            ElseIf chb_en_data_5.Checked = False Then
                Call F5()
            End If
        End Sub
    
        Private Sub chb_en_data_6_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_6.CheckedChanged
            If chb_en_data_6.Checked = True Then
                tb_data_e_6.Enabled = True
                tb_data_n_6.Enabled = True
                chb_en_data_7.Enabled = True
            ElseIf chb_en_data_6.Checked = False Then
                Call F6()
            End If
        End Sub
    
        Private Sub chb_en_data_7_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_7.CheckedChanged
            If chb_en_data_7.Checked = True Then
                tb_data_e_7.Enabled = True
                tb_data_n_7.Enabled = True
                chb_en_data_8.Enabled = True
            ElseIf chb_en_data_7.Checked = False Then
                Call F7()
            End If
        End Sub
    
        Private Sub chb_en_data_8_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_8.CheckedChanged
            If chb_en_data_8.Checked = True Then
                tb_data_e_8.Enabled = True
                tb_data_n_8.Enabled = True
                chb_en_data_9.Enabled = True
            ElseIf chb_en_data_8.Checked = False Then
                Call F8()
            End If
        End Sub
    
        Private Sub chb_en_data_9_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_9.CheckedChanged
            If chb_en_data_9.Checked = True Then
                tb_data_e_9.Enabled = True
                tb_data_n_9.Enabled = True
                chb_en_data_10.Enabled = True
            ElseIf chb_en_data_9.Checked = False Then
                Call F9()
            End If
        End Sub
    
        Private Sub chb_en_data_10_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_10.CheckedChanged
            If chb_en_data_10.Checked = True Then
                tb_data_e_10.Enabled = True
                tb_data_n_10.Enabled = True
                chb_en_data_11.Enabled = True
            ElseIf chb_en_data_10.Checked = False Then
                Call F10()
            End If
        End Sub
    
        Private Sub chb_en_data_11_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_11.CheckedChanged
            If chb_en_data_11.Checked = True Then
                tb_data_e_11.Enabled = True
                tb_data_n_11.Enabled = True
                chb_en_data_12.Enabled = True
            ElseIf chb_en_data_11.Checked = False Then
                Call F11()
            End If
        End Sub
    
        Private Sub chb_en_data_12_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_12.CheckedChanged
            If chb_en_data_12.Checked = True Then
                tb_data_e_12.Enabled = True
                tb_data_n_12.Enabled = True
                chb_en_data_13.Enabled = True
            ElseIf chb_en_data_12.Checked = False Then
                Call F12()
            End If
        End Sub
    
        Private Sub chb_en_data_13_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_13.CheckedChanged
            If chb_en_data_13.Checked = True Then
                tb_data_e_13.Enabled = True
                tb_data_n_13.Enabled = True
                chb_en_data_14.Enabled = True
            ElseIf chb_en_data_13.Checked = False Then
                Call F13()
            End If
        End Sub
    
        Private Sub chb_en_data_14_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_14.CheckedChanged
            If chb_en_data_14.Checked = True Then
                tb_data_e_14.Enabled = True
                tb_data_n_14.Enabled = True
                chb_en_data_15.Enabled = True
            ElseIf chb_en_data_14.Checked = False Then
                Call F14()
            End If
        End Sub
    
        Private Sub chb_en_data_15_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_15.CheckedChanged
            If chb_en_data_15.Checked = True Then
                tb_data_e_15.Enabled = True
                tb_data_n_15.Enabled = True
            ElseIf chb_en_data_15.Checked = False Then
                Call F15()
            End If
        End Sub
        Private Sub chb_en_data_16_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_16.CheckedChanged
            If chb_en_data_16.Checked = True Then
                tb_data_e_16.Enabled = True
                tb_data_n_16.Enabled = True
            ElseIf chb_en_data_16.Checked = False Then
                Call F16()
            End If
        End Sub
        Private Sub chb_en_data_17_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_17.CheckedChanged
            If chb_en_data_17.Checked = True Then
                tb_data_e_17.Enabled = True
                tb_data_n_17.Enabled = True
            ElseIf chb_en_data_17.Checked = False Then
                Call F17()
            End If
        End Sub
        Private Sub chb_en_data_18_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_18.CheckedChanged
            If chb_en_data_18.Checked = True Then
                tb_data_e_18.Enabled = True
                tb_data_n_18.Enabled = True
            ElseIf chb_en_data_18.Checked = False Then
                Call F18()
            End If
        End Sub
        Private Sub chb_en_data_19_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_19.CheckedChanged
            If chb_en_data_19.Checked = True Then
                tb_data_e_19.Enabled = True
                tb_data_n_19.Enabled = True
            ElseIf chb_en_data_19.Checked = False Then
                Call F19()
            End If
        End Sub
        Private Sub chb_en_data_20_CheckedChanged(sender As Object, e As EventArgs) Handles chb_en_data_20.CheckedChanged
            If chb_en_data_20.Checked = True Then
                tb_data_e_20.Enabled = True
                tb_data_n_20.Enabled = True
            ElseIf chb_en_data_20.Checked = False Then
                Call F20()
            End If
        End Sub
        'call funksjoner skru av knapper 1 - 15
        Public Sub F1()
            tb_data_e_1.Text = ""
            tb_data_n_1.Text = ""
            'tb_data_e_1.Enabled = False
            'tb_data_n_1.Enabled = False
            Call F2()
        End Sub
        Public Sub F2()
            chb_en_data_2.Enabled = False
            chb_en_data_2.Checked = False
            tb_data_e_2.Text = ""
            tb_data_n_2.Text = ""
            tb_data_e_2.Enabled = False
            tb_data_n_2.Enabled = False
            Call F3()
        End Sub
        Public Sub F3()
            chb_en_data_3.Enabled = False
            chb_en_data_3.Checked = False
            tb_data_e_3.Text = ""
            tb_data_n_3.Text = ""
            tb_data_e_3.Enabled = False
            tb_data_n_3.Enabled = False
            Call F4()
        End Sub
        Public Sub F4()
            chb_en_data_4.Enabled = False
            chb_en_data_4.Checked = False
            tb_data_e_4.Text = ""
            tb_data_n_4.Text = ""
            tb_data_e_4.Enabled = False
            tb_data_n_4.Enabled = False
            Call F5()
        End Sub
        Public Sub F5()
            chb_en_data_5.Enabled = False
            chb_en_data_5.Checked = False
            tb_data_e_5.Text = ""
            tb_data_n_5.Text = ""
            tb_data_e_5.Enabled = False
            tb_data_n_5.Enabled = False
            Call F6()
        End Sub
        Public Sub F6()
            chb_en_data_6.Enabled = False
            chb_en_data_6.Checked = False
            tb_data_e_6.Text = ""
            tb_data_n_6.Text = ""
            tb_data_e_6.Enabled = False
            tb_data_n_6.Enabled = False
            Call F7()
        End Sub
        Public Sub F7()
            chb_en_data_7.Enabled = False
            chb_en_data_7.Checked = False
            tb_data_e_7.Text = ""
            tb_data_n_7.Text = ""
            tb_data_e_7.Enabled = False
            tb_data_n_7.Enabled = False
            Call F8()
        End Sub
        Public Sub F8()
            chb_en_data_8.Enabled = False
            chb_en_data_8.Checked = False
            tb_data_e_8.Text = ""
            tb_data_n_8.Text = ""
            tb_data_e_8.Enabled = False
            tb_data_n_8.Enabled = False
            Call F9()
        End Sub
        Public Sub F9()
            chb_en_data_9.Enabled = False
            chb_en_data_9.Checked = False
            tb_data_e_9.Text = ""
            tb_data_n_9.Text = ""
            tb_data_e_9.Enabled = False
            tb_data_n_9.Enabled = False
            Call F10()
        End Sub
        Public Sub F10()
            chb_en_data_10.Enabled = False
            chb_en_data_10.Checked = False
            tb_data_e_10.Text = ""
            tb_data_n_10.Text = ""
            tb_data_e_10.Enabled = False
            tb_data_n_10.Enabled = False
            Call F11()
        End Sub
        Public Sub F11()
            chb_en_data_11.Enabled = False
            chb_en_data_11.Checked = False
            tb_data_e_11.Text = ""
            tb_data_n_11.Text = ""
            tb_data_e_11.Enabled = False
            tb_data_n_11.Enabled = False
            Call F12()
        End Sub
        Public Sub F12()
            chb_en_data_12.Enabled = False
            chb_en_data_12.Checked = False
            tb_data_e_12.Text = ""
            tb_data_n_12.Text = ""
            tb_data_e_12.Enabled = False
            tb_data_n_12.Enabled = False
            Call F13()
        End Sub
        Public Sub F13()
            chb_en_data_13.Enabled = False
            chb_en_data_13.Checked = False
            tb_data_e_13.Text = ""
            tb_data_n_13.Text = ""
            tb_data_e_13.Enabled = False
            tb_data_n_13.Enabled = False
            Call F14()
        End Sub
        Public Sub F14()
            chb_en_data_14.Enabled = False
            chb_en_data_14.Checked = False
            tb_data_e_14.Text = ""
            tb_data_n_14.Text = ""
            tb_data_e_14.Enabled = False
            tb_data_n_14.Enabled = False
            Call F15()
        End Sub
        Public Sub F15()
            chb_en_data_15.Enabled = False
            chb_en_data_15.Checked = False
            tb_data_e_15.Text = ""
            tb_data_n_15.Text = ""
            tb_data_e_15.Enabled = False
            tb_data_n_15.Enabled = False
            Call F16()
        End Sub
        Public Sub F16()
            chb_en_data_16.Enabled = False
            chb_en_data_16.Checked = False
            tb_data_e_16.Text = ""
            tb_data_n_16.Text = ""
            tb_data_e_16.Enabled = False
            tb_data_n_16.Enabled = False
            Call F17()
        End Sub
        Public Sub F17()
            chb_en_data_17.Enabled = False
            chb_en_data_17.Checked = False
            tb_data_e_17.Text = ""
            tb_data_n_17.Text = ""
            tb_data_e_17.Enabled = False
            tb_data_n_17.Enabled = False
            Call F18()
        End Sub
        Public Sub F18()
            chb_en_data_18.Enabled = False
            chb_en_data_18.Checked = False
            tb_data_e_18.Text = ""
            tb_data_n_18.Text = ""
            tb_data_e_18.Enabled = False
            tb_data_n_18.Enabled = False
            Call F19()
        End Sub
        Public Sub F19()
            chb_en_data_19.Enabled = False
            chb_en_data_19.Checked = False
            tb_data_e_19.Text = ""
            tb_data_n_19.Text = ""
            tb_data_e_19.Enabled = False
            tb_data_n_19.Enabled = False
            Call F20()
        End Sub
        Public Sub F20()
            chb_en_data_20.Enabled = False
            chb_en_data_20.Checked = False
            tb_data_e_20.Text = ""
            tb_data_n_20.Text = ""
            tb_data_e_20.Enabled = False
            tb_data_n_20.Enabled = False
        End Sub
    
        'lagre
        Public Sub FS20()
            EXdata20 = tb_data_e_20.Text
            NEdata20 = tb_data_n_20.Text
            Call FS19()
        End Sub
        Public Sub FS19()
            EXdata19 = tb_data_e_19.Text
            NEdata19 = tb_data_n_19.Text
            Call FS18()
        End Sub
        Public Sub FS18()
            EXdata18 = tb_data_e_18.Text
            NEdata18 = tb_data_n_18.Text
            Call FS17()
        End Sub
        Public Sub FS17()
            EXdata17 = tb_data_e_17.Text
            NEdata17 = tb_data_n_17.Text
            Call FS16()
        End Sub
        Public Sub FS16()
            EXdata16 = tb_data_e_16.Text
            NEdata16 = tb_data_n_16.Text
            Call FS15()
        End Sub
        Public Sub FS15()
            EXdata15 = tb_data_e_15.Text
            NEdata15 = tb_data_n_15.Text
            Call FS14()
        End Sub
        Public Sub FS14()
            EXdata14 = tb_data_e_14.Text
            NEdata14 = tb_data_n_14.Text
            Call FS13()
        End Sub
        Public Sub FS13()
            EXdata13 = tb_data_e_13.Text
            NEdata13 = tb_data_n_13.Text
            Call FS12()
        End Sub
        Public Sub FS12()
            EXdata12 = tb_data_e_12.Text
            NEdata12 = tb_data_n_12.Text
            Call FS11()
        End Sub
        Public Sub FS11()
            EXdata11 = tb_data_e_11.Text
            NEdata11 = tb_data_n_11.Text
            Call FS10()
        End Sub
        Public Sub FS10()
            EXdata10 = tb_data_e_10.Text
            NEdata10 = tb_data_n_10.Text
            Call FS9()
        End Sub
        Public Sub FS9()
            EXdata9 = tb_data_e_9.Text
            NEdata9 = tb_data_n_9.Text
            Call FS8()
        End Sub
        Public Sub FS8()
            EXdata8 = tb_data_e_8.Text
            NEdata8 = tb_data_n_8.Text
            Call FS7()
        End Sub
        Public Sub FS7()
            EXdata7 = tb_data_e_7.Text
            NEdata7 = tb_data_n_7.Text
            Call FS6()
        End Sub
        Public Sub FS6()
            EXdata6 = tb_data_e_6.Text
            NEdata6 = tb_data_n_6.Text
            Call FS5()
        End Sub
        Public Sub FS5()
            EXdata5 = tb_data_e_5.Text
            NEdata5 = tb_data_n_5.Text
            Call FS4()
        End Sub
        Public Sub FS4()
            EXdata4 = tb_data_e_4.Text
            NEdata4 = tb_data_n_4.Text
            Call FS3()
        End Sub
        Public Sub FS3()
            EXdata3 = tb_data_e_3.Text
            NEdata3 = tb_data_n_3.Text
            Call FS2()
        End Sub
        Public Sub FS2()
            EXdata2 = tb_data_e_2.Text
            NEdata2 = tb_data_n_2.Text
            Call FS1()
        End Sub
        Public Sub FS1()
            EXdata1 = tb_data_e_1.Text
            NEdata1 = tb_data_n_1.Text
        End Sub
    
        Private Sub btn_data_sar_Click(sender As Object, e As EventArgs) Handles btn_data_sar.Click
            If chb_en_data_20.Checked = True Then
                Call FS20()
            ElseIf chb_en_data_19.Checked = True And chb_en_data_20.Checked = False Then
                Call FS19()
            ElseIf chb_en_data_18.Checked = True And chb_en_data_19.Checked = False Then
                Call FS18()
            ElseIf chb_en_data_17.Checked = True And chb_en_data_18.Checked = False Then
                Call FS17()
            ElseIf chb_en_data_16.Checked = True And chb_en_data_17.Checked = False Then
                Call FS16()
            ElseIf chb_en_data_15.Checked = True And chb_en_data_16.Checked = False Then
                Call FS15()
            ElseIf chb_en_data_14.Checked = True And chb_en_data_15.Checked = False Then
                Call FS14()
            ElseIf chb_en_data_13.Checked = True And chb_en_data_14.Checked = False Then
                Call FS13()
            ElseIf chb_en_data_12.Checked = True And chb_en_data_13.Checked = False Then
                Call FS12()
            ElseIf chb_en_data_11.Checked = True And chb_en_data_12.Checked = False Then
                Call FS11()
            ElseIf chb_en_data_10.Checked = True And chb_en_data_11.Checked = False Then
                Call FS10()
            ElseIf chb_en_data_9.Checked = True And chb_en_data_10.Checked = False Then
                Call FS9()
            ElseIf chb_en_data_8.Checked = True And chb_en_data_9.Checked = False Then
                Call FS8()
            ElseIf chb_en_data_7.Checked = True And chb_en_data_8.Checked = False Then
                Call FS7()
            ElseIf chb_en_data_6.Checked = True And chb_en_data_7.Checked = False Then
                Call FS6()
            ElseIf chb_en_data_5.Checked = True And chb_en_data_6.Checked = False Then
                Call FS5()
            ElseIf chb_en_data_4.Checked = True And chb_en_data_5.Checked = False Then
                Call FS4()
            ElseIf chb_en_data_3.Checked = True And chb_en_data_4.Checked = False Then
                Call FS3()
            ElseIf chb_en_data_2.Checked = True And chb_en_data_3.Checked = False Then
                Call FS2()
            ElseIf chb_en_data_1.Checked = True And chb_en_data_2.Checked = False Then
                Call FS1()
            End If
        End Sub
    End Class
    Module:

    Code:
    Module Module1
        Public mode As String
    
        Public inndata As String
    
        Public strekkode As String
    
        Public strekkodeny As String
    
        Public anfør As String
    
    
        'eksisterende data
        Public EXdata1 As String
        Public EXdata2 As String
        Public EXdata3 As String
        Public EXdata4 As String
        Public EXdata5 As String
        Public EXdata6 As String
        Public EXdata7 As String
        Public EXdata8 As String
        Public EXdata9 As String
        Public EXdata10 As String
        Public EXdata11 As String
        Public EXdata12 As String
        Public EXdata13 As String
        Public EXdata14 As String
        Public EXdata15 As String
        Public EXdata16 As String
        Public EXdata17 As String
        Public EXdata18 As String
        Public EXdata19 As String
        Public EXdata20 As String
        'ny data
        Public NEdata1 As String
        Public NEdata2 As String
        Public NEdata3 As String
        Public NEdata4 As String
        Public NEdata5 As String
        Public NEdata6 As String
        Public NEdata7 As String
        Public NEdata8 As String
        Public NEdata9 As String
        Public NEdata10 As String
        Public NEdata11 As String
        Public NEdata12 As String
        Public NEdata13 As String
        Public NEdata14 As String
        Public NEdata15 As String
        Public NEdata16 As String
        Public NEdata17 As String
        Public NEdata18 As String
        Public NEdata19 As String
        Public NEdata20 As String
    End Module
    I did not include form 1 science that only handles the input text written in form 1 and the values specified in form 2
    Last edited by TheTveit; May 12th, 2021 at 02:17 AM.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: How to simplify and compress code?

    You could loop through the controls on your form and check whether they are of the desired type and perform the appropriate action on them if so. The page explains how to do it:
    https://stackoverflow.com/questions/...rols-in-vb-net

    Also, you usually don't need to add " = True" at the end of an If/Then expression. You could also replace " = False" at the end of an expression with "Not" at the beginning.

    Examples:
    1. "If variable = True Then" becomes "If Not Variable".
    2. "If variable = False Then" becomes "If Not variable Then".

    Also look up short-circuiting: https://www.google.com/search?q=shor...client=gws-wiz.

    Basically it comes down to replacing all instances of "And"/"Or" with "AndAlso"/"OrElse" in conditional expressions.

    Also looking at your code you may want to look up on arrays: https://www.tutorialspoint.com/vb.net/vb.net_arrays.htm. Or better yet, lists: https://www.dotnetperls.com/list-vbnet.

    You might also want to give your controls meaningful names and initialize variables upon declaration.
    Last edited by Peter Swinkels; May 12th, 2021 at 03:12 AM.

  7. #7

    Thread Starter
    New Member TheTveit's Avatar
    Join Date
    May 2021
    Posts
    5

    Re: How to simplify and compress code?

    I was also thinking about some kind of loop where you would only need to switch out the numbers of the contents, but i have no idea how to loop through a small part of code while also making it so that the same code can be used for task 1; result 1 and task2; result 2 and not making that code just as long as the old.

    The functions and names have a meaning behind them like "EX_data1" stands for "Existing data 1", "F1" - "F20" being the enable functions, "FS1" - "FS20" being the end / transfer functions. F standing for function and S standing for the norwegian word "end" (that word usually gets censored online)

    You are right about the " = True" thing, i just like to have it in as a mental note of what i'm doing. I usually remove it when i finish the project if the part is not needed.
    Last edited by TheTveit; May 12th, 2021 at 03:41 AM.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How to simplify and compress code?

    There are multiple ways this could be done. Here is an example of just one of them:
    vb.net Code:
    1. Private checkBoxes As CheckBox()
    2. Private textBoxes As TextBox()
    3.  
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5.     checkBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
    6.     textBoxes = {TextBox1, TextBox2, TextBox3, TextBox4}
    7. End Sub
    8.  
    9. Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged,
    10.                                                                                 CheckBox3.CheckedChanged,
    11.                                                                                 CheckBox2.CheckedChanged,
    12.                                                                                 CheckBox1.CheckedChanged
    13.     Dim currentCheckBox = DirectCast(sender, CheckBox)
    14.     Dim index = Array.IndexOf(checkBoxes, currentCheckBox)
    15.     Dim currentChecked = currentCheckBox.Checked
    16.  
    17.     'Enable a TextBox if and only if the corresponding CheckBox is checked.
    18.     textBoxes(index).Enabled = currentChecked
    19.  
    20.     index += 1
    21.  
    22.     'Check whether this is the last CheckBox.
    23.     If index < checkBoxes.Length Then
    24.         'Enable the next CheckBox if and only if the current CheckBox is checked.
    25.         checkBoxes(index).Enabled = currentChecked
    26.     End If
    27. End Sub
    28.  
    29. Private Sub CheckBoxes_EnabledChanged(sender As Object, e As EventArgs) Handles CheckBox4.EnabledChanged,
    30.                                                                                 CheckBox3.EnabledChanged,
    31.                                                                                 CheckBox2.EnabledChanged,
    32.                                                                                 CheckBox1.EnabledChanged
    33.     Dim currentCheckBox = DirectCast(sender, CheckBox)
    34.  
    35.     'Uncheck a CheckBox when it is disabled.
    36.     If Not currentCheckBox.Enabled Then
    37.         currentCheckBox.Checked = False
    38.     End If
    39. End Sub
    40.  
    41. Private Sub TextBoxes_EnabledChanged(sender As Object, e As EventArgs) Handles TextBox4.EnabledChanged,
    42.                                                                                TextBox3.EnabledChanged,
    43.                                                                                TextBox2.EnabledChanged,
    44.                                                                                TextBox1.EnabledChanged
    45.     Dim currentTextBox = DirectCast(sender, TextBox)
    46.  
    47.     'Clear a TextBox when it is disabled.
    48.     If Not currentTextBox.Enabled Then
    49.         currentTextBox.Clear()
    50.     End If
    51. End Sub
    You can extend that as much as you like. Note that checking or unchecking a CheckBox only directly affects the Enabled property of the corresponding TextBox and the next CheckBox. Disabling a TextBox clears it and disabling a CheckBox unchecks it and that, in turn affects another TextBox and CheckBox. If you had every CheckBox checked and every TextBox populated, unchecking the first CheckBox would initiate a cascade that would disable and clear every TextBox and disabled and uncheck every other Checked Box.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How to simplify and compress code?

    If you wanted to use the numbers in the control names instead of arrays then here's the code from my previous post modified to do that:
    vb.net Code:
    1. Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_4.CheckedChanged,
    2.                                                                                 CheckBox_3.CheckedChanged,
    3.                                                                                 CheckBox_2.CheckedChanged,
    4.                                                                                 CheckBox_1.CheckedChanged
    5.     Dim currentCheckBox = DirectCast(sender, CheckBox)
    6.     Dim number = CInt(currentCheckBox.Name.Split("_"c).Last())
    7.     Dim currentChecked = currentCheckBox.Checked
    8.  
    9.     'Enable a TextBox if and only if the corresponding CheckBox is checked.
    10.     Controls("TextBox_" & number).Enabled = currentChecked
    11.  
    12.     number += 1
    13.  
    14.     Dim nextCheckBox = Controls("CheckBox_" & number)
    15.  
    16.     'Check whether this is the last CheckBox.
    17.     If nextCheckBox IsNot Nothing Then
    18.         'Enable the next CheckBox if and only if the current CheckBox is checked.
    19.         nextCheckBox.Enabled = currentChecked
    20.     End If
    21. End Sub
    22.  
    23. Private Sub CheckBoxes_EnabledChanged(sender As Object, e As EventArgs) Handles CheckBox_4.EnabledChanged,
    24.                                                                                 CheckBox_3.EnabledChanged,
    25.                                                                                 CheckBox_2.EnabledChanged,
    26.                                                                                 CheckBox_1.EnabledChanged
    27.     Dim currentCheckBox = DirectCast(sender, CheckBox)
    28.  
    29.     'Uncheck a CheckBox when it is disabled.
    30.     If Not currentCheckBox.Enabled Then
    31.         currentCheckBox.Checked = False
    32.     End If
    33. End Sub
    34.  
    35. Private Sub TextBoxes_EnabledChanged(sender As Object, e As EventArgs) Handles TextBox_4.EnabledChanged,
    36.                                                                                TextBox_3.EnabledChanged,
    37.                                                                                TextBox_2.EnabledChanged,
    38.                                                                                TextBox_1.EnabledChanged
    39.     Dim currentTextBox = DirectCast(sender, TextBox)
    40.  
    41.     'Clear a TextBox when it is disabled.
    42.     If Not currentTextBox.Enabled Then
    43.         currentTextBox.Clear()
    44.     End If
    45. End Sub
    I've put underscores in the control names to make them easy to split but, other than that, the last two event handlers are exactly the same as before, while the first one has changed just a little.

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,815

    Re: How to simplify and compress code?

    I think using a Dictionary is better to read

    Code:
    Public Class Form1
    
    
        Private Sub WireUpCheckboxesWithTextboxes()
            Dim kvps = New Dictionary(Of CheckBox, Control) From {{CheckBox1, TextBox1}, _
                                                                 {CheckBox2, TextBox2}}
            For Each kvp In kvps
                kvp.Value.Enabled = kvp.Key.Checked
                kvp.Value.DataBindings.Add("Enabled", kvp.Key, "Checked", False, DataSourceUpdateMode.Never)
            Next
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            WireUpCheckboxesWithTextboxes()
        End Sub
    
    End Class
    EDIT:
    you can select that way what ever control, here with Textbox and DTP
    Code:
     Private Sub WireUpCheckboxesWithTextboxes()
            Dim kvps = New Dictionary(Of CheckBox, Control) From {{CheckBox1, TextBox1}, _
                                                                 {CheckBox2, TextBox2}, _
                                                                 {CheckBox3, DateTimePicker1}}
            For Each kvp In kvps
                kvp.Value.Enabled = kvp.Key.Checked
                kvp.Value.DataBindings.Add("Enabled", kvp.Key, "Checked", False, DataSourceUpdateMode.Never)
            Next
        End Sub
    Last edited by ChrisE; May 12th, 2021 at 05:38 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How to simplify and compress code?

    Quote Originally Posted by ChrisE View Post
    I think using a Dictionary is better to read
    Fair enough, but there are two TextBoxes per CheckBox, so that means either two Dictionaries keyed on the same CheckBoxes or one Dictionary where the values are some type that contains two TextBoxes, e.g.
    vb.net Code:
    1. Private textBoxesByCheckBox As Dictionary(CheckBox, Tuple(Of TextBox, TextBox))
    and:
    vb.net Code:
    1. textBoxesByCheckBox = New Dictionary(CheckBox, Tuple(Of TextBox, TextBox)) From {{CheckBox1, Tuple.Create(TextBox1A, TextBox1B)}, ...}
    Last edited by jmcilhinney; May 12th, 2021 at 07:52 PM. Reason: Added missing parenthesis

  12. #12
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,815

    Re: How to simplify and compress code?

    Quote Originally Posted by jmcilhinney View Post
    Fair enough, but there are two TextBoxes per CheckBox, so that means either two Dictionaries keyed on the same CheckBoxes or one Dictionary where the values are some type that contains two TextBoxes
    missed that part where he need's 2 Textboxes per checkbox, but I think the OP has enough to work with now
    Last edited by ChrisE; May 12th, 2021 at 09:30 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  13. #13

    Thread Starter
    New Member TheTveit's Avatar
    Join Date
    May 2021
    Posts
    5

    Re: How to simplify and compress code?

    Thank you both!
    I went with jmcilhinney's solution since they posted first.
    Didn't have much time to do stuff yesterday, but i'll take a look through the thread so i can learn how this works
    I can probably use much of the same solutions when working with the text on the main form too!

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