Mathew Robinson
Mathew Robinson

Reputation: 5

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.

Upvotes: 0

Views: 48

Answers (1)

quest4truth
quest4truth

Reputation: 1140

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.

Upvotes: 2

Related Questions