Vb-2005如何使用名称和索引通过循环处理文本框? [英] Vb-2005 how to process textboxes through loop using their names and index?

查看:73
本文介绍了Vb-2005如何使用名称和索引通过循环处理文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:Winform包含以下文本框... 

txtPrice01
txtPrice02
txtPrice03
txtPrice04
txtPrice05
....
....
txtPrice60


txtQty01
txtQty02
txtQty03
txtQty04
txtQty05
....
....
txtQty60





我是什么尝试过:



我正在使用以下代码...但是没有按要求工作.... 


Dim ItemID As Integer()= {0,1,2,61,3,62,14,13,63,12,11,64,18,16,15,65,8 ,66,9,10,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41 ,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60}
Dim n as string

对于i as Integer = 1到60
如果i< 10然后n =0+ i.ToString()Else n = i.ToString()

txtQty = DirectCast(txtQty+ n,TextBox)
txtPrice = DirectCast( txtPrice+ n,TextBox)

txtQty.Clear()
txtPrice.Text = ITM.PriceSalesSP(ItemID(i))
Next

解决方案

为你正在使用的两种类型的盒子创建一个TextBox数组,将表单控件分配给数组并完成。



要获取控件的索引,您可以对字符串的最后两位进行子串和解析,或者可以将索引放入控件的Tag字段中(需要更多工作)。



  Dim 数量( 61  as  TextBox 
Dim 价格( 61 As TextBox

For 每个 ctrl 作为控制 < span class =code-keyword> Me .Controls
If ctrl.Name.StartsWith( txtQty)= True 然后
Dim idx as 整数

idx = 整数 .Parse(ctrl.Name.Substring(ctrl.Name.Length - 2 ))

数量(idx)= ctrl
end if
'
对价格执行相同操作
'
下一页





或者使用Tag方法;



 对于 每个 ctrl 作为控制   Me  .Controls 
If ctrl.Name.StartsWith( txtQty)= True 然后
数量(ctrl.Tag)= ctrl
end if
下一步





如果放置控件进入容器以将它们保持在一起然后你只需要循环容器控件而不是整个表单,如果你想要表单并且有容器控制s你想搜索你需要单独迭代容器.HasChildren = True表示它是一个带子控件的容器。





现在您的设置只需使用TextBox数组来对控件执行计算和操作;



  For  i  As  整数 =  1     60  
数量(i).Clear()
价格(i).Text = ITM.PriceSalesSP(ItemID(i))
下一步


Suppose: Winform Contains following textboxes...

txtPrice01
txtPrice02
txtPrice03
txtPrice04
txtPrice05
....
....
txtPrice60


txtQty01
txtQty02
txtQty03
txtQty04
txtQty05
....
....
txtQty60



What I have tried:

I am using following code...which did not work as required....


 Dim ItemID As Integer() = {0, 1, 2, 61, 3, 62, 14, 13, 63, 12, 11, 64, 18, 16, 15, 65, 8, 66, 9, 10, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60}
 Dim n as string 

 For i As Integer = 1 To 60
        If i < 10 Then n = "0" + i.ToString() Else n = i.ToString()

        txtQty = DirectCast("txtQty" + n, TextBox)
        txtPrice = DirectCast("txtPrice" + n, TextBox)

        txtQty.Clear()
        txtPrice.Text = ITM.PriceSalesSP(ItemID(i))
 Next

解决方案

Create an array of TextBox for the two type of box you are using, assign the form controls to the array and your done.

To get the index of the control you can substring and parse the last two digits of the name or you can put the index into the Tag field of the control (bit more work involved).

Dim Quantities(61) as TextBox
Dim Prices(61) As TextBox

For Each ctrl As Control in Me.Controls
  If ctrl.Name.StartsWith("txtQty") = True Then
     Dim idx as Integer

     idx = Integer.Parse(ctrl.Name.Substring(ctrl.Name.Length - 2))

     Quantities(idx) = ctrl
   end if
'
' Do the same for Prices
'
Next



Or using the Tag method;

For Each ctrl As Control in Me.Controls
  If ctrl.Name.StartsWith("txtQty") = True Then
     Quantities(ctrl.Tag) = ctrl
   end if
Next



If you put the controls into a container to keep them together then you only need to loop through the containers controls not the whole form, if you are loping the form and have container controls you want to also search you need to iterate the containers separately the .HasChildren = True indicates it is a container with child controls.


Now your set you simply use the TextBox arrays to perform your calculations and manipulations on the controls;

For i As Integer = 1 To 60
       Quantities(i).Clear()
       Prices(i).Text = ITM.PriceSalesSP(ItemID(i))
Next


这篇关于Vb-2005如何使用名称和索引通过循环处理文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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