【问题标题】:Using a ComboBox selection as directory source for another ComboBox, C#使用 ComboBox 选择作为另一个 ComboBox 的目录源,C#
【发布时间】:2016-04-18 10:33:40
【问题描述】:

所以我创建了一个表单,该表单在C:\Modules 目录下创建了一个.txt 文件和一个具有相同名称的directory

我已经可以从ModuleSelectorComboBox 中选择.txt 文件,现在我开始工作时遇到的问题是使用在ModuleSelectorComboBox 中选择的文件的名称来构成目录名称的一部分在NoteSelectorComboBox

    public Default()
    {
        InitializeComponent();

        System.IO.Directory.CreateDirectory(@"C:\Modules");
        string[] files = Directory.GetFiles(@"C:\Modules");
        foreach (string file in files)
            ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
    }

    private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
    {
        NewModule newmodule = new NewModule();

        newmodule.Show();

    }

    private void ModuleSelectorComboBox_SelectedValueChanged(object sender, EventArgs e)
    {
        richTextBox1.Clear(); //Clears previous Modules Text
        string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
        string filePath = Path.Combine(@"C:\Modules\", ModulefileName + ".txt");

        if (File.Exists(filePath))
            richTextBox1.AppendText(File.ReadAllText(filePath));
        else
            MessageBox.Show("There's been a problem. Please restart the program. \nError 1", "Error 1", //error 1 is file deleted while the program is running
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation,
            MessageBoxDefaultButton.Button1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ModuleSelectorComboBox.Items.Clear();
        string[] files = Directory.GetFiles(@"C:\Modules");
        foreach (string file in files)
            ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
    }

    private void NoteSelectorComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
        string[] files = Directory.GetFiles(@"C:\Modules\" + ModulefileName); //THIS IS WHAT I HAVE TRIED BUT CANNOT GET TO WORK
        foreach (string file in files)
            NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
    }
}

因此,如果在ModuleSelectorComboBox 中选择了“模块 1”,则NoteSelectorComboBox 的目录列表将设置为C:\Modules\Module 1(即C:\Modules\<NAME OF SELECTED MODULE From ModuleSelectorComboBox>,因此该文件夹中的文件将显示在组合框中。

【问题讨论】:

  • 您说“//这是我尝试过但无法开始工作的方法”。这条线的输出是什么?你得到什么错误?
  • 我没有收到错误,它只是不能作为一种方法工作。
  • “不起作用”是什么意思?不是错误。所以它编译但没有达到你的预期? ModulefileName 中有什么内容?
  • 没有达到我的预期。 ModuleFileName 只是链接到 ModuleComboBox 中的选定项。
  • 对不起,我还是不明白。 SelectedItemobject 类型,使用 selectedItem.ToString() 你应该得到组合框的文本。

标签: c# winforms combobox directory


【解决方案1】:

我创建了一个 OnClick();现在已经解决了这个问题的事件。

private void button2_Click(object sender, EventArgs e)
    {
        string fileName = (string)ModuleSelectorComboBox.SelectedItem;
        NoteSelectorComboBox.Items.Clear();
        string[] files = Directory.GetFiles(@"C:\Modules\" + (string)ModuleSelectorComboBox.SelectedItem);
        foreach (string file in files)
            NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
    }

【讨论】:

    猜你喜欢
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多