textbox的值怎么在外部函数调用?

csuchen6 2011-07-25 12:08:51

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = 0
Dim SmallValue As Single
SmallValue = TextBox1.Text
End Sub

我有两个问题,都比较菜鸟,请不吝赐教啊。
其一:我怎样在TextBox内显示默认值?
其二:这里的SmallValue = TextBox1.Text的值怎么能在外部方法里面调用?谢谢了。
...全文
283 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wl58796351 2011-07-25
  • 打赏
  • 举报
回复


Public Class Form1

Property RetValue As String
Get
Return TextBox1.Text
End Get
Set(value As String)
TextBox1.Text = value
End Set
End Property


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "moren"
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = 0
Dim SmallValue As Single
SmallValue = TextBox1.Text
End Sub
End Class

'*****外部调用*****
Class Waibu
Dim frm As New Form1
Dim Mystr As String

Public Sub ReadTxt()
Mystr = frm.RetValue
End Sub
End Class
csuchen6 2011-07-25
  • 打赏
  • 举报
回复
感谢三位兄台,问题解决了!
cosmo_sei 2011-07-25
  • 打赏
  • 举报
回复
假设类名为 C -> 假设类的实例名为 C
cosmo_sei 2011-07-25
  • 打赏
  • 举报
回复
属性加上public声明
cosmo_sei 2011-07-25
  • 打赏
  • 举报
回复
1 在窗体show出来前的任意阶段给textbox赋值均可,可以在构造函数New里;也可以在designer里设置,design的初始化其实是New里面调用InitializeCompoment方法实现的;也可以在load里,就是1楼给出来的例子。关键要了解form的生命周期。

2 封装为属性,smallvalues应该定义为域。

Dim _smallValue As Single ''定义在外面
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = 0
_smallValue = TextBox1.Text
End Sub

Property ReadOnly SmallValue As Single
Get
Return _smallValue
End Get
End Property


而实际上,没有必要定义smallvalue,直接用一个属性返回转化后的textbox的值即可,属性定义

Property ReadOnly SmallValue As Single
Get
Return CType(TextBox1.Text, Single)
End Get
End Property


假设类名为 C,外部访问 C.SmallValue 即可
fsh1985 2011-07-25
  • 打赏
  • 举报
回复
定义个类外部访问变量,这样实例化个类的变量,就能访问到这个变量

默认只的话,就是在这个窗体的Load 事件写
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
End Sub

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧