每次单击不同的按钮时,将数组文件中的项添加到一个列表框中 [英] Add an item from an array file to one listbox each time a different button is clicked

查看:55
本文介绍了每次单击不同的按钮时,将数组文件中的项添加到一个列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每个按钮显示与桌面上的阵列文件不同的一行,并计算阵列中显示的价格总数到位于列表框下方的文本框中,以及一个指定为初始值的进度条价值



i需要一个答案



提前谢谢。



我的尝试:



i want each button to display a different line from the array file that is located on the desktop and calculate the total of prices presented in the array into the textbox located under the listbox and a progress bar located that is assigned as an initial value

i need an answer asap

thank you in advance.

What I have tried:

double result;
        string r;
        string[] myArray;

        private void listInvoice()
        {
            try
            {
                string path = @"d:\\data.txt";
                using (StreamReader sr = new StreamReader(path))
                {
                    string s;
                    while ((s = sr.ReadLine()) != null)
                    {
                        myArray = s.Split('|');
                    }

                }
            }

            catch (Exception error)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(error.Message);
            }

        }
        private void getTotal()
        {
            int count = lstInvoice.Items.Count;
            for(int i =0; i<count;i++)
            {
                r = lstInvoice.Items[i].ToString();
                string[] table = r.Split(',');
                if (rdbTVAYes.Checked == true)
                    result += (Convert.ToDouble(table[1]) * Convert.ToDouble(table[2])) * 1.1;
                else
                    result += (Convert.ToDouble(table[1]) * Convert.ToDouble(table[2]));
            }
            txtTotal.Text = result.ToString();
        }

推荐答案

Quote:

我需要一个答案asap

i need an answer asap



我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!提示:首先看看你在做什么:你的 myArray 只保存文件中最后一行文本的分割值......所以先考虑一下哪些数据您需要存储,然后考虑如何存储它。



如果您遇到特定问题,请询问相关问题,我们会尽力帮助您。但是我们不打算为你做这一切!


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! Hint: start by looking at what you are doing: your myArray holds the split values from just the last line of text in the file ... so start by thinking about what data you need to store, and then think about how to store it.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


你可能会对ListBox控件的 MultiColumn 属性感到困惑,这只适用于布局目的。

如果你想要真正的列,你可以使用 ListView 控件或DataGridView。

You are probably confused by the The MultiColumn property of the ListBox control, this only serves for layout purposes.
If you want real columns you can use the ListView control or the DataGridView.
using System;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form21invoice : Form
    {
        public Form21invoice()
        {
            InitializeComponent();
            listInvoiceInit();
        }

        private void listInvoiceInit()
        {
            lstInvoice.View = View.Details;
            // Assume columns are defined in the designer
            //lstInvoice.Columns.Add("A");
            //lstInvoice.Columns.Add("B");
            ListViewItem lvi = new ListViewItem(new[] { "2", "10.2"});
            lstInvoice.Items.Add(lvi);
        }

        private void getTotal()
        {
            double result = 0;

            foreach (ListViewItem item in lstInvoice.Items)
            {
                string a = item.SubItems[0].Text;
                string b = item.SubItems[1].Text;
                result += Convert.ToDouble(a) * Convert.ToDouble(b);
            }

            txtTotal.Text = result.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getTotal();
        }
    }
}


这篇关于每次单击不同的按钮时,将数组文件中的项添加到一个列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆