简体   繁体   中英

C# Combobox not populating from list correctly

I have a combo box that I am attempting to populate from "Settings" AFTER the data has been edited by a user. I am running into 1 of two weird issues.

Issue 1.) If I write the code like this:

var list = Properties.UserSpecific.Default.webCombo.Cast<string>().ToList(); comboBox7.Items.Add(list);

I end up getting this:

在此处输入图片说明

So I tried writing it like this:

        string[] webDropDown = Properties.UserSpecific.Default.webCombo.Cast<string>().ToArray();
        comboBox7.DataSource = webDropDown;

But it is giving me my data all on 1 line.

在此处输入图片说明

As far as I can tell the data isn't on 1 line. I can write to a richtextbox and it shows up on individual lines. So I am not sure what I am doing wrong. I have other lines of code:

comboBox1.DataSource = Globals.combo1;

Where I don't have this issue at all. Best I can tell the issue is happening because I am attempting to allow for a User to be able to Customize a Systems.Collections.Specialized.StringCollection in my settings page. I am accomplishing this by using this code to update:

            List<string> display = new List<string>();
        display.Add(richTextBox1.Text);
        Properties.UserSpecific.Default.webCombo.Clear();
        foreach(string str in display)
        {
            Properties.UserSpecific.Default.webCombo.Add(str);
        }
        //Save All Changes
        Properties.UserSpecific.Default.Save();

I am displaying this back using:

            //Populate DropDown Box For Websites
        var list = Properties.UserSpecific.Default.webCombo.Cast<string>().ToList();
        foreach (string str in list)
        {
            richTextBox1.Text += str;
        }

Clearly I am doing something wrong, but I can't figure it out.

string[] webDropDown = Properties.UserSpecific.Default.webCombo.Cast<string>().ToArray();
comboBox7.Items.AddRange(webDropDown);

The following lines of code is creating the issue.

List<string> display = new List<string>();
display.Add(richTextBox1.Text);

It will add the entire text as a single item.

Try something like below.

 List<string> display = new List<string>();
 display = richTextBox1.Text.Split('\n').ToList<string>();

Add is for items themselves

Try

foreach(string item in list)
{
    comboBox7.Items.Add(item);
}

(Posted the solution on behalf of the question author to move it to the answer space).

Ken and Ajith below got me pointed in the right direction. I ended up following Ken's advice and using:

           string[] webDropDown = Properties.UserSpecific.Default.webCombo.Cast<string>().ToArray();
        comboBox7.Items.AddRange(webDropDown);

I also had to follow Ajith's advice and make changes:

display = richTextBox1.Text.Split('\n').ToList<string>();

As well as a change to the way I was displaying the content back to my Editor:

        var list = Properties.UserSpecific.Default.webCombo.Cast<string>().ToList();
        foreach (string str in list)
        {
            richTextBox1.Text += str + Environment.NewLine;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM