你能用 C# 代码帮助我吗,其中一个 + 按钮将触发列表框 1 和列表框 2 中的总和我想要 2 个列表的 * 值[关闭]

问题描述 投票:0回答:0
private void btn_mais_o_Click(object sender, EventArgs e)
{            
    try
    {
        if (qto.SelectedIndex >= 0 && pro.SelectedIndex >= 0)
        {
            int index = qto.SelectedIndex;
            int vNumMais = Convert.ToInt16(qto.Items[index]);
            int vMaisUm = vNumMais + 1;
            qto.Items[index] = vMaisUm.ToString();

            string cpro = pro.Items[index].ToString();
            string spro = Regex.Replace(cpro, "[^0-9.,]+", "");

            string sprov = spro.Replace(".", ",");

            double vpro = Convert.ToDouble(sprov);
            double rpro = vpro * vMaisUm;

            pro.Items[index] = rpro.ToString("0.00") + " €";
        }
        else
        {
            MessageBox.Show("Selecione um item em ambas as listas.");
        }
    }
    catch (FormatException)
    {
        MessageBox.Show("Erro! Verifique o valor introduzido.");
    }
}

我尝试使用数组来复制 listbox2 的原始值,但结果是一样的,你能帮帮我吗?

double[] vproi = new double[pro.Items.Count];
double vpro;

try
{
    for (int i = 0; i < pro.Items.Count; i++)
    {
        string cpro = pro.Items[i].ToString();
        string spro = Regex.Replace(cpro, "[^0-9.,]+", "");
        string sprov = spro.Replace(".", ",");
        vpro = Convert.ToDouble(sprov);
        vproi[i] = vpro;
    }

    if (qto.SelectedIndex >= 0 && pro.SelectedIndex >= 0)
    {
        int index = qto.SelectedIndex;
        int vNumMais = Convert.ToInt16(qto.Items[index]);
        int vMaisUm = vNumMais + 1;

        double vproinic = vproi[index];
        double rpro = vMaisUm * vproinic;

        pro.Items[index] = rpro.ToString("0.00").Replace(".", ",") + " €";
        qto.Items[index] = vMaisUm.ToString();                  
    }
    else
    {
        MessageBox.Show("Selecione um item em ambas as listas.");
    }
}
catch (FormatException)
{
    MessageBox.Show("Erro! Verifique o valor introduzido.");
}
c# object button listbox multiplication
© www.soinside.com 2019 - 2024. All rights reserved.