Usuário com melhor resposta
maskedTextBox, como trabalhar corretamente

Pergunta
-
Bom dia a todos,
Possuo um maskedTextBox em minha aplicação e preciso uma formatação de moeda (não precisa ter o simbolo).
Eu gostaria que ao digitar o valor no controle, ele fosse completando o valor automaticamente, ex:
Digito 5 e ficaria 0,05, depois 0, ficaria 0,50 e assim por diante.
Mas com a mascara que coloquei (##,##), tenho que dar espaço se meu valor não completar for menor que o tamanho da mascara.
Como usar corretamente o controle?
Att
Luciano Pimenta
Respostas
-
Boa tarde Luciano,
Utilize o evento KeyPress e KeyDown para montar um mask textbox dinâmico, o exemplo abaixo foi utilizado um textbox, pois o maskedTexBox são mascara fixas, segue exemplo abaixo:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string str = ""; private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { int KeyCode = e.KeyValue; if (!IsNumeric(KeyCode)) { e.Handled = true; return; } else { e.Handled = true; } if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0)) { str = str.Substring(0, str.Length - 1); } else if (!((KeyCode == 8) || (KeyCode == 46))) { str = str + Convert.ToChar(KeyCode); } if (str.Length == 0) { textBox1.Text = ""; } if (str.Length == 1) { textBox1.Text = "0.0" + str; } else if (str.Length == 2) { textBox1.Text = "0." + str; } else if (str.Length > 2) { textBox1.Text = str.Substring(0, str.Length - 2) + "." + str.Substring(str.Length - 2); } } private bool IsNumeric(int Val) { return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46)); } private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { e.Handled = true; } } }
Caso tenha sido útil, marque como resposta, segue link abaixo do artigo sobre o mesmo:
http://www.codeproject.com/KB/edit/currency_textbox_c__form.aspx
Vitor Mendes | Seu feedback é muito importante para todos!- Marcado como Resposta Luciano Pimenta quarta-feira, 13 de julho de 2011 19:10
Todas as Respostas
-
Boa tarde Luciano,
Utilize o evento KeyPress e KeyDown para montar um mask textbox dinâmico, o exemplo abaixo foi utilizado um textbox, pois o maskedTexBox são mascara fixas, segue exemplo abaixo:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string str = ""; private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { int KeyCode = e.KeyValue; if (!IsNumeric(KeyCode)) { e.Handled = true; return; } else { e.Handled = true; } if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0)) { str = str.Substring(0, str.Length - 1); } else if (!((KeyCode == 8) || (KeyCode == 46))) { str = str + Convert.ToChar(KeyCode); } if (str.Length == 0) { textBox1.Text = ""; } if (str.Length == 1) { textBox1.Text = "0.0" + str; } else if (str.Length == 2) { textBox1.Text = "0." + str; } else if (str.Length > 2) { textBox1.Text = str.Substring(0, str.Length - 2) + "." + str.Substring(str.Length - 2); } } private bool IsNumeric(int Val) { return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46)); } private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { e.Handled = true; } } }
Caso tenha sido útil, marque como resposta, segue link abaixo do artigo sobre o mesmo:
http://www.codeproject.com/KB/edit/currency_textbox_c__form.aspx
Vitor Mendes | Seu feedback é muito importante para todos!- Marcado como Resposta Luciano Pimenta quarta-feira, 13 de julho de 2011 19:10
-