Usuário com melhor resposta
Tratar textbox vazios

Pergunta
-
Bom dia,
Tenho a necessidade de não deixar os textbox´s vazios.
Ja tentei com alguns exemplos que busquei no Keypress e Keydown e simplesmente nada funciona.
Quando dou ENTER são ignorados ou o Tabindex fica fora de ordem.
Alguem poderia me ajudar por favor:
Segue o codigo do FORM.
public frmPrestacaoContas()
{
InitializeComponent();
}
frmLogin frmloginn = Application.OpenForms["frmLogin"] as frmLogin;
private void bloqueartxtbox()
{
extraçãoTextBox.Enabled = false;
dataDateTimePicker.Enabled = false;
dataInicioDateTimePicker.Enabled = false;
quantidadeNormalTextBox.Enabled = false;
valorNormalDinheiroTextBox.Enabled = false;
quantidadeExtraTextBox.Enabled = false;
valorExtraDinheiroTextBox.Enabled = false;
totalDinheiroVendasTextBox.Enabled = false;
repasseFinalTextBox.Enabled = false;
totalDespesasTextBox.Enabled = false;
}
private void desbloqueartxtbox()
{
extraçãoTextBox.Enabled = true;
dataDateTimePicker.Enabled = true;
dataInicioDateTimePicker.Enabled = true;
quantidadeNormalTextBox.Enabled = true;
valorNormalDinheiroTextBox.Enabled = true;
quantidadeExtraTextBox.Enabled = true;
valorExtraDinheiroTextBox.Enabled = true;
totalDinheiroVendasTextBox.Enabled = true;
repasseFinalTextBox.Enabled = true;
totalDespesasTextBox.Enabled = true;
}
private void valorNormalDinheiroTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') &&
(e.KeyChar != ',' && e.KeyChar != '.' &&
e.KeyChar != (Char)13 && e.KeyChar != (Char)8))
{
e.KeyChar = (Char)0;
}
else
{
if (e.KeyChar == '.' || e.KeyChar == ',')
{
if (!valorNormalDinheiroTextBox.Text.Contains(','))
{
e.KeyChar = ',';
}
else
{
e.KeyChar = (Char)0;
}
}
}
}
private void valorNormalDinheiroTextBox_Enter(object sender, EventArgs e)
{
String x = "";
for (int i = 0; i <= valorNormalDinheiroTextBox.Text.Length - 1; i++)
{
if ((valorNormalDinheiroTextBox.Text[i] >= '0' &&
valorNormalDinheiroTextBox.Text[i] <= '9') ||
valorNormalDinheiroTextBox.Text[i] == ',')
{
x += valorNormalDinheiroTextBox.Text[i];
}
}
valorNormalDinheiroTextBox.Text = x;
valorNormalDinheiroTextBox.SelectAll();
}
private void valorExtraDinheiroTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') &&
(e.KeyChar != ',' && e.KeyChar != '.' &&
e.KeyChar != (Char)13 && e.KeyChar != (Char)8))
{
e.KeyChar = (Char)0;
}
else
{
if (e.KeyChar == '.' || e.KeyChar == ',')
{
if (!valorExtraDinheiroTextBox.Text.Contains(','))
{
e.KeyChar = ',';
}
else
{
e.KeyChar = (Char)0;
}
}
}
}
private void valorExtraDinheiroTextBox_Enter(object sender, EventArgs e)
{
String x = "";
for (int i = 0; i <= valorExtraDinheiroTextBox.Text.Length - 1; i++)
{
if ((valorExtraDinheiroTextBox.Text[i] >= '0' &&
valorExtraDinheiroTextBox.Text[i] <= '9') ||
valorExtraDinheiroTextBox.Text[i] == ',')
{
x += valorExtraDinheiroTextBox.Text[i];
}
}
valorExtraDinheiroTextBox.Text = x;
valorExtraDinheiroTextBox.SelectAll();
}
private void quantidadeExtraTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!char.IsDigit(ch) && ch != 8 && ch != 13)
{
e.Handled = true;
MessageBox.Show("Este campo aceita apenas números!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void frmPrestacaoContas_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.ProcessTabKey(true);
e.Handled = true;
}
}
private void totalDespesasTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') &&
(e.KeyChar != ',' && e.KeyChar != '.' &&
e.KeyChar != (Char)13 && e.KeyChar != (Char)8))
{
e.KeyChar = (Char)0;
}
else
{
if (e.KeyChar == '.' || e.KeyChar == ',')
{
if (!totalDespesasTextBox.Text.Contains(','))
{
e.KeyChar = ',';
}
else
{
e.KeyChar = (Char)0;
}
}
}
}
private void totalDespesasTextBox_Enter(object sender, EventArgs e)
{
String x = "";
for (int i = 0; i <= totalDespesasTextBox.Text.Length - 1; i++)
{
if ((totalDespesasTextBox.Text[i] >= '0' &&
totalDespesasTextBox.Text[i] <= '9') ||
totalDespesasTextBox.Text[i] == ',')
{
x += totalDespesasTextBox.Text[i];
}
}
totalDespesasTextBox.Text = x;
totalDespesasTextBox.SelectAll();
}
}
}
Christian de Góis Ribeiro
Respostas
-
Christian de Góis,
Pelo que eu entendi, você poderia colocar em um LOSTFOCUS (textChanged() ou Validating()) ou em botão de confirmação, como SALVAR...
Exemplo em uma finalização e conferência de informação que faltante:
foreach (Control c in this.Controls) { if (c.GetType().Name.ToString() == "TextBox") { if (string.IsNullOrEmpty(c.ToString())) { c.Focus(); } } }
=======================================
Evento LostFocus em TextBox (C#)
https://social.msdn.microsoft.com/Forums/pt-BR/5eaaa437-9724-4ca9-a872-1b2cc288062c/evento-lostfocus-em-textbox-c?forum=aspnetpt=======================================
Método String.IsNullOrEmpty (String)
https://msdn.microsoft.com/pt-br/library/system.string.isnullorempty%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
=======================================
foreach loop to loop all the text box in a form
https://social.msdn.microsoft.com/Forums/pt-BR/db55a8dc-5c1c-4831-af1f-a7016d42970c/foreach-loop-to-loop-all-the-text-box-in-a-form?forum=csharplanguage
=======================================
[]'s,
Fabio I.- Marcado como Resposta Christian de Góis sexta-feira, 31 de agosto de 2018 13:11
- Editado Fabio I sexta-feira, 31 de agosto de 2018 13:17
Todas as Respostas
-
Christian de Góis,
Desculpe, não entendi... "Tabindex fica fora de ordem"?!?
No caso de Windows Form você pode definir a ordem de tabulação. veja em:Você também pode definir o "TabStop" como FALSE e o "TAB" não passará pela "TextBox"...
[]'s,
Fabio I.- Editado Fabio I sexta-feira, 31 de agosto de 2018 12:30
-
Fabio bom dia,
Isso ja esta feito. Toda tabulacao esta correta.
Queria saber se tem algo haver com o codigo que coloquei no FORM.
Preciso saber em qual Evento correto seria colocado o codigo abaixo do exemplo:
if (textbox == "")
{
textbox.focus();
{
else
{
textbox2.focus();
}
Se seria no Keypress e o que estaria faltando para dar certo.
private void frmPrestacaoContas_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.ProcessTabKey(true);
e.Handled = true;
}
}Christian de Góis Ribeiro
-
Christian de Góis,
Pelo que eu entendi, você poderia colocar em um LOSTFOCUS (textChanged() ou Validating()) ou em botão de confirmação, como SALVAR...
Exemplo em uma finalização e conferência de informação que faltante:
foreach (Control c in this.Controls) { if (c.GetType().Name.ToString() == "TextBox") { if (string.IsNullOrEmpty(c.ToString())) { c.Focus(); } } }
=======================================
Evento LostFocus em TextBox (C#)
https://social.msdn.microsoft.com/Forums/pt-BR/5eaaa437-9724-4ca9-a872-1b2cc288062c/evento-lostfocus-em-textbox-c?forum=aspnetpt=======================================
Método String.IsNullOrEmpty (String)
https://msdn.microsoft.com/pt-br/library/system.string.isnullorempty%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
=======================================
foreach loop to loop all the text box in a form
https://social.msdn.microsoft.com/Forums/pt-BR/db55a8dc-5c1c-4831-af1f-a7016d42970c/foreach-loop-to-loop-all-the-text-box-in-a-form?forum=csharplanguage
=======================================
[]'s,
Fabio I.- Marcado como Resposta Christian de Góis sexta-feira, 31 de agosto de 2018 13:11
- Editado Fabio I sexta-feira, 31 de agosto de 2018 13:17
-