.NET 中的数字分组

发布于 2024-08-23 23:38:11 字数 341 浏览 9 评论 0原文

C#中如何输入数字分组?我的代码有效,但仅适用于一个实例。我必须不断点击它才能对计算器中的每个数字进行分组。我们如何对其进行分组,以便如果我们单击它,它将对每个数字(而不仅仅是显示的数字)进行分组,而如果我们取消选中该复选框,则不会分组?

这是当前的代码:

NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat;


double int_value = Convert.ToDouble(textboxt1.text);


textbox1.Text = int_Value.ToString("N", nFI);

How do we input digit grouping in C#? My code works but only for one instance. I have to constantly click it to group every number in the calculator. How do we do group it so that if we click it, it groups every number (not only the number presented), and if we uncheck the checkbox, it doesn't?

This is the current code:

NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat;


double int_value = Convert.ToDouble(textboxt1.text);


textbox1.Text = int_Value.ToString("N", nFI);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-08-30 23:38:11

听起来您在错误的位置调用了格式。除了单击复选框(即,每当您单击计算器时),当您的值发生变化时,您可能还想调用 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 中或无论如何,调用该私有函数即可。

Sounds like you're calling the formatting in the wrong place. You probably want to call that textbox1.Text = int_Value.ToString("N", nFI); when your value is changing in addition to when you click the checkbox (ie, whenever you click on your calculator buttons or on textboxt1.TextChanged or whatever), and only if your checkbox is checked (if (checkbox1.Checked == true) textbox1.Text...). It should also probably be in a separate function, that's called in all the places it's needed.

Edit for clarity

The problem you're likely having is that you have multiple sources that are changing the display in textbox1. The code you posted is most likely in some checkbox_CheckChanged(sender, e) event handler.

You likely also have code elsewhere (maybe calculator buttons or something) that will change the value in your textbox (something like...

double value = Convert.ToDouble(textbox.Text);
value = value + 1;
textbox1.Text = value.ToString();

in a +1 button, for example?)

What you want to do is have a separate displayValue(double value) function that will format it correctly all the time, maybe something like...

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();
}

and every place that you would have set textbox1.Text in your code, regardless of whether it's in a button or a _CheckChanged or whatnot, instead call that private function.

夜司空 2024-08-30 23:38:11

好的,这就是代码:

using System;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用系统数据;
使用系统绘图;
使用系统文本;
使用 System.Windows.Forms;
使用系统全球化;

命名空间_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
        {

        }
     }



    }

}

我们应该把它放在哪里?

Okay, so this is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace _3rdQuarterExam_ADLM
{
public partial class Form1 : Form
{

    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
        {

        }
     }



    }

}

Where should we place it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文