Can't display programmatically set value of Winforms textbox

Decision

Now I'm kicking myself over this, but you'll notice in the code below that I'm using a parameterized constructor for Form4. The standard procedure for a parameterized constructor in C # is to use :this()

after declaration (at least for forms, I think). This first calls the unparameterized / default constructor, which contains InitializeComponent()

that sets the form and its controls.

InitializeComponent()

should definitely not be in a parameterized constructor if you used :this()

, since it will reinitialize your form with "new" controls. This leads to an ambiguous state of your form and controls, as well as the strange behavior I was getting.

Original question

I have a form where I would like the controls (including the textbox) to have initial values ​​on first submission. The values ​​are taken from the SQL statement during the construction of the form, so I cannot use the form constructor. Form elements / elements were also copied from an almost identical form, since this is "Add" and this is "Edit"

The problem is this: Using the debugger shows that I am successfully getting good data from SQL and that the setup textbox.Text

succeeds, but when the form is displayed it does not reflect the changes I made. The answer here: https://stackoverflow.com/a/1989283/ means the value can just be set (presumably during initialization or loading).

I've tried doing this during initialization, on Load event and Shown event, and none of them work. The call Refresh()

and Application.DoEvents()

also did nothing. Is there something I am missing? Some event that prevents this textbox from updating / displaying correctly? Were the copied controls had unintended side effects?

None of the controls I'm trying to tweak this way display the changed value, but the text box was the simplest since it doesn't contain any indexes to link to.

    public Form4(int collIDFromParent, string collNameFromParent): this()
    {
        InitializeComponent();

        retCollID = collIDFromParent;
        retCollName = collNameFromParent;

        //Initialize these here so they activate on first use0
        button1.DialogResult = DialogResult.None;
        button2.DialogResult = DialogResult.Cancel;

        //PopulateEditData goes first for potential SQL failure
        PopulateEditData();

        textBox6.Text = "TEST";
    }

    private void Form4_Load_1(object sender, EventArgs e)
    {
        textBox1.Text = "TEST";
    }

    private void Form4_Shown_1(object sender, EventArgs e)
    {
        textBox2.Text = "TEST";
    }

      

And yes, they set different text boxes, but none of them work, so it doesn't really matter.

Typical textBox change handler. One would think that the way it is currently written might cause some sort of reset, but comments that one line doesn't change the behavior I'm worried about.

    private void textBox6_TextChanged_2(object sender, EventArgs e)
    {
            retCollName = textBox6.Text;
    }

      

+3


source to share


3 answers


With simple changes between functions (such as Add or Modify), the basic functionality of the form will not change. The only major difference between the two functions you are trying to handle is the new object and the existing object. Because of this, one shape would be much better than two separate shapes. It also saves a lot of work and is one of the gadgets of OOP.

Another thing I would suggest would be to get the data before creating the form. Create a data object that contains all of your properties that you retrieve from the database. Give this data class a Load method that accepts whatever identifier you are looking for. You are now in the mood for an easier time.

I'll use some of the default buttons for this example, your experience may be different. :)

AddButton_Click(//default button click parameters) {
    Widget widget = new Widget();
    OpenWidgetForm(widget);
}

EditButton_Click(//default button click parameters) {
    Widget widget = new Widget();
    // You'll use the way you have now to determine which record to load,
    // and turn it to the id variable
    widget.Load(id);
    OpenWidgetForm(widget);
}

OpenWidgetForm(Widget widget) {
    frmWidget frm = new frmWidget(widget);
    frm.Show();
}

      



This code will be in whatever form you have, which will lead you to the add / edit forms.

Now in the Add / Edit form you will change it like this:

private Widget _widget;

public frmWidget() {
    Widget widget = new Widget();
    _widget = widget;
    LoadWidget();
}

public frmWidget(Widget widget) {
    _widget = widget;
    LoadWidget();
}

public void LoadWidget() {
    // go through each of the properties of your object
    // and set the values to the Text properties of each Textbox.
    txtName.Text = _widget.Name;
}

      

With that, you should be good to go. Let me know if you have any questions.

+1


source


I made a quick project to test this functionality. All three of these options successfully set the TextBox value. You have a problem that is not shown in the posted code. Either something changes the values ​​of the TextBox, or refers to the wrong text boxes. I just managed to debug a similar issue with our application. It turned out that we somehow had two overlapping sets of TextBoxes (the ones we actually used were under the set of empty text boxes).



    public Form1()
    {
        InitializeComponent();

        textBox1.Text = "TEST";
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox2.Text = "TEST";
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        textBox3.Text = "TEST";
    }

      

0


source


This may be a more trivial answer than is required for this problem, but make sure you are actually calling Controls.Add(textBox1)

. I noticed that no one has mentioned this yet. First check the generated constructor code.

You will have to call it yourself if you are adding controls dynamically.

0


source







All Articles