Citizen Jane
Citizen Jane

Reputation: 25

Saving User Input in a Dynamically Created Form

I'm trying to get the values that a user inputs in a textbox in a dynamically generated form. Another method loads and parses an XML file and creates an object that has specific getters and setters for the settings that it finds in the file (Server, Port, Title, etc.).

The dynamic form is created using labels and textboxes like this. It was designed only to display the information from the XML file, and I am trying to implement a system that allows users to edit the information before saving it again to the file. I'm doing alright with the methods to save and edit the XML file, but I'm lost for how to associate the input in any given textbox with the associated label representing the key in the XML file to change.

Below is the current form implementation where the labels and textboxes are created as part of a foreach loop. I tried creating textbox.Leave eventHandler to track when the user has finished changing a value, but I can't figure out how to know what label it is associated with.

var sortedSettings = new SortedDictionary<string, string>(theSettings.Settings);

int numSettings = sortedSettings.Count;

TextBox[] txt = new TextBox[numSettings];
Label[] label = new Label[numSettings];

int labelSpacing = this.labelSecond.Top - this.labelTop.Bottom;
int textSpacing = this.textBoxSecond.Top - this.textBoxTop.Bottom;

int line = 0;
    foreach (KeyValuePair<string, string> key in sortedSettings)
    {
        label[line] = new Label();
        label[line].Text = key.Key;
        label[line].Left = this.labelTop.Left;
        label[line].Height = this.labelTop.Height;
        label[line].Width = this.labelTop.Width;

        txt[line] = new TextBox();
        txt[line].Text = key.Value;
        txt[line].Left = this.textBoxTop.Left;
        txt[line].Height = this.textBoxTop.Height;
        txt[line].Width = this.textBoxTop.Width;
        txt[line].ReadOnly = false;
        // Attach and initialize EventHandler for template textbox on Leave
        txt[line].Leave += new System.EventHandler(txt_Leave);

        if (line > 0)
        {
            label[line].Top = label[line - 1].Bottom + labelSpacing;
            txt[line].Top = txt[line - 1].Bottom + textSpacing;
        }
        else
        {
            label[line].Top = this.labelTop.Top;
            txt[line].Top = this.textBoxTop.Top;
        }


        this.Controls.Add(label[line]);
        this.Controls.Add(txt[line]);

        line++;
 }


private void txt_Leave(object sender, EventArgs e)
{
    String enteredVal = sender;
    FormUtilities.FindAndCenterMsgBox(this.Bounds, true, "EventChecker");
    MessageBox.Show("The current value of LABEL is " + enteredVal, "EventChecker");
}

Upvotes: 1

Views: 637

Answers (1)

Hassan
Hassan

Reputation: 5430

One option is to use TextBox.Tag property.

Example (in foreach loop):

txt[line] = new TextBox();
txt[line].Text = key.Value;
txt[line].Tag = label[line];

To get label associated with TextBox:

TextBox t = txt[0];
Label l = t.Tag as Label;

//Here is how you identify textbox which generated the event.
private void txt_Leave(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    //..
}

Upvotes: 1

Related Questions