简体   繁体   中英

C# Add 2 comboBox to csv text file

Am close with this but it still failing. Just adds combobox1 item many times then changes. Combobox2 gets added correctly for each entry of cbo1. Also adds the delimiter , at front of line in MyFile.txt I load this with a split no problem, going back is the trouble I'm like new real new C#.

StreamWriter OutFile = new StreamWriter("MyFile.txt",false);

foreach(object L in comboBox1.Items)
foreach(object M in comboBox2.Items)
{
    string lineoftext1 = (L.ToString());

    string lineoftext2 = (",");

    string lineoftext3 = (M.ToString());

    string joinedText;
    joinedText = String.Join(lineoftext1, lineoftext2, lineoftext3);
    //MessageBox.Show(joinedText);

    OutFile.WriteLine(joinedText);
}

OutFile.Close();

I am not sure , i think in your combobox1, same items are added when a post back is done, so it repeats back to combobox1 even any changes happens in combobox2. If this is your question, you can overcome this by adding your code inside, if (!IsPostBack) , something like this,

 protected void Page_Load(object sender, EventArgs e)
 {

    if (!IsPostBack)
    {
      //write you code here 
    }
 }

OOOOH, i see the problem, it's in the way you are handling your strings, you don't use string.join that way, you can either join them explicitly like this:

joinedText = lineoftext1 + lineoftext2 + lineoftext3;

or you need to create something that the join method accepts, i would use a list, so the inside of your foreach loop would look like this

List<string> mystrings = new List<string>();
mystrings.add(L.ToString());
mystrings.add(M.ToString());

joinedText = String.Join(",", mystrings);

alternativly you could leave it how you have it, but change some values around

joinedText = String.Join(lineoftext2, lineoftext1, lineoftext3);

The first parameter passed to String.Join is the seperator. you were passing it the text.

If this doesn't help, then i would want to see your expected output as i mentioned in my comments above.

Edit: Try this if you want a 1-1 2-2 style

int i = 0;

foreach (object M in comboBox1.Items)
        {

            List<string> mystrings = new List<string>();

            mystrings.Add(comboBox2.Items[i].ToString());
            mystrings.Add(M.ToString());

            OutFile.WriteLine(String.Join(",", mystrings));
            i++;

        }

just be aware, this will error if there are different numbers of items in each combobox. (you will get an outside array bounds error)

Nikerym you got it man I just added combobox1 below 129 lines as was the original Thanks M8

        StreamWriter OutFile = new StreamWriter("MyFile.txt", false);

        int i = 0;

        foreach (object M in comboBox1.Items)
        {

            List<string> mystrings = new List<string>();
            mystrings.Add(comboBox1.Items[i].ToString());
            mystrings.Add(comboBox2.Items[i].ToString());


            OutFile.WriteLine(String.Join(",", mystrings));
            i++;

        }

        OutFile.Close();

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