So I'm working on a calculator application in C-Sharp and I want to prevent people from entering more than 1 period/point at once. So they can't type "....." or "1..1" or "1.1.1" Really just stuff like that.... I would also like to prevent them from adding alphabetical characters in by typing them in with the keyboard, characters like "a, b, c".
I was told to use a MaskedTextBox, and I want to know开发者_如何学C if that is correct. Also, if it is correct, how would I implement it into my code? I'm a complete beginner when it comes to C#, so I would like some help (toned down for a beginner).
So far the code I have written is:
double total1 = 0;
double total2 = 0;
public Form1()
{
InitializeComponent();
}
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnThree.Text;
}
private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFour.Text;
}
private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFive.Text;
}
private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSix.Text;
}
private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
}
private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnEight.Text;
}
private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnNine.Text;
}
private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnZero.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
private void btnPlus_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
private void btnEquals_Click(object sender, EventArgs e)
{
total2 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Text = total2.ToString();
total1 = 0;
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void txtDisplay_TextChanged(object sender, EventArgs e)
{
}
}
So I ask... how/where do I add in this "MaskedTextBox" - if that is correct? How do I implement it? What makes it work?
Thanks!
You don't need a MaskedTextBox
, since you're simulating the keypad with your buttons. Just put something like this in btnPoint_Click
:
private void btnPoint_Click(object sender, EventArgs e)
{
if (!txtDisplay.Text.Contains("."))
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
}
In addition to MusiGenesis's snippet, you can use the KeyPress event of the text box to prevent non-digits and multiple periods.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsDigit(e.KeyChar) || ((e.KeyChar == '.' && textBox1.Text.IndexOf(".") < 0) ) )
{
textBox1.Text += e.KeyChar;
}
e.Handled = true;
}
or set ReadOnly
property of the text box to true
.
You can also save a big amount of code if you write the code of the click event just once and asign the click event handlers of every number buttons to it:
private void btnNumber_Click(object sender, EventArgs e)
{
if (sender is Button)
txtDisplay.Text = txtDisplay.Text + ((Button)sender).Text;
}
So you can save all of the following methods
private void btnZero_Click(object sender, EventArgs e) (...)
private void btnOne_Click(object sender, EventArgs e) (...)
.
.
.
private void btnNine_Click(object sender, EventArgs e) (...)
精彩评论