简体   繁体   中英

How can I get the text from a texbox that is dynamically made?

//Assessment type combobox
comboBoxAssessments.Add(new ComboBox());
System.Drawing.Point pCombo = new System.Drawing.Point(450, 25 + i * 75);
(comboBoxAssessments[i] as ComboBox).Location = pCombo;
(comboBoxAssessments[i] as ComboBox).Size = new System.Drawing.Size(100, 20);
(comboBoxAssessments[i] as ComboBox).Items.AddRange(new object[]{
     "Coursework",
     "Exam"
});

I'm working on a grade prediction tool for a university degree that has to have dynamically built controls. The code above dynamically builds a combobox to let the user choose from "Coursework" or "Exam" assessment types. "comboBoxAssessments" is an arrayList that holds any given number of comboBoxes. Once I have the user's selection, I'm saving it into an XML file using the XMLTextWriter class. How can I go about getting the user's selection from the combobox during runtime and then write it to an XML file? I've tried using:

textWriter.WriteStartElement("Assessment Type", "");                        //Assessment type start
textWriter.WriteString((comboBoxAssessments[i] as ComboBox).SelectedItem.ToString);
textWriter.WriteEndElement();                                               //Assessment type end 

but found no glory. If anyone has some ideas, please throw them my way and I'll try them out.

It's just comboBoxAssessments.Text . Use this for your second line:

textWriter.WriterString(comboBoxAssessments[i].Text);

The Text property is already a string, and will be the value of the user's selection.

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