如何使用vb.net中的ID从动态创建的文本框中检索值作为数组? [英] how to retrieve the value as array from a dynamically created textbox using its ID in vb.net?

查看:122
本文介绍了如何使用vb.net中的ID从动态创建的文本框中检索值作为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种编程方法不熟悉。情况是我已经动态创建了复选框和文本框,其中文本框的名称和复选框相同,并使用数据库处理。



我需要的数据从用户使用复选框选择的文本框中获取是



数组中的详细信息需要是文本框的名称及其相应的值文本框



i使用这种方法创建了复选框和文本框..与此相关的一些事情



  Dim  checkBox =  new  CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = 点(偏移量, 10
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = 大小(< span class =code-digit> 100 , 20







i在运行时成功创建复选框。帮我获取值..还建议我使用合适的数组列表。

解决方案

我完全不知道你的意思是建议我使用合适的数组列表 - 你不应该使用无论如何,ArrayList被Generic List< T>取代了。很多年前上课...



但是......如果你创建一个动态复选框(一般来说,这是一个糟糕的主意,通常有更好的方法来处理这种事情)你需要做的就是在构造它时在类级别保持对它的引用:将checkBox变量声明移动到类而不是方法内部,它将被保留以供以后使用。 />


替代方案,您可以查看Controls数组并找到它 - 但除非您添加一个区别特征,如Name属性,否则您将无法分辨哪个是如果你创建了多个。


你好...

当你创建你的复选框时,你应该给它一个名字。使用这个给定的名称,您可以在from(或容器控件)的控件集合中找到它,以询问它的检查状态。



另一种可能性是向命名Sub添加处理程序,允许您直接对Checkbox状态的更改做出反应。但是......当你使用这种方式时,你必须在处理表格后删除处理程序,你在。



问候

Ralf


您可以在VB.NET中使用OfType方法。



如果您有多个相同类型的控件或一个控件,使用以下方法:



   每个 CheckBox  as  CheckBox < controlcontainer> .Controls.OfType(< span class =code-keyword> Of  CheckBox)()
< controlcontainer> .Controls(CheckBox.Name).Text)
下一页
< / controlcontainer > < / controlcontainer > ;


Iam new to this programming method. the case is I have created the checkbox and textbox dynamically where the name of the text box and check box is equal and it is handled using database.

The data i need to get is from the textbox which is selected using checkbox by the user

The details in the array need to be name of the textbox and its corsponding value textbox

i created the checkbox and textbox using this method.. some thing related to this

Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)




i successfully created checkbox at run time .help me getting the value.. and also suggest me with the suitable array list .

解决方案

I have absolutely no idea what you mean by "suggest me with the suitable array list" - you shouldn't be using ArrayList anyway, there were superseded by the Generic List<T> class many years ago...

But...if you create a dynamic check box (and generally speaking, it's a poor idea, there are normally better ways to handle this kind of thing) all you need to do is keep a reference to it at a class level when you construct it: move the checkBox variable declaration to the class instead of inside the method and it will be persisted for later use.

Alternatives, you could look in the Controls array and find it - but unless you add a distinguishing feature such as a Name property, you won't be able to tell which is which if you create more than one.


Hi ...
When you create your Checkbox you should give it a Name. With this given Name you could find in inside the controls-collection of the from (or the container-control) to ask for it's checked-state.

Another possibility is to add a handler to a named Sub which allows you to react directly on the changing of the state of the Checkbox. But ... when you use this way you must later remove the handler when you dispose the form, you are in.

Greetings
Ralf


You can user OfType method in VB.NET.

If you have multiple controls of same type or one control, Use following method :

for each CheckBox as CheckBox In <controlcontainer>.Controls.OfType(Of CheckBox)()
     <controlcontainer>.Controls(CheckBox.Name).Text)
next 
</controlcontainer></controlcontainer>


这篇关于如何使用vb.net中的ID从动态创建的文本框中检索值作为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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