我们如何在C#中输入数字分组?我的代码可以工作但只适用于一个实例。我必须不断点击它来对计算器中的每个数字进行分组。我们如何对它进行分组,以便如果我们点击它,它会对每个数字进行分组(不仅仅是显示的数字),如果我们取消选中该复选框,它就不会?
这是当前的代码:
NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat;
double int_value = Convert.ToDouble(textboxt1.text);
textbox1.Text = int_Value.ToString("N", nFI);
答案 0 :(得分:2)
听起来你在错误的地方调用格式。除了单击复选框(即,每当您点击计算器按钮或textbox1.Text = int_Value.ToString("N", nFI);
或其他任何内容)时,您可能希望在值发生变化时调用textboxt1.TextChanged
,并且仅在您选中复选框时已选中(if (checkbox1.Checked == true) textbox1.Text...
)。它也应该在一个单独的函数中,在所需的所有位置调用。
为了清晰起见而编辑
您可能遇到的问题是您有多个来源正在更改textbox1
中的显示内容。您发布的代码最有可能出现在某个checkbox_CheckChanged(sender, e)
事件处理程序中。
您可能还有其他地方的代码(可能是计算器按钮或其他内容)会改变文本框中的值(类似......
double value = Convert.ToDouble(textbox.Text);
value = value + 1;
textbox1.Text = value.ToString();
在+1按钮中,例如?)
你想要做的是有一个单独的displayValue(double value)
函数,它会一直正确地格式化它,可能就像......
private void SetDisplayValue(double value)
{
NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat
if (checkBox.Checked == true)
textbox1.Text = value.ToString("N", nFI);
else
textbox1.Text = value.ToString();
}
以及在代码中设置textbox1.Text的每个地方,无论是在按钮中还是在_CheckChanged或whatnot中,都会调用该私有函数。
答案 1 :(得分:0)
好的,这就是代码:
使用System; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Text; 使用System.Windows.Forms; 使用System.Globalization;
namespace _3rdQuarterExam_ADLM { 公共部分类Form1:表格 {
public Form1()
{
InitializeComponent();
}
double x;
double y;
string operation;
double memory;
string current;
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button1.Text;
}
else
{
textBox1.Text += button1.Text;
}
}
private void button13_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button13.Text;
}
else
{
textBox1.Text += button13.Text;
}
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button6.Text;
}
else
{
textBox1.Text += button6.Text;
}
}
private void button16_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button16.Text;
}
else
{
textBox1.Text += button16.Text;
}
}
private void button9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button9.Text;
}
else
{
textBox1.Text += button9.Text;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button3.Text;
}
else
{
textBox1.Text += button3.Text;
}
}
private void button10_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button10.Text;
}
else
{
textBox1.Text += button10.Text;
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button15.Text;
}
else
{
textBox1.Text += button15.Text;
}
}
private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button8.Text;
}
else
{
textBox1.Text += button8.Text;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button4.Text;
}
else
{
textBox1.Text += button4.Text;
}
}
private void Plus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
x = Convert.ToDouble(textBox1.Text);
operation = Plus.Text;
}
else
{
x = Convert.ToDouble(textBox1.Text);
operation = Plus.Text;
textBox1.Text = "";
}
}
private void MINUS_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
x = Convert.ToDouble(textBox1.Text);
operation = MINUS.Text;
}
else
{
x = Convert.ToDouble(textBox1.Text);
operation = MINUS.Text;
textBox1.Text = "";
}
}
private void Times_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
x = Convert.ToDouble(textBox1.Text);
operation = Times.Text;
}
else
{
x = Convert.ToDouble(textBox1.Text);
operation = Times.Text;
textBox1.Text = "";
}
}
private void DIVIDE_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
x = Convert.ToDouble(textBox1.Text);
operation = DIVIDE.Text;
}
else
{
x = Convert.ToDouble(textBox1.Text);
operation = DIVIDE.Text;
textBox1.Text = "";
}
}
private void button24_Click(object sender, EventArgs e)
{
y = Convert.ToDouble(textBox1.Text);
if (operation == Plus.Text)
{
textBox1.Text = Convert.ToString(x + y);
}
else if (operation == MINUS.Text)
{
textBox1.Text = Convert.ToString(x - y);
}
else if (operation == Times.Text)
{
textBox1.Text = Convert.ToString(x * y);
}
else if (operation == DIVIDE.Text)
{
textBox1.Text = Convert.ToString(x / y);
}
}
private void button25_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
private void button27_Click(object sender, EventArgs e)
{
textBox1.Text = "0.";
}
private void button26_Click(object sender, EventArgs e)
{
textBox1.Text = "0.";
operation = Convert.ToString(ConsoleCancelEventArgs.Empty);
}
private void button21_Click(object sender, EventArgs e)
{
x = Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(Math.Sqrt(x));
}
private void button19_Click(object sender, EventArgs e)
{
x = Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(1 / x);
}
private void button20_Click(object sender, EventArgs e)
{
y = Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(x * (y / 100));
}
private void button2_Click(object sender, EventArgs e)
{
x = Convert.ToDouble(textBox1.Text);
if (textBox1.Text == "0.")
{
textBox1.Text = textBox1.Text;
}
else
{
textBox1.Text = Convert.ToString(-1 * x);
}
}
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0.")
{
textBox1.Text = button5.Text;
}
else
{
textBox1.Text += button5.Text;
}
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
current = textBox1.Text;
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Paste(current);
}
private void aboutUToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void button18_Click(object sender, EventArgs e)
{
memory = 0;
textBox1.Text = "0.";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(memory);
}
private void button17_Click(object sender, EventArgs e)
{
memory = 0;
memory += Convert.ToDouble(textBox1.Text);
}
private void button11_Click(object sender, EventArgs e)
{
memory += Convert.ToDouble(textBox1.Text);
}
private void digitGroupingToolStripMenuItem_Click(object sender, EventArgs e)
{
if (digitGroupingToolStripMenuItem.Checked)
{
NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat;
double int_value = Convert.ToDouble(textBox1.Text);
textBox1.Text = int_value.ToString("N", nFI);
}
else
{
}
}
}
}
我们应该把它放在哪里?