Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c# - Can I make a dynamic combo Box from a text file?

I want to make a combo box like this:

enter image description here

But the boxes should not be hardcoded they should come from a text file like this:

enter image description here

Addition of data in text file should result in addition of combo Boxes. Also each comboBox should have the same list of options in it which are 1,2,3,4

I made the following class to read and write the text file, but I couldn't find any resources in the internet to turn these text files to combo Box.

public static string ReadFromTextFile(string path)
        {
            if (File.Exists(path))
            {
                string data;
                using (StreamReader r = new StreamReader(path))
                {
                    data = r.ReadToEnd();

                }
                if (data != "")
                {
                    data = "[" + data + "]";
                }
                return data;
            }
            return null;

   }
public static void WriteToTextFile(string path, string data, bool append = true, int count = 1)
        {
            if (!File.Exists(path))
            {
                var file = File.Create(path);
                file.Close();
            }
            using (StreamWriter writer = new StreamWriter(path, append: append))
            {
                if (!append)
                {
                    //remove opening bracket "[" from data passed
                    data = data.Trim().Substring(1, data.Trim().Length - 1);
                    //remove last bracket "]" from data passed
                    data = data.Trim().Substring(0, data.Trim().Length - 1);
                }
                if (count != 0)
                {
                    data = data + ",";
                }
                writer.WriteLine(data);
            }
        }
 public static DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            if (data != null)
            {
                foreach (T item in data)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
            }
            return table;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the string "Food"/"Water" as the Name property of the ComboBox to identify the each ComboBox.

Besides, note that should set a different Location for each ComboBox.

private void buttonCreateComboBox_Click(object sender, EventArgs e)
{
    int locationX = 50;
    int locationY = 10;

    string line;
    System.IO.StreamReader file =
        new System.IO.StreamReader(@"C:UsersAdministratorDesktopest.txt");
    while ((line = file.ReadLine()) != null)
    {
        // Remove the extra ','
        string comboName = line.Substring(0, line.Length - 1);

        ComboBox comboBox = new ComboBox();
        comboBox.Name = comboName;
        comboBox.Items.AddRange(new object[] { 1, 2, 3, 4 });
        comboBox.Location = new Point(locationX, locationY);
        this.Controls.Add(comboBox);

        Label label = new Label();
        label.Text = comboName;
        label.Location = new Point(0, locationY);
        this.Controls.Add(label);

        locationY += 30;
    }

    file.Close();
}

If you want to access a specific ComboBox, you can call Control.ControlCollection.Find(String, Boolean) Method to get it.

private void buttonGetComboWaterText_Click(object sender, EventArgs e)
{
    ComboBox comboWater = (ComboBox)this.Controls.Find("Water", true)[0];
    MessageBox.Show(comboWater.Text);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...