从编程创建的复选框中获取结果 [英] getting a result from a programatically created checkbox
问题描述
我创建了一个Windows窗体应用程序,并以编程方式添加了几个控件。每次添加的控件都不一样 - 它们是动态的,具体取决于我的数据。控件将是TextBox或CheckBox。
用户输入数据并单击提交后,我显然必须从我创建的控件中检索数据。 使用 Me.Controls.Item(a).Text 从TextBox获取数据没有问题。
但是,我无法确定如何判断是否已检查CheckBox。
如果我使用 me.controls.item(a).ToString 我得到" System.Windows.Forms.CheckBox,CheckState:1 " ;,但我想知道是否能访问该CheckState(或检查)而不必解析该字符串
格雷琴,
这是一种常见的方式:
Option Strict On
选项显式在
选项推断关闭
公共类Form1
私有子Form1_Load(发件人为System.Object,_ $ b $为As System.EventArgs)_
Handles MyBase.Load
Dim cb As New CheckBox
with cb
.Name =" CheckBox1"
.Location = New Point(40,80)
.Text =" Yes"
结束与
Me.Controls.Add(CB)
的AddHandler cb.CheckedChanged,AddressOf CheckBox_CheckedChanged
结束子
Private Sub _
CheckBox_CheckedChanged(发件人为System.Object的,_
                  &NBSP ;           &NBSP,E作为System.EventArgs)
     &NBSP ; 如果TypeOf发件人是CheckBox那么 Dim cb As CheckBox = DirectCast(发件人,CheckBox)
MessageBox.Show(的String.Format(QUOT;名称:{0} {1}经过:{2}" ;, _
         &NBSP ;                        &NBSP ;       cb.Name,vbCrLf,_
                                        cb.Checked)) 结束如果
结束次级
结束等级
如上所示,我有"停止"。这就像一个断点。当它到达那里时,将鼠标悬停在"cb"上。你可以访问它的所有属性。
我修改了一些,我展示了你想要的一些属性:
I have created a Windows Forms App and have programatically added several controls to it. The controls that are added are not the same every time - they are dynamic depending on my data. The controls will either be a TextBox or a CheckBox.
After the user enters the data and clicks Submit, I obviously have to retrieve the data from the controls I created. I have no problem with getting the data from the TextBox, using Me.Controls.Item(a).Text.
However, I cannot figure our how to tell if the CheckBoxes have been checked.
If I use me.controls.item(a).ToString I get "System.Windows.Forms.CheckBox, CheckState: 1", but I am wondering if I can access that CheckState (or Checked) without having to parse that string?
Gretchen,
This is a common way:
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load Dim cb As New CheckBox With cb .Name = "CheckBox1" .Location = New Point(40, 80) .Text = "Yes" End With Me.Controls.Add(cb) AddHandler cb.CheckedChanged, AddressOf CheckBox_CheckedChanged End Sub Private Sub _
CheckBox_CheckedChanged(sender As System.Object, _
e As System.EventArgs)
If TypeOf sender Is CheckBox Then
Dim cb As CheckBox = DirectCast(sender, CheckBox)
MessageBox.Show(String.Format("Name: {0}{1}Checked: {2}", _
cb.Name, vbCrLf, _
cb.Checked))
End If
End Sub End Class
As you can see above, I have "Stop". That works like a breakpoint. When it gets there, hover your mouse over "cb" and you have access to all of its properties.I have modified it some and I'm showing some of the properties you'll want:
这篇关于从编程创建的复选框中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!