I have a WinForm popping up within a WinForm. If I close the inner Winform and reopen it, the text in the textbox doesn't highlight. However if I close both WinForms and reopen the first and then the inner WinForm, the text in the textbox will highlight.
How can I get the inner WinForm text to highlight every time?
I have tried the 2 ways I know of / most common ways of highlighting the text but they seem to only work the first time around.
Any and all help/direction is appreciated.
The inner winforms is being called from a .ShowDialog();
in the outer Winforms.
portalTitleEntryForm.ShowDialog();
Here is my code of the inner Winform and what I have tried:
public partial class PortalTitleEntryForm : Form
{
public string portalEntryTitle;
public string dialogResult;
public PortalTitleEntryForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
titleTextBox.TabIndex = 0;
continueButton.TabIndex = 1;
cancelButton.TabIndex = 2;
this.CancelButton = cancelButton;
this.AcceptButton = continueButton;
titleTextBox.Focus();
if (portalEntryTitle != null || !portalEntryTitle.Equals(""))
{
titleTextBox.Focus();
titleTextBox.Text = portalEntryTitle;
/*titleTextBox.SelectionStart = 0; // doesn't work the second time
titleTextBox.SelectionLength = titleTextBox.Text.Length;*/ // doesn't work the second time
titleTextBox.SelectAll(); // doesn't work the second time
bool focusStatus = titleTextBox.Focused; // bool equals false the second
//titleTextBox.Focus(); // doesn't work the second time around
}
private void continueButton_Click(object sender, EventArgs e)
{
if (!titleTextBox.Text.Equals(""))
{
portalEntryTitle = titleTextBox.Text;
dialogResult = DialogResult.OK.ToString();
continueButton.DialogResult = DialogResult.OK;
Close();
return;
}
else if (titleTextBox.Text.Equals(""))
{
MessageBox.Show("Please give your entry a title.", "M3dida", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
dialogResult = DialogResult.Cancel.ToString();
Debug.WriteLine("Cancel button was clicked");
Close();
return;
}
}
}
Form loads only once, unless it is destroyed.
You might want to handle Form.Activated
event and invoke titleTextBox.Focus()
in the handler.