使用resetText()重新加载comboBox

时间:2014-11-02 12:02:56

标签: c# .net winforms combobox

    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文本,但它失败了..

任何想法如何解决?

1 个答案:

答案 0 :(得分:1)

将您的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);
        }
    }
}