使用 resetText() 重新加载组合框

本文关键字:加载 组合 resetText 使用 新加载 | 更新日期: 2023-09-27 18:28:56

    private void ReloadForm()
    {
        comboBox4.ResetText();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        string layers = textBox1.Text;
        FileStream fs = new FileStream("xml/" + layers + ".xml", FileMode.Create);
        XmlWriter w = XmlWriter.Create(fs);
        w.WriteStartDocument();
        w.WriteStartElement("layers");
        // Write a product.
        w.WriteStartElement("layer");
        w.WriteAttributeString("id", "1");
        w.WriteElementString("layerName", layers);
        w.WriteEndElement();
        w.WriteEndDocument();
        w.Flush();
        fs.Close();
        ReloadForm();
    }
public Form3()
        {
            InitializeComponent();
            // Put XML name files in comboBox4
            string[] filePaths = Directory.GetFiles(@"xml'", "*");
            foreach (string file in filePaths)
            {
                string mypath = file;
                string[] directories = mypath.Split(Path.DirectorySeparatorChar);
                foreach (string dir in directories){
                        comboBox4.Items.Add(dir);
                }
            }
        }

上面的代码在单击时创建XML文件,我得到了显示每个XML文件名称的单独代码。我尝试使用void ReloadForm()刷新 comboBox4 文本,但它失败了。

有什么想法可以解决这个问题吗?

使用 resetText() 重新加载组合框

Form3构造函数更改为此

public Form3()
{
    InitializeComponent();
    ReloadComboBox4();
}

并将ReloadForm()重命名为 ReloadComboBox4 并将其更改为

private void ReloadComboBox4()
{
    comboBox4.Items.Clear()
    string[] filePaths = Directory.GetFiles(@"xml'", "*");
    foreach (string file in filePaths)
    {
        string mypath = file;
        string[] directories = mypath.Split(Path.DirectorySeparatorChar);
        foreach (string dir in directories)
        {
            comboBox4.Items.Add(dir);
        }
    }
}