c# - 如何从数组填充组合框?

标签 c# arrays wpf combobox split

我是一个包含姓名和电话号码的文本文件。我的讲师给了我一段代码,将其分成两个数组。现在我在用这些数组填充姓名和电话号码组合框时遇到了问题。我得到的只是错误提示 array1 和 array2 在当前上下文中不存在。我怎样才能正确地做到这一点?

这是我的代码的相关部分;

公共(public)部分类 MainWindow : Window { 私有(private)字符串 cFileName = "customer.txt"; 私有(private)字符串[] cNames = 新字符串[0]; private string[] cPhoneNumbers = new string[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]);
        }
    }

最佳答案

变量 array1array2 只存在于你的函数范围内。

您打算使用 cNamescPhoneNumbers

关于c# - 如何从数组填充组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827851/

相关文章:

c# - 创建一个表示两个其他对象之间的值差异的对象

c# - WPF 使用控件附加弹出窗口

c# - 使用 lambda 将带有列表的元素列表转换为单个列表

c++ - 多站点执行 : Getting rid of virtual table with inheritance (legacy code)

c# - 自定义 WPF MessageBox 未返回结果

java - 在 Android 开发中何时使用 Bitmap 与 Byte[]?

javascript - 循环对象数组中的属性的最有效方法是什么?

python - 将列表中的数字放置到元素不是 np.nan 的数组中

c# - 如何在不违反 MVVM 的情况下绑定(bind)到不可绑定(bind)的属性?

c# - WPF中使用 "Binding with StaticResource"和使用 "StaticResource directly"有什么区别