Solution
I am now kicking myself over this, but you'll notice in the code below, that I am using a parameterized constructor for Form4. Standard procedure for a parameterized constructor in C# is to use :this()
after the declaration (I think, at least for Forms). This calls the unparameterized/default constructor first, which contains InitializeComponent()
, which sets up the form and its controls.
InitializeComponent()
should most definitely not be in the parameterized constructor if you have used :this()
, since it re-initializes your form with 'new' controls. This leads to an ambiguous state for your Form and controls, and the weird behavior I was getting.
Original Question
I have a form in which I would like the controls (including a textbox) to have initial values on first view. The values come from an SQL statement during the Form's construction, so I can't use the Form designer. This form's elements/controls were also copy-pasted from a nearly-identical form since that one is 'Add', and this one is 'Edit'
Problem is this: Using the debugger shows that I successfully get good data from the SQL, and that setting the textbox.Text
succeeds, but when the form is displayed, it does not reflect the changes made. The answer here: https://stackoverflow.com/a/7830769/1655707 implies that the value can simply be set (presumably during initialization or load).
I've tried doing it during initialization, in the Load event, and the Shown event, and none of those work. Calling Refresh()
and Application.DoEvents()
also did nothing. Is there something I am missing? Some event that prevents this textbox from updating/displaying properly? Did copy-pasting the controls have unintended side-effects?
None of the controls I try and set this way display the changed value, but textbox was the simplest since it does not have indices to mess with.
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 are setting different text boxes, but none of them work, so it makes little difference.
A typical textBox change handler. One might think that the way it's currently written might invoke some sort of resetting, but commenting out that one line does not change the behavior I'm concerned about.
private void textBox6_TextChanged_2(object sender, EventArgs e)
{
retCollName = textBox6.Text;
}