使用参考在另一个类库项目中添加对象的代码-Visual Basic [英] Add object's code in another Class Library Project with Reference - Visual Basic
问题描述
我有一个名为Odest Installer的Windows窗体项目.它是具有自定义功能的自定义安装程序.它包括4种形式.我要做的是让某些对象(例如文本框)的源代码位于一个名为多次安装扩展"的类库项目中.我已经尝试过了:
I have a Windows Form Project called Odest Installer. It is a custom Installer with custom features. It consists of 4 forms. What I want to do is I want some Objects (Eg. Textbox) to have their Source Code Located in a Class Library Project called Multiple Installs Extension. I've tried this:
在多次安装"扩展程序中(类库项目):
In Multiple Installs Extension (Class Library Project):
Imports Odest_Installer
Dim getval As Integer
Public Class Class1
Public Sub txtboxshow ()
If TextBox1.Value < 3 Then
Button4.Disabled = True
TextBox1.Text = "3"
Else
EndIf
If TextBox1.Value = 3 Then
Button4.Disabled = True
Else
EndIf
If TextBox1.Value > 1 Then
Button5.Disabled = True
TextBox1.Text = "1"
Else
EndIf
If TextBox1.Value = 1 Then
Button5.Disabled = True
Else
EndIf
End Sub
Public Sub btn4()
getval = TextBox1.Value + 1
End Sub
Public Sub btn5()
getval = TextBox1.Value - 1
End Sub
End Class
在Odest安装程序(WindowsFormApplication)中:
In Odest Installer (WindowsFormApplication):
Imports ClassLibrary1
Public Class Form 3
Private Sub TextBox1.TextChanged() Handles TextBox1.TextChanged
txtboxshow()
End Sub
Private Sub Button4.OnClick() Handles Button1.Onclick
btn4()
End Sub
Private Sub Button5.OnClick() Handles Button1.Onclick
btn5()
End Sub
End Class
即使在相互引用后,TextBox1也会收到错误消息未声明Textbox1.由于其保护级别,它可能无法访问".我在某个地方出错吗,或者您有另一种引用这两个项目的方式. (注意:我不想将多个安装扩展添加到我的项目中,以便可以移植)
The TextBox1 gets an error "Textbox1 is not declared. It may be inaccessible due to its protection level" even after giving a Reference to each Other. Am I going wrong somewhere or You have another way of Referencing these two Projects. (Note: I don't want to Add Multiple Installs Extensions into my Project so it can become portable)
推荐答案
您的代码有两个问题-文本框没有.value
属性,因此您需要将.text
属性转换为和每次都是整数.
There are a couple of problems with your code - TextBoxes don't have .value
properties so you'll need to convert the .text
property to and integer each time.
老实说-除非有必要将类库中的代码分开,否则应将其全部保留在表单的类定义内.
To be honest - unless there is a reason why the code in the class library needs to be separate, you should keep it all within your form's class definition.
但是-如果您需要在单独的类中编写代码,请尝试
However - if you need the code in a separate class, try this
更改类库定义,使它们接受控件和getval作为参数,并更改代码以使其匹配,而不是尝试直接在类库中引用该表单.
Rather than try to reference the form directly in your class library, change your class library definitions so that they accept the controls and getval as parameters, and change the code to match.
赞
Public Sub txtboxshow(tBox1 As TextBox, btn4 As Button, btn5 As Button)
If CInt(tBox1.Text) < 3 Then
btn4.Enabled = False
tBox1.Text = "3"
Else
End If
If CInt(tBox1.Text) = 3 Then
btn4.Enabled = False
Else
End If
If CInt(tBox1.Text) > 1 Then
btn5.Enabled = False
tBox1.Text = "1"
Else
End If
If CInt(tBox1.Text) = 1 Then
btn5.Enabled = False
Else
End If
End Sub
Public Sub btn4(tbox1 As TextBox, ByRef getval As Integer)
getval = CInt(tbox1.Text) + 1
End Sub
Public Sub btn5(tbox1 As TextBox, ByRef getval As Integer)
getval = CInt(tbox1.Text) - 1
End Sub
但是txtBoxShow
中的if.. End If
语句可能存在逻辑问题.
There are however possible logic problems with the if.. End If
statements in txtBoxShow
.
如果.text="3"
第一个If
语句将使它保持不变,并使button4保持其原始状态.
The first If
statement will leave it alone and leave button4 in it's original state.
第二个If
语句将保留textbox1
并禁用button4
The second If
statement will leave textbox1
alone and disable button4
第三个If
语句会将textbox1.text
设置为"1",并禁用button5
The third If
statement will set textbox1.text
to "1" and disable button5
因为textbox1现在包含"1,所以第四个If
也将禁用button5
Because textbox1 now contains "1, the fourth If
will also disable button5
因此,如果textbox1
包含三个,则最终将禁用button4
和button5
,而textbox1
将包含"1"
So if textbox1
contains three, you will end up with button4
and button5
disabled and textbox1
will contain "1"
这似乎不是您想要的行为.您的If
语句可能需要重写.
It doesn't seem like this is behaviour you want. Your If
statements may need rewriting.
这篇关于使用参考在另一个类库项目中添加对象的代码-Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!