Limpar Todos os Controles de um form...
Galera e o seguinte estou com um seguinte problema gostaria de limpar todos os controles de um form.
mais o que acontece e o seguinte, os contrles que estão dentro de outro não consigo limpar.
Ex.
Um controle que esta dentro de panel, TabControl esse n consigo limpar...
Galera obrigrado pela atenção.
Danielson Sousa
Curtidas 0
Respostas
Netasper
25/05/2010
Crie um metodo que limpa todos para vc. Coloca todos os controles do form dentro dele e invoca ele sempre que necessário.
[]s
[]s
GOSTEI 0
Danielson Sousa
25/05/2010
Isso eu já tenho, mais o que acontece...
ex:
TextBox dentro de panel ou tabcontrol ele não limpa, ele e limpo o text.... vcs souberem me ajudem...
int i ;
for (int i = 0; i <= this.Controls.Count - 1; i++)
{
if (this.Controls[i] is System.Windows.Forms.TextBox)
{
(this.Controls[i] as TextBox).Text = string.Empty;
}
}
GOSTEI 0
Netasper
25/05/2010
tenta esse
public static void ClearForm(System.Windows.Forms.Control parent)
{
foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
{
//Loop through all controls
if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
{
//Check to see if it's a textbox
((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
//If it is then set the text to String.Empty (empty textbox)
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
{
//If its a RichTextBox clear the text
((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
{
//Next check if it's a dropdown list
((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
//If it is then set its SelectedIndex to 0
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
{
//Next uncheck all checkboxes
((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
{
//Unselect all RadioButtons
((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
}
if (ctrControl.Controls.Count > 0)
{
//Call itself to get all other controls in other containers
ClearForm(ctrControl);
}
}
}
//Example call
ClearForm(this);
O resto que nao tiver e so acrescentar .
Espero que ajude
[]s
public static void ClearForm(System.Windows.Forms.Control parent)
{
foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
{
//Loop through all controls
if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
{
//Check to see if it's a textbox
((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
//If it is then set the text to String.Empty (empty textbox)
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
{
//If its a RichTextBox clear the text
((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
{
//Next check if it's a dropdown list
((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
//If it is then set its SelectedIndex to 0
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
{
//Next uncheck all checkboxes
((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
{
//Unselect all RadioButtons
((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
}
if (ctrControl.Controls.Count > 0)
{
//Call itself to get all other controls in other containers
ClearForm(ctrControl);
}
}
}
//Example call
ClearForm(this);
O resto que nao tiver e so acrescentar .
Espero que ajude
[]s
GOSTEI 0
Danielson Sousa
25/05/2010
Bem o que esse control parent.
Quando declaro ele esta pedindo....
Obg pela ajuda.
GOSTEI 0
Netasper
25/05/2010
São os controles que são passados para o metodo!!!
GOSTEI 0