2

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();
}
4

2 に答える 2

6

これは、ユーザーがメニュー項目を選択すると、メニューにフォーカスが設定されるため、テキストボックス内にカーソルを表示しても意味がないためです。

カーソルを表示したい場合は、メニュー操作が完了した後、テキストボックスにフォーカスを戻す必要があります。

例(ボタンを使用):

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox1.Focus();
    }

ただし、メニューの動作によっては、これが少し難しい場合があることに注意してください。フォーカスがテキストボックスに戻らない場合は、タイマーを追加してフォーカスを設定し、メニューがフォーカスを「再スチール」せずに、実行中の処理をすべて終了できるようにする必要があります。

于 2013-01-07T17:29:51.503 に答える
0

Form.invalidate()またはフォームを更新する他のメソッドを使用してみましたか?テキストボックスにフォーカスを戻すこともできます。

それらが機能しない場合は、テキストボックスを繰り返し更新するBackgroundWorkerをコーディングすることができます。

于 2013-01-07T17:27:14.927 に答える