LIMPAR CAMPO
Existe alguma maneira de limpar todos os campos textbox e maskedbox de meu form sem ter que ir de um a um.
Exemplo
txtnome.text = [Ô][Ô]
txtendereco.text = [Ô][Ô]
mskcodigo.text = [Ô][Ô]
mskcodigo.mask = ####.###
gostaria de saber se tem alguma forma porque possuo inumeros campos
Exemplo
txtnome.text = [Ô][Ô]
txtendereco.text = [Ô][Ô]
mskcodigo.text = [Ô][Ô]
mskcodigo.mask = ####.###
gostaria de saber se tem alguma forma porque possuo inumeros campos
Veja esse post meu em outro TOpico !
http://www.vbmania.com.br/pages/index.php?varModulo=Forum&varMethod=abrir&varID=362370&varWorld=
Abraços
http://www.vbmania.com.br/pages/index.php?varModulo=Forum&varMethod=abrir&varID=362370&varWorld=
Abraços
Fiz conforme o seu exemplo só que dá um erro, coloquei assim:
Private Sub cmdlimpar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlimpar.Click
Dim strErrorMess As String = Nothing
For Each ctrl As Control In GetChildControls(Me.Parent)
If TypeOf ctrl Is TextBox Then
ctrl.Text = String.Empty
End If
Next
End Sub
Function GetChildControls(ByVal parent As Control) As ArrayList
Dim textBoxList As New ArrayList()
For Each ctrl As Control In parent.Controls
[ô]add current control to the textbox list. O Erro dá aqui
textBoxList.Add(ctrl)
[ô]Recursive call to method to add all child controls as well.
textBoxList.AddRange(GetChildControls(ctrl))
Next
Return textBoxList
End Function
System.NullReferenceException was unhandled
Message=A referência de objecto não foi definida como uma instância de um objecto.
Source=RICTX
StackTrace:
em RICT.RICTX.GetChildControls(Control parent) em G:\RICTX\RICTX\RICTXictx.vb:line 35
em RICT.RICTX.cmdlimpar_Click(Object sender, EventArgs e) em G:\RICTX\RICTX\RICTXictx.vb:line 27
em System.Windows.Forms.Control.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ButtonBase.WndProc(Message& m)
em System.Windows.Forms.Button.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.Run(ApplicationContext context)
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
em RICT.My.MyApplication.Main(String[] Args) em 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
em System.Threading.ThreadHelper.ThreadStart_Context(Object state)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
em System.Threading.ThreadHelper.ThreadStart()
InnerException:
Private Sub cmdlimpar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlimpar.Click
Dim strErrorMess As String = Nothing
For Each ctrl As Control In GetChildControls(Me.Parent)
If TypeOf ctrl Is TextBox Then
ctrl.Text = String.Empty
End If
Next
End Sub
Function GetChildControls(ByVal parent As Control) As ArrayList
Dim textBoxList As New ArrayList()
For Each ctrl As Control In parent.Controls
[ô]add current control to the textbox list. O Erro dá aqui
textBoxList.Add(ctrl)
[ô]Recursive call to method to add all child controls as well.
textBoxList.AddRange(GetChildControls(ctrl))
Next
Return textBoxList
End Function
System.NullReferenceException was unhandled
Message=A referência de objecto não foi definida como uma instância de um objecto.
Source=RICTX
StackTrace:
em RICT.RICTX.GetChildControls(Control parent) em G:\RICTX\RICTX\RICTXictx.vb:line 35
em RICT.RICTX.cmdlimpar_Click(Object sender, EventArgs e) em G:\RICTX\RICTX\RICTXictx.vb:line 27
em System.Windows.Forms.Control.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ButtonBase.WndProc(Message& m)
em System.Windows.Forms.Button.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.Run(ApplicationContext context)
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
em RICT.My.MyApplication.Main(String[] Args) em 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
em System.Threading.ThreadHelper.ThreadStart_Context(Object state)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
em System.Threading.ThreadHelper.ThreadStart()
InnerException:
esse é o erro
Function [ô]GetChildControls[ô] doesn[ô]t return a value on all code paths. A null reference exception could occur at run time when the result is used. G:\RICTX\RICTX\RICTXictx.vb 50 5 RICTX
Function [ô]GetChildControls[ô] doesn[ô]t return a value on all code paths. A null reference exception could occur at run time when the result is used. G:\RICTX\RICTX\RICTXictx.vb 50 5 RICTX
Public Shared Sub ClearForm(parent As System.Windows.Forms.Control)
For Each ctrControl As System.Windows.Forms.Control In parent.Controls
[ô]Loop por todos os controles
If Object.ReferenceEquals(ctrControl.[GetType](), GetType(System.Windows.Forms.TextBox)) Then
[ô]Checando se é TextBox
DirectCast(ctrControl, System.Windows.Forms.TextBox).Text = String.Empty
ElseIf Object.ReferenceEquals(ctrControl.[GetType](), GetType(System.Windows.Forms.RichTextBox)) Then
[ô]If its a RichTextBox clear the text
DirectCast(ctrControl, System.Windows.Forms.MaskedTextBox).Text = String.Empty
End If
If ctrControl.Controls.Count > 0 Then
[ô]Call itself to get all other controls in other containers
ClearForm(ctrControl)
End If
Next
End Sub
e voalaaa
[ô]Chame Assim
ClearForm(Me)
Abraços
Tópico encerrado , respostas não são mais permitidas