简体   繁体   English

如何使文本框的行为类似于C#中的弹出信息框

[英]How to make textbox behave like a popup info box in C#

i intend to display some information when i right click on a textbox. 当我右键单击文本框时,我打算显示一些信息。 this information is just plain readable information. 这些信息只是普通的可读信息。

My approach was to use a richtextbox to be visible whenever the texbox is right clicked. 我的方法是每当右键单击texbox时,都使用一个RichTextBox可见。 however i am not able to hide the textbox when user clicks the container. 但是,当用户单击容器时,我无法隐藏文本框。 using mousecapturechanged event for Richtextbox only caputres the click on the Richtextbox and not any clicks made outside the Richtextbox. 对Richtextbox使用mousecapturechanged事件只会显示对Richtextbox的点击,而不会对Richtextbox之外的任何点击进行提示。 release focus also does not solve the purpose. 发布重点也不能解决目的。

Edit: Gist: 编辑: 要点:

So what am i trying to do is create a popup info box, whose sole purpose should be to display information and then hide when click is made anywhere other than the box itself 所以我想做的是创建一个弹出信息框,其唯一目的应该是显示信息,然后在单击框本身以外的任何位置时隐藏信息

我知道您说过明确释放焦点是行不通的(确切地说,您是怎么做的?),即使在富文本框中为LostFocus设置了事件侦听器,然后在出现偶数时将其隐藏,该怎么办?

This works for me : this assumes you do want the RTF control to pop up where the user clicked in the TextBox, rather than in a fixed location. 这对我有用:假设您确实希望RTF控件在用户单击TextBox的位置而不是固定位置弹出。 And this example suppresses the default context menu by setting ShortCutsEnabled : it re-enables using keyboard shortcuts when the left mouse goes down : if they are turned off. 此示例通过设置ShortCutsEnabled抑制默认的上下文菜单:当鼠标左键按下时,它会使用键盘快捷键重新启用:如果它们已关闭。 This example also defines a double-click handler on the RTF control which will also hide the RTF control. 本示例还在RTF控件上定义了一个双击处理程序,该处理程序也将隐藏RTF控件。

    private Point rtfLocation;

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (MouseButtons == MouseButtons.Right)
        {
            rtfLocation = this.PointToClient(textBox1.PointToScreen(new Point(e.X, e.Y)));
            textBox1.ShortcutsEnabled = false;
            richTextBox1.Location = rtfLocation;
            richTextBox1.Show();
        }
    }

    private void richTextBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        richTextBox1.Hide();
    }

    private void richTextBox1_Leave(object sender, EventArgs e)
    {
        richTextBox1.Hide();
    }

    private void Form1_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Visible)
        {
            richTextBox1.Hide();
        }
    }

    private void textBox1_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Visible) richTextBox1.Hide();
    }

    private void richTextBox1_VisibleChanged(object sender, EventArgs e)
    {
        textBox1.ShortcutsEnabled = ! richTextBox1.Visible;
    }

In winforms Controls the LostFocus Events doesn't work in all situations.. better use the Leave event.. it always fires when the control Loses the Focus.. In your case you need to track the MouseDown( or whatever event of the mouse you like the best) in the TextBox to pop out the RichtextBox and then use the Leave event of the RichtextBox to Hide it. 在winforms控件中,LostFocus事件并非在所有情况下都起作用。.更好地使用Leave事件..当控件失去焦点时,它总是会触发。.在这种情况下,您需要跟踪MouseDown(或鼠标的任何事件, (最好的)在TextBox中弹出RichtextBox,然后使用RichtextBox的Leave事件将其隐藏。 Do not try to Remove the RichtextBox Control in the leave event.. it might crash. 不要在离开事件中尝试删除RichtextBox控件。它可能会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM