Visual Studio - 一次影响所有文本框 [英] Visual Studio - Affect all textboxes at once

查看:26
本文介绍了Visual Studio - 一次影响所有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的文本框中删除前导零.我有代码工作,但我有接近 50 个文本框.我真的不想制作 50 个 textbox.TextChanged 事件.

I am trying to remove leading zeroes from my textboxes. I have the code working, but I have close to 50 textboxes. I don't really want to have to make 50 textbox.TextChanged events.

有没有办法用相同的代码影响所有的文本框?

Is there anyway to affect all of the textboxes with the same code?

这是我使用的代码:

Private Sub txtTier1_100_TextChanged(sender As Object, e As EventArgs) Handles txtTier1_100.TextChanged

    txtTier1_100.Text = txtTier1_100.Text.TrimStart("0"c)

End Sub

推荐答案

第一步是定义一个通用的处理程序

First step is to define a general purpose handler

Private Sub HandleTextChanged(sender As Object, e As EventArgs) 
  Dim tb = CType(sender, TextBox)
  tb.Text = tb.Text.TrimStart("0"c)
End Sub

然后将您的每个 TextBox 实例附加到此单个处理程序

Then attach every one of your TextBox instances to this single handler

AddHandler txtTier1_100.TextChanged, AddressOf HandleTextChanged
AddHandler txtTier1_101.TextChanged, AddressOf HandleTextChanged
AddHandler txtTier1_102.TextChanged, AddressOf HandleTextChanged

请注意,如果您在集合中拥有所有 TextBox 实例,则也可以使用 For Each 循环来完成

Note that if you had all of the TextBox instances in a collection this could be done with a For Each loop as well

ForEach tb in textBoxList 
  AddHandler tb.TextChanged, AddressOf HandleTextChanged
Next

这篇关于Visual Studio - 一次影响所有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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