|
User inputs values into textboxes , user hits calculate(Button) user needs to be able to input new values so. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click lbl1.text = "" lbl2.text = "" lbl3.Text = "" End Sub Button1 is to clear all input and out put. The question is. Is there a better way of clearing all the user input and system output. There are about 13 textbox input values for the user and a little more then 100 label.text out puts. | | Sunspot9393 Monday, June 08, 2009 9:08 PM | This is the code that I was able to get running with Option Strict On:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr In activeTab.Controls.OfType(Of TextBox)()
DirectCast(ctr, TextBox).Text = String.Empty
Next
For Each ctr In activeTab.Controls.OfType(Of Label)()
DirectCast(ctr, Label).Text = String.Empty
Next
NOTE: You can put this code into a shared subroutine somewhere and have the appropriate button on any form call this routine. You would need to pass the TabControl in to the method. Let me know if you need the syntax for how to do this. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | DeborahK Tuesday, June 09, 2009 5:51 PM |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
This will work provided all controls are in same container, form1 in this context.
Thanks - Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void - Proposed As Answer byReed KimbleModeratorMonday, June 08, 2009 9:28 PM
- Marked As Answer byReed KimbleModeratorTuesday, June 09, 2009 8:30 PM
-
| | Omie Monday, June 08, 2009 9:23 PM |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
This will work provided all controls are in same container, form1 in this context.
Thanks
- Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
I generally second this post. One difference I would suggest would be to use OrElse instead of Or - OrElse performs short-circuiting of the evaluation and will not check to see if a label is a textbox as Or will. This will speed up the comparison a little bit for a ton of controls. Now, you may have additional textboxes and or lables which are not supposed to get reset with all the others. In this case, one solution is to use a naming convention you can test for: If TypeOf ctrl Is Label OrElse TypeOf ctrl Is TextBox AndAlso ctrl.Name.EndsWith("_x") Where all the labels and textboxes to clear have a "_x" at the end of their name. Or you can place a certain value in the Tag property of each lable/textbox to clear and test for that instead (e.g.... AndAlso CInt(ctrl.Tag) = 1 ... if you put a "1" in the Tag of each control to clear). This would also allow you to write a recursive routine which could go through all the control containers on the form in case your controls are not all located on the main form.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all" - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:08 PM
-
| | Reed Kimble Monday, June 08, 2009 9:34 PM | Something similar will work, you just need to get the active tab. Something like this:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr In activeTab.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = String.Empty
End If
Next
NOTE: This does not compile for me because I have option strict on. The ctr.Text generates a compile error. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:08 PM
-
| | DeborahK Tuesday, June 09, 2009 5:45 PM | assuming a clear button is on each tabpage. change the T.Name to the name for that page
For Each T As TabPage In TabControl1.TabPages
If T.Name = "TabPage1" Then
For Each ctrl As Control In T.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
ctrl.Text = ""
End If
Next
End If
Next
- Edited byjwavila Tuesday, June 09, 2009 5:51 PMedit
- Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | jwavila Tuesday, June 09, 2009 5:46 PM | Here is the subroutine:
#Region " ClearTabControl"
''' <summary>
''' Clears the TextBox and Labels from the active tab of the defined TabControl.
''' </summary>
''' <param name="currentTabControl">TabControl to clear</param>
''' <remarks></remarks>
Public Sub ClearTabControl(ByVal currentTabControl As TabControl)
' Only clear the active tab
Dim activeTab As TabPage = currentTabControl.SelectedTab
For Each ctr In activeTab.Controls.OfType(Of TextBox)()
DirectCast(ctr, TextBox).Text = String.Empty
Next
For Each ctr In activeTab.Controls.OfType(Of Label)()
DirectCast(ctr, Label).Text = String.Empty
Next
End Sub
#End Region
Hope this helps. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | DeborahK Tuesday, June 09, 2009 5:59 PM |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
This will work provided all controls are in same container, form1 in this context.
Thanks - Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void - Proposed As Answer byReed KimbleModeratorMonday, June 08, 2009 9:28 PM
- Marked As Answer byReed KimbleModeratorTuesday, June 09, 2009 8:30 PM
-
| | Omie Monday, June 08, 2009 9:23 PM |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
This will work provided all controls are in same container, form1 in this context.
Thanks
- Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
I generally second this post. One difference I would suggest would be to use OrElse instead of Or - OrElse performs short-circuiting of the evaluation and will not check to see if a label is a textbox as Or will. This will speed up the comparison a little bit for a ton of controls. Now, you may have additional textboxes and or lables which are not supposed to get reset with all the others. In this case, one solution is to use a naming convention you can test for: If TypeOf ctrl Is Label OrElse TypeOf ctrl Is TextBox AndAlso ctrl.Name.EndsWith("_x") Where all the labels and textboxes to clear have a "_x" at the end of their name. Or you can place a certain value in the Tag property of each lable/textbox to clear and test for that instead (e.g.... AndAlso CInt(ctrl.Tag) = 1 ... if you put a "1" in the Tag of each control to clear). This would also allow you to write a recursive routine which could go through all the control containers on the form in case your controls are not all located on the main form.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all" - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:08 PM
-
| | Reed Kimble Monday, June 08, 2009 9:34 PM |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
This will work provided all controls are in same container, form1 in this context. This works as you say in the context of a form. However in this app. each form has tabpages, and each page is simular but calculates different based on the what type needed.Eachpage has its own button and set ofvariables and formulas each has it's own Try & End Try In useing this method for clearing the user input and system output I would want the clear button to only clear the data for that page and not for the whole form. thanks | | Sunspot9393 Tuesday, June 09, 2009 5:34 PM | In my code see line
For Each ctr as Control in Me.Controls
note that Me.Controls This block of code search for all controls in 'Me' that is Form1. If you use same block for any particular button in any other container such as different panel or a tab page, and if you change Me.Controls to ContainerName.Controls, code should work for that container. For Example,
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Panel1.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = ""
End If
Next
End Sub
Also consider Reed's post about adding a tag or unique suffix for control name to identify control. Implement that if its necessary.
Thanks - Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void | | Omie Tuesday, June 09, 2009 5:40 PM | Something similar will work, you just need to get the active tab. Something like this:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr In activeTab.Controls
If TypeOf ctr Is Label Or TypeOf ctr Is TextBox Then
ctr.Text = String.Empty
End If
Next
NOTE: This does not compile for me because I have option strict on. The ctr.Text generates a compile error. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:08 PM
-
| | DeborahK Tuesday, June 09, 2009 5:45 PM | assuming a clear button is on each tabpage. change the T.Name to the name for that page
For Each T As TabPage In TabControl1.TabPages
If T.Name = "TabPage1" Then
For Each ctrl As Control In T.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
ctrl.Text = ""
End If
Next
End If
Next
- Edited byjwavila Tuesday, June 09, 2009 5:51 PMedit
- Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | jwavila Tuesday, June 09, 2009 5:46 PM | This is the code that I was able to get running with Option Strict On:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr In activeTab.Controls.OfType(Of TextBox)()
DirectCast(ctr, TextBox).Text = String.Empty
Next
For Each ctr In activeTab.Controls.OfType(Of Label)()
DirectCast(ctr, Label).Text = String.Empty
Next
NOTE: You can put this code into a shared subroutine somewhere and have the appropriate button on any form call this routine. You would need to pass the TabControl in to the method. Let me know if you need the syntax for how to do this. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | DeborahK Tuesday, June 09, 2009 5:51 PM | I would thank you for that add :)
Thanks
- Omie
C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void | | Omie Tuesday, June 09, 2009 5:52 PM | Here is the subroutine:
#Region " ClearTabControl"
''' <summary>
''' Clears the TextBox and Labels from the active tab of the defined TabControl.
''' </summary>
''' <param name="currentTabControl">TabControl to clear</param>
''' <remarks></remarks>
Public Sub ClearTabControl(ByVal currentTabControl As TabControl)
' Only clear the active tab
Dim activeTab As TabPage = currentTabControl.SelectedTab
For Each ctr In activeTab.Controls.OfType(Of TextBox)()
DirectCast(ctr, TextBox).Text = String.Empty
Next
For Each ctr In activeTab.Controls.OfType(Of Label)()
DirectCast(ctr, Label).Text = String.Empty
Next
End Sub
#End Region
Hope this helps. www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer bySunspot9393 Tuesday, June 09, 2009 6:07 PM
-
| | DeborahK Tuesday, June 09, 2009 5:59 PM | This is the code that I was able to get running with Option Strict On:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr In activeTab.Controls.OfType(Of TextBox)()
DirectCast(ctr, TextBox).Text = String.Empty
Next
For Each ctr In activeTab.Controls.OfType(Of Label)()
DirectCast(ctr, Label).Text = String.Empty
Next
NOTE: You can put this code into a shared subroutine somewhere and have the appropriate button on any form call this routine. You would need to pass the TabControl in to the method. Let me know if you need the syntax for how to do this.
www.insteptech.com We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
This is a great example ofwhy I thinkAnonymous Types are such a bad idea.... I realize the OP has got the idea of looping through the appropriate container(s) to clear the controls, but the following should be pointed out. Here is the proper fix to thatcode sample for Option Strict On:
Dim activeTab As TabPage = TabControl1.SelectedTab
For Each ctr As Control In activeTab.Controls
If TypeOf ctr Is Label OrElse TypeOf ctr Is TextBox Then
ctr.Text = String.Empty
End If
Next
Note that all that was needed was declaring ctr As Control instead of leaving it anonymous... using the OfType() extension will add a tremendous number of clock cycles to this routine given the number of controls in question and the fact that it must be run twice.
Also note, again, that this code should use the OrElse operator; it is a waste of clock cycles to check to see if the type of ctr is TextBox when it has already been determined to be a Label.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all" | | Reed Kimble Tuesday, June 09, 2009 8:28 PM |
|