我如何缩短我的代码 [英] How Can I Shorten My Code

查看:66
本文介绍了我如何缩短我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为个人项目编写代码,现在它完全按照我的要求运行,没有任何错误!



这个东西对编码很新,代码很长,同样的代码一遍又一遍地复制



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

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





如您所见,有26个按钮,每个按钮重复相同的代码。有没有办法缩短









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

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.................





这也可以达到26



我尝试了很多不同的东西事情和失败,我认为现在是时候问,因为我确定这是一个非常简单的答案。



谢谢



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

推荐答案

在Maciej的解决方案上扩展一点:

(我猜测的类型加否定。)

Extending a bit on Maciej's solution:
(I'm guessing on the types of plus and negative.)
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:

Private Sub Plus1_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus1.ClickButtonArea
    DoSomething(TextBox1, Label1, Plus1, Negative1)
    End Sub



调用相同的 DoSomething 方法,每种情况都有不同的参数。


Call the same DoSomething method with different arguments for each case.


这部分代码:

This part of code:
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:

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:

Private Sub Plus1_ClickButtonArea(Sender As Object, e As MouseEventArgs) Handles Plus1.ClickButtonArea
    DoSomething()
    End Sub





注意:请查看 DoSomething()程序。你看到你和我的代码之间的区别了吗?



Note: Please, have a look at DoSomething() procedure. Do you see the difference between your and mine code?


引用:



每组按钮,文本框和标签是否保持相同的相对位置?如果是这样,请考虑创建一个包含其中每个项的UserControl,并将您的逻辑写入该UserControl。然后你可以将这个UserControl添加到你的表单中,他们都会按照相同的逻辑行事。

TnTinMn - 17小时前

回复

[修改评论。] [删除评论。]

我怎么去做?

会员10439491 - 7分钟前

回复


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

既然你问过,我会提出另一种解决方案。

1.从项目菜单中选择添加用户控件(文件 - 编辑 - 查看 - 项目-... )在VS IDE的顶部。

2.您可以从工具箱添加控件,类似于将其添加到表单的方式。

3.你也可以像表单一样添加代码。

4.对UserControl进行更改后,通过右键单击解决方案资源管理器窗口中的项目构建项目,然后选择构建。

5.返回表单的设计视图。 UserControl应出现在YourProjectName组件选项卡下方顶部的ToolBox中。现在您可以将其添加到表单中。

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.


这篇关于我如何缩短我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆