问题
I'm making a simple Notepad program in C# and in my main form I have a textbox which experiences some weird things, I have a DELETE in my Edit menu on_click I have an event
txtContent.text = "";
Also Tried
txtContent.Text = string.Empty;
And
txtContent.Clear();
but every time after that operation my Caret disappears, I thought it might be a focus issue but it is not I tried that too. Accidentally I minimized my notepad and than opened it again Cursor came back and it doesn't disappears after DELETE operation I searched the web for this issue but couldn't find anything hope you have some suggestions
Here is my Complete code, this might bring some more clarity to the issue
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Clear();
time.Interval = 20000;
time.Enabled = true;
time.Start();
time.Tick+=focus;
}
void focus(object sender, EventArgs e)
{
textbox1.Focus();
}
回答1:
This is because when the user selects the menu item, the menu now has the focus, so there is no point in showing the cursor within the textbox.
If you want the cursor to be shown, you will need to set focus back on the textbox after the menu operation completes.
For example (using a button):
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Focus();
}
However, note that this could get a little tricky depending on the behavior of the menu. If focus doesn't return to the textbox, you may need to add a timer to set the focus so that the menu can finish whatever it is doing and not "re-steal" the focus.
回答2:
Have you tried using Form.invalidate() or other methods that also refresh the form? You could also return focus to the textbox.
If those do not work, it is possible to code a BackgroundWorker that repeatedly refreshes the textbox.
来源:https://stackoverflow.com/questions/14200909/caret-disappears-in-a-textbox