插入符号在文本框中消失 [英] Caret disappears in a textbox

查看:35
本文介绍了插入符号在文本框中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 制作一个简单的记事本程序,在我的主窗体中,我有一个文本框,它经历了一些奇怪的事情,我的编辑菜单中有一个 DELETE on_click 我有一个事件

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 = "";

也试过了

 txtContent.Text = string.Empty;

 txtContent.Clear();

但每次操作后,我的 Caret 消失了,我认为这可能是一个焦点问题,但我也没有尝试过.不小心我最小化了我的记事本然后再次打开它光标回来了并且在删除操作后它没有消失我在网上搜索了这个问题但找不到任何东西希望你有一些建议

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

推荐答案

这是因为当用户选择菜单项时,菜单现在具有焦点,因此在文本框中显示光标没有意义.

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.

例如(使用按钮):

    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.

这篇关于插入符号在文本框中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆