Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I've been writing code for a personal project and it now all works exactly how I want it to with no errors!

The thing is im very new to coding and the code is very long with the same code copied over and over

VB
Private Sub Plus1_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus1.ClickButtonArea
        Dim ask As String
        If TextBox1.Visible = False Then
            ask = InputBox("Enter Income Name")
            If ask.Length <> 0 Then
                TextBox1.Visible = True
                Label1.Visible = True
                Label1.Text = ask
                Plus1.Visible = False
                Negative1.Visible = True
            Else
                Exit Sub
            End If
        End If
    End Sub

    Private Sub Plus2_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus2.ClickButtonArea
        Dim ask As String
        If TextBox2.Visible = False Then
            ask = InputBox("Enter Income Name")
            If ask.Length <> 0 Then
                TextBox2.Visible = True
                Label2.Visible = True
                Label2.Text = ask
                Plus2.Visible = False
                Negative2.Visible = True
            Else
                Exit Sub
            End If
        End If
    End Sub

................................

Private Sub Plus26_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus26.ClickButtonArea
        Dim ask As String
        If TextBox26.Visible = False Then
            ask = InputBox("Enter Income Name")
            If ask.Length <> 0 Then
                TextBox26.Visible = True
                Label26.Visible = True
                Label26.Text = ask
                Plus26.Visible = False
                Negative26.Visible = True
            Else
                Exit Sub
            End If
        End If
    End Sub


As you can see there are 26 buttons and the same code is repeated for every button. Is there a way to make that shorter

and also

VB
Private Sub Add_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles AddRemove.ClickButtonArea
        WorkItOut.Visible = False
        AddRemove.Visible = False
        Rename.Visible = False
        Done.Visible = True
        Panel1.Visible = True
        Panel2.Visible = True

        If TextBox1.Visible = False Then

            Plus1.Visible = True
        Else
            Negative1.Visible = True
        End If
        If TextBox2.Visible = False Then

            Plus2.Visible = True
        Else
            Negative2.Visible = True
        End If
        If TextBox3.Visible = False Then.................


That also goes up to 26

I've tried so many different things and failed that I think its now time to ask as im sure there's a really easy answer to this.

Thanks
Posted
Updated 5-Jun-20 3:24am
Comments
TnTinMn 3-Dec-13 20:40pm    
Does each grouping of buttons, textbox, and labels maintain the same relative position to each other? If so, consider creating a UserControl that contains each of these items and write your logic once into that UserControl. Then you can add this UserControl to your form and they will all behave according to the same logic.
Member 10439491 4-Dec-13 14:21pm    
how do I go about doing that?
Aravindba 5-Dec-13 1:02am    
try to use for each statement,like dim i as integer=1 to 26,and use i for append in textbox and label,positive and negative also.use dosomething fuction and pass parameter with append i value

Extending a bit on Maciej's solution:
(I'm guessing on the types of plus and negative.)
VB
Private Sub DoSomething(textBox as TextBox, label as Label, plus as Button, negative as Button)
        Dim ask As String, bRetVal As Boolean
        If textBox.Visible Then Exit Sub
        ask = InputBox("Enter Income Name")
        bRetVal = (ask.Length > 0) 
        textBox.Visible = bRetVal
        label.Visible = bRetVal
        label.Text = ask
        plus.Visible = bRetVal
        negative.Visible = bRetVal
End Sub

After that, call it:
VB
Private Sub Plus1_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus1.ClickButtonArea
    DoSomething(TextBox1, Label1, Plus1, Negative1)
    End Sub

Call the same DoSomething method with different arguments for each case.
 
Share this answer
 
v3
Comments
Maciej Los 4-Dec-13 6:32am    
Good alternative ;)
+5!
Member 10439491 4-Dec-13 13:30pm    
aha that would work. ill give that a go Thanks Everyone
Matt T Heffron 4-Dec-13 13:34pm    
Great.
Note that I just fixed an error in the assignment to label.Text
Member 10439491 4-Dec-13 13:38pm    
Yea I noticed that after I tried it and my label was named "true" rather then what was in my inputbox :)
Member 10439491 4-Dec-13 13:44pm    
oh and Plus1 and negative1 are CButtonLib.Cbutton so I need to try and make plus.Visible = bRetVal and negative.Visible = bRetVal work
This part of code:
VB
Dim ask As String
        If TextBox1.Visible = False Then
            ask = InputBox("Enter Income Name")
            If ask.Length <> 0 Then
                TextBox1.Visible = True
                Label1.Visible = True
                Label1.Text = ask
                Plus1.Visible = False
                Negative1.Visible = True
            Else
                Exit Sub
            End If
        End If


move to another procedure/function, like that:
VB
Private Sub DoSomething()
        Dim ask As String, bRetVal As Boolean
        If TextBox1.Visible Then Exit Sub
        ask = InputBox("Enter Income Name")
        bRetVal = (ask.Length > 0) 
        TextBox1.Visible = bRetVal
        Label1.Visible = bRetVal
        Label1.Text = bRetVal
        Plus1.Visible = bRetVal
        Negative1.Visible = bRetVal
End Sub


After that, call it:
VB
Private Sub Plus1_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus1.ClickButtonArea
    DoSomething()
    End Sub


Note: Please, have a look at DoSomething() procedure. Do you see the difference between your and mine code?
 
Share this answer
 
v2
Comments
CHill60 3-Dec-13 16:11pm    
Hi Maciej Los... good answer ... but I think you forgot to pass the controls into the DoSomething() sub. I think the OP might need that extra nudge
Maciej Los 3-Dec-13 16:14pm    
Thank you, Chill ;)
No, i haven't forgot. Have a look inside DoSomething procedure... ;)
CHill60 3-Dec-13 16:24pm    
d'oh ... got caught out by that old true/false game again *slaps head*
Maciej Los 3-Dec-13 16:26pm    
;)
Member 10439491 3-Dec-13 17:19pm    
I see the difference but wouldn't that only work for textbox1 and still have to write that same code for textbox2 and 3 and so on?
Quote:

Does each grouping of buttons, textbox, and labels maintain the same relative position to each other? If so, consider creating a UserControl that contains each of these items and write your logic once into that UserControl. Then you can add this UserControl to your form and they will all behave according to the same logic.
TnTinMn - 17 hrs ago
Reply
[Modify the comment.] [Delete the comment.]
how do I go about doing that?
Member 10439491 - 7 mins ago
Reply
Since you asked, I will present this an alternative solution.
1. Select "Add UserControl" From the "Project Menu" (File - Edit - View - Project -...) at the top of the VS IDE.
2. You can add controls from the toolbox similar to the way you add them to a form.
3. You can also add the code behind just like with a form.
4. After you have made changes to your UserControl, "Build" the project by right-clicking on the project in the "Solution Explorer" window and select "Build".
5. Go back to the design view of your form. The UserControl should be present in your ToolBox at the top under the tab "YourProjectName Components". Now you can add it to your form.
 
Share this answer
 
Comments
Member 10439491 4-Dec-13 16:23pm    
That seems like a good idea but I think ill have to use that for next time or ill have to remake my form I think

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900