Windows表单上的“重置"按钮 [英] Reset button on a Windows form
本文介绍了Windows表单上的“重置"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows窗体,其中包含文本框和组框,其中包含单选按钮和一个datetimepicker.我试图通过单选按钮将文本框重置为空格和组框,以便在按下表单上的重置"按钮时没有选中任何单选按钮,但是它不适用于单选按钮.只有文本框和datetimepicker起作用.
请帮忙.到目前为止的代码如下.
谢谢,
杜沙尔
Hi,
I have a windows forms with textboxes and group boxes containing radio buttons in them and a datetimepicker. I am trying to reset textboxes to spaces and group boxes with the radio buttons not to have any radio buttons selected when i press the Reset button on the form, however it is not working for the radio buttons. Only textboxes and datetimepicker is working.
Please help. the code so far is as under.
Thanks,
Tushar
Private Sub ClearControls(ByVal frm As Form)
Dim ctrl As Control
For Each ctrl In frm.Controls
If ctrl.GetType Is GetType(TextBox) Then
ctrl.Text = ""
ElseIf ctrl.GetType Is GetType(RadioButton) Then
Dim radbut As RadioButton = ctrl
radbut.Checked = False
ElseIf ctrl.GetType Is GetType(DateTimePicker) Then
Dim datetimepicker1 As DateTimePicker = ctrl
datetimepicker1.ResetText()
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ClearControls(Me)
End Sub
推荐答案
这非常简单:单选按钮位于GroupBox内:这意味着它们位于GroupBox.Controls列表内,而不是位于主窗体Controls列表内.更改您的ClearControls例程以接受ControlCollection而不是表单,并使用其中的所有项的Controls列表对其进行递归调用.
It''s pretty simple: the radio buttons are within a GroupBox: which means they are within the GroupBox.Controls list, rather than being in the main form Controls list. Change your ClearControls routine to accept a ControlCollection instead of a form, and recursively call it with the Controls list for all items within it.
尝试此操作
Try this
Private Sub ClearControls(ByVal frm As Form)
Dim ctrl As Control
For Each ctrl In frm.Controls
If ctrl.GetType Is GetType(TextBox) Then
ctrl.Text = ""
ElseIf ctrl.GetType Is GetType(RadioButton) Then
Dim radbut As RadioButton = ctrl
radbut.Checked = False
ElseIf ctrl.GetType Is GetType(DateTimePicker) Then
Dim datetimepicker1 As DateTimePicker = ctrl
datetimepicker1.ResetText()
ElseIf ctrl.GetType Is GetType(GroupBox) Then
ClearGroupedControls(ctrl)
End If
Next
End Sub
Private Sub ClearGroupedControls(ByVal gctrl As GroupBox)
For Each ctrl In gctrl.Controls
If ctrl.GetType Is GetType(RadioButton) Then
Dim radbut As RadioButton = ctrl
radbut.Checked = False
End If
Next
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ClearControls(Me)
End Sub
请参考以下站点以获取更多信息
http://stackoverflow. com/questions/297526/什么是清除表格c上所有控件的最佳方法
Please refer the follwing site for more
http://stackoverflow.com/questions/297526/what-is-the-best-way-to-clear-all-controls-on-a-form-c
这篇关于Windows表单上的“重置"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文