如何从数组中填充combobox ?

时间:2021-05-30 20:05:48

I a text file with names and phone numbers. My lecturer gave me a piece of code to split this into two arrays. Now I'm having trouble populating the name and phone number comboboxes with these arrays. All I get is errors saying array1 and array2 don't exist in the current context. How can I do this correctly?

我有一个带有姓名和电话号码的文本文件。我的讲师给了我一段代码把它分成两个数组。现在我在用这些数组填充姓名和电话号码时遇到了麻烦。我得到的是说array1和array2在当前上下文中不存在的错误。我怎么才能做对呢?

Here is the relevent part of my code;

这是我代码的相关部分;

public partial class MainWindow : Window { private string cFileName = "customer.txt"; private string[] cNames = new string[0]; private string[] cPhoneNumbers = new string[0];

公共部分类主窗口:窗口{私有字符串cFileName = "customer.txt";私有字符串[]cNames =新字符串[0];私有字符串[]cPhoneNumbers =新字符串[0];

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Window_Loaded);
    }

    private void read_Delimited_File(string fileName, ref string[] array1, ref string[] array2)
    {
        StreamReader fileSR = new StreamReader(fileName);

        char[] delimiters = { ',' };

        string[] tempArray = new string[0];
        string line = fileSR.ReadLine();
        while (line != null)
        {
            Array.Resize(ref array1, array1.Length + 1);
            Array.Resize(ref array2, array2.Length + 1);

            tempArray = line.Split(delimiters);

            array1[array1.Length - 1] = tempArray[0];
            array2[array2.Length - 1] = tempArray[1];
            line = fileSR.ReadLine();
        }
        fileSR.Close();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Keyboard.Focus(phoneTextBox);

        read_Delimited_File(cFileName, ref cNames, ref cPhoneNumbers);

        for (int i = 0; i < array1.Length; i++)
        {
            nameComboBox.Items.Add(array1[i]);
        }

        for (int i = 0; i < array2.Length; i++)
        {
            phoneNumberComboBox.Items.Add(array1[i]);
        }
    }

2 个解决方案

#1


0  

The variables array1 and array2 only exist inside your function scope.

变量array1和array2只存在于函数范围内。

You meant to use cNames and cPhoneNumbers.

你的意思是用cname和cPhoneNumbers。

#2


0  

I would fire a person who wrote code like you did in 2 seconds. Never resize arrays. Instead use list objects!!! Try this

我会解雇一个像你一样在2秒内写代码的人。没有调整数组。而不是使用列表对象! ! !试试这个

private void read_Delimited_File(string fileName, ref List<string> array1, ref List<string> array2)
        {
            StreamReader fileSR = new StreamReader(fileName);

            char[] delimiters = { ',' };

            string line = "";
            while ((line = fileSR.ReadLine()) != null)
            {
                string[] tempArray = line.Trim().Split(delimiters);
                array1.Add(tempArray[0]);
                array2.Add(tempArray[1]);
            }
            fileSR.Close();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(phoneTextBox);
            List<string> cNames = new List<string>();
            List<string> cPhoneNumbers = new List<string>();


            read_Delimited_File(cFileName, ref cNames, ref cPhoneNumbers);

            for (int i = 0; i < array1.Length; i++)
            {
                nameComboBox.Items.Add(array1[i]);
            }

            for (int i = 0; i < array2.Length; i++)
            {
                phoneNumberComboBox.Items.Add(array1[i]);
            }
        }​

#1


0  

The variables array1 and array2 only exist inside your function scope.

变量array1和array2只存在于函数范围内。

You meant to use cNames and cPhoneNumbers.

你的意思是用cname和cPhoneNumbers。

#2


0  

I would fire a person who wrote code like you did in 2 seconds. Never resize arrays. Instead use list objects!!! Try this

我会解雇一个像你一样在2秒内写代码的人。没有调整数组。而不是使用列表对象! ! !试试这个

private void read_Delimited_File(string fileName, ref List<string> array1, ref List<string> array2)
        {
            StreamReader fileSR = new StreamReader(fileName);

            char[] delimiters = { ',' };

            string line = "";
            while ((line = fileSR.ReadLine()) != null)
            {
                string[] tempArray = line.Trim().Split(delimiters);
                array1.Add(tempArray[0]);
                array2.Add(tempArray[1]);
            }
            fileSR.Close();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(phoneTextBox);
            List<string> cNames = new List<string>();
            List<string> cPhoneNumbers = new List<string>();


            read_Delimited_File(cFileName, ref cNames, ref cPhoneNumbers);

            for (int i = 0; i < array1.Length; i++)
            {
                nameComboBox.Items.Add(array1[i]);
            }

            for (int i = 0; i < array2.Length; i++)
            {
                phoneNumberComboBox.Items.Add(array1[i]);
            }
        }​