19需要文本框循环指定值0 [英] 19 Text Box required Loop to assign value 0
本文介绍了19需要文本框循环指定值0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我在vb.net表格中有19个文本框。 每当我清除或为文本框指定相同的值时,我将其写入大约19或38或更多。
我使用函数来精确地减少代码, 用于功能, 但仍然是19或38或太多。 我需要一个清除文本框中所有值的循环。
textbox1.clear()
textbox2.clear()
。
。
textexbox19.clear()
解决方案
根据以下内容向项目添加代码模块
公共模块扩展
< Runtime。 CompilerServices.Extension>
公共迭代器函数后代(T作为类)(ByVal控件作为控件)As IEnumerable(Of T)
For Each child As Control in control.Controls
Dim childOfT As T = TryCast(子,T)
如果childOfT IsNot Nothing那么
收益childOfT
结束如果
如果是child.HasChildren那么
For Each descendant As T在child.Descendants(Of T)()
收益后代
下一个
结束如果
下一个
结束函数
结束模块
用法
私有子Button1_Click(发件人为对象,e为EventArgs)处理Button1.Click
Dim tbList =(来自c In Descendants(Of TextBox)()选择c).ToList()
For each tb As TextBox in tbList
tb.Text =" 0"
下一个
结束子
Hi All
I have 19 text box in vb.net form. whenever i clear or assign same values to text box i write it almost 19 or 38 or more than this.
i precise it with function to reduce the code, with use for function, but still 19 or 38 or too much. i need a loop who clear all the values from text box.
textbox1.clear()
textbox2.clear()
.
.
textexbox19.clear()
解决方案
Add a code module to your project as per the following
Public Module Extensions <Runtime.CompilerServices.Extension> Public Iterator Function Descendants(Of T As Class)(ByVal control As Control) As IEnumerable(Of T) For Each child As Control In control.Controls Dim childOfT As T = TryCast(child, T) If childOfT IsNot Nothing Then Yield childOfT End If If child.HasChildren Then For Each descendant As T In child.Descendants(Of T)() Yield descendant Next End If Next End Function End ModuleUsage
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim tbList = (From c In Descendants(Of TextBox)() Select c).ToList() For Each tb As TextBox In tbList tb.Text = "0" Next End Sub
这篇关于19需要文本框循环指定值0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文