views:

737

answers:

3

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. 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. 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

A: 

I know you say that explicitly releasing focus does not work (how are you doing this, exactly?), what about setting up an event listener for the LostFocus even on the rich text box, and then hiding it when the even occurs?

FrustratedWithFormsDesigner
i have used the evenlistener relase focus on rich textbox, the focus release does not occur when i click the container. if i click some other control like button or other textbox than it does.if i implement enter focus on container, the control remains hidden all the time.
Kazoom
A: 

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. 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. This example also defines a double-click handler on the RTF control which will also hide the RTF control.

    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;
    }
BillW
double clicking the rich textbox looks like a neat idea but i want it to hide when someone clicks on the container.
Kazoom
Try our "new improved version" ... above ... at no extra cost :) This will handle hiding the RTF if the user clicks on the Form as well as any other control. You do realize the first example would hide the RTF as soon the user clicked on any other enabled control on the Form including the TextBox ?
BillW
Note the above code sample does not handle the case where you want to limit the RTF pop-up appearing only when you clicked on a "non-empty" place in the TextBox. That's a refinement I would want to pursue if I were developing this further. Note that another alternative ("thinking out of the box") is to have a another Form containing an RTF control which you can set partially transparent and show over the TextBox, and which has a close button for the end user.
BillW
awesome trick, but i have one more thing to add, my container is not a form it is a groupbox, inside a form, so now i use mousecapturechange event on container to hide the richtextbox, however i also have to implement form click to hide the container, and mousecaputerchange events for other containers. is there a way to just hide my richtext box my only implementing form click?
Kazoom
Hi Kazoom, at this point the "torch" passes to you, brother (or sister as the case may be) :) : this runner needs a rest, and the quirks of a third-party control I am using demand my full attention. Duct tape, baling wire, yeah, we gotta use 'em. best,
BillW
A: 

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. Do not try to Remove the RichtextBox Control in the leave event.. it might crash.

jmayor
@jmayor Hi, I thought defining 'Leave would take care of all possibilities also; turns out, on further testing, that it does not handle the case of a click on the TextBox as you might expect : in my revised code above, I had to include a Click event handler on the TextBox to cover all the bases. best, Bill
BillW
Well ok, in my personal experience I've made customized masked textBoxes and always showing the mask when textBox Leave event is fired.. and they work so far... I don't know if that event is suppressed for some reason on the RichTextBox.
jmayor
I mean that under some conditions it doesn't fire
jmayor
Hi JMayor, You "got me" : I can't compute why at times handling Leave doesn't work for the RTF control. Just another example of the need for duct tape in programming to the standard WinForms controls :) ? best,
BillW