【问题标题】:C# Multiplying label text by textbox input.C# 将标签文本乘以文本框输入。
【发布时间】:2016-11-19 17:20:20
【问题描述】:

我正在尝试将我的标签输入乘以输入到文本框中的数字,但一直遇到问题...如果文本框 3、4、5 中没有输入,我还试图保持标签值不变。例如,如果我将 3.20 放在文本框 3 中,它将使用新值更新标签 1 和 4,但不会将剩余标签保留为原始值。

非常失落...非常感谢任何帮助。

我想要完成的事情:

在这个阶段,用于输入加仑的文本框被激活,填充按钮也被激活。用户在文本框中输入加仑数并单击填充。 天然气订单的成本显示在另一个文本框中,仅用于显示。 将订单价格计算打包成一个函数。

价格按钮:在表单的下半部分显示一组新的控件,显示每个等级燃料的每加仑价格[这些是与上半部分的标签不同的新控件,并且是旨在向经理显示当前价格是多少],并带有用于为每个价格输入新价格的文本框,以及第三行中的两个按钮,更新和取消。更新将从文本框中读取的价格更新为每加仑价格的新值。 [请注意,这些新值应反映在表格上半部分燃料等级按钮下方显示的标签中。这些标签应始终显示当前价格,因此经理对价格所做的任何更改也应更改这些标签。] 取消不会改变任何内容。做出选择后,下半部分的控件又消失了。

    private int gasPrice = 0;
    private int gasPrice1 = 0;
    private int gasPrice2 = 0;
    private int numGallons = 0;
    private double total = 0;
    private bool regularButton;
    private bool premiumButton;
    private bool xtraButton; 

    public Form1 ( )
    {
        InitializeComponent();
    }

    private void regButton_Click ( object sender, EventArgs e )
    {
        regButton.BackColor = Color.Green;
        regButton.ForeColor = Color.Red;

        textBox1.Enabled = true;

        regularButton = true;
        xtraButton = false;
        premiumButton = false; 
    }

    private void extraButton_Click ( object sender, EventArgs e )
    {
        extraButton.BackColor = Color.Green;
        extraButton.ForeColor = Color.Red;

        textBox1.Enabled = true;

        regularButton = false;
        xtraButton = true;
        premiumButton = false;
    }

    private void premButton_Click ( object sender, EventArgs e )
    {
        premButton.BackColor = Color.Green;
        premButton.ForeColor = Color.Red;

        textBox1.Enabled = true;

        regularButton = false;
        xtraButton = false;
        premiumButton = true;
    }

    private void fillButton_Click ( object sender, EventArgs e )
    {
        //double price;
        int gallons;
        int price, price1, price2;

        gasPrice = Convert.ToInt32(label1.Text);  
        gasPrice1 = int.Parse(label2.Text);
        gasPrice2 = int.Parse(label3.Text);
        numGallons = int.Parse(textBox1.Text);
        //gallons = int.Parse(textBox1.Text);
        //price = int.Parse(label1.Text);
        //price1 = int.Parse(label2.Text);
        //price2 = int.Parse(label3.Text);


        if (regularButton == true)
        { total = gasPrice * numGallons; }
        else if (xtraButton == true)
        { total = gasPrice1 * numGallons; }
        else
        { total = gasPrice2 * numGallons; }

        textBox2.Text = total.ToString("c");
        textBox2.Visible = true; 

        if (textBox1.Text == "0")
        { MessageBox.Show("Operation Cancelled"); }
        ResetData();



    }

    private void finishButton_Click ( object sender, EventArgs e )
    {

        ResetData();





    }

    private void ResetData ( )
    {

        //Resets buttons back to default settings
        regButton.BackColor = default(Color);
        regButton.ForeColor = default(Color);
        extraButton.BackColor = default(Color);
        extraButton.ForeColor = default(Color);
        premButton.BackColor = default(Color);
        premButton.ForeColor = default(Color);

        //Clears out both text boxes
        textBox1.Clear();
        textBox2.Clear();

        //textboxes are returned to original state
        textBox1.Enabled = false;
        textBox2.Visible = false;




    }

    private void salesButton_Click ( object sender, EventArgs e )
    {

    }

    private void button1_Click ( object sender, EventArgs e )
    {
        // updates gas price if/when supervisor changes
        label1.Text = "$" + textBox3.Text;
        label2.Text = "$" + textBox4.Text;
        label3.Text = "$" + textBox5.Text;
        label4.Text = "$" + textBox3.Text;
        label5.Text = "$" + textBox4.Text;
        label6.Text = "$" + textBox5.Text;

    }

    private void pricesButton_Click ( object sender, EventArgs e )
    {
        label4.Visible = true;
        label5.Visible = true;
        label6.Visible = true;

        textBox3.Visible = true;
        textBox4.Visible = true;
        textBox5.Visible = true;

        updateButton.Visible = true;
        cancelButton.Visible = true;

【问题讨论】:

  • 您遇到了什么问题?请具体说明您需要什么帮助。
  • 我遇到了两个问题...1 试图将gasPrice * numGallons 相乘,我将gas 价格列为标签上的文本为3.70,它说不能将字符串转换为int。第二个问题是在文本框 3 中输入新值时,它会更新标签 1 和 4,但作为回报,标签 2、3、5、6 只保留 $ 而不是标签文本中列出的值。
  • 您的第一个问题是因为您试图将文本从标签转换为 int 并且它失败了,因为您可能有不是数字的字符。您的第二个问题是因为您在 button1_Click 中的代码正在使用 textbox3 更新标签 1 和 4,当然,如果显示这些标签的文本框中没有任何内容,其他标签将只有一个美元符号。
  • 所以我在文本字段中有“$3.60”,在其他字段中有类似的美元值。如何在标签本身中获取“$”,但仅将数字本身相乘以作为输出?
  • 这个答案对你有帮助吗?

标签: c# function integer


【解决方案1】:

选择您只想允许数字输入的文本框,在属性窗口中找到事件 (lightnig) 图标,然后双击 KeyPress 事件。这将生成一个处理程序。将此代码放入处理程序中:

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
    (e.KeyChar != '.'))
{
        e.Handled = true;
}

// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
    e.Handled = true;
}

该代码只允许用户在文本框中输入数字和小数点。

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多