5

I'm making a dialog that look like Notepad's Find Dialog. I notice that the underline character of Notepad's Find dialog always show all the time (I have to press ALT key to see this with my dialog). How to always show underline character like that?

I try to use SendKeys.Send("%") on Form_Load event but nothing happens.

There is another problem, when I press ALT key on child Form, it show underline charater of parent Form too. How to avoid that?

This is sreenshot of Notepad's find dialog: enter image description here

I pretty sure this is not about Ease of Acess Center, because the main Form of Notepad doesn't always show this.

Ryan
  • 135
  • 9
  • 1
    What you already tried ? Add your code please... – Fabjan Jul 20 '15 at 07:27
  • Nothing but SendKeys.Send("%") at Form_Load. Know that I use '&' with the text of the control - Anyway, is there any chance that they use background picture for this? – Ryan Jul 20 '15 at 07:29

2 Answers2

2

Seeing the n in "Find" underlined in the Notepad dialog is an intentional bug. The dialog isn't actually part of Notepad, it built into Windows. Underlying winapi call is FindText(). The feature is in general a pile 'o bugs, one core problem is that creating a new window after the UI is put in the "show underlines" state doesn't work correctly, that new window isn't also in that state. Presumably the intentional bug was based on the assumption that the user would be somewhat likely to use the Alt key to get the dialog displayed. Yuck if he pressed Ctrl+F.

The Windows dialog probably does it by simply drawing the "Find" string with DrawText() with the DT_NOPREFIX option omitted. You could do the same with TextRenderer.DrawText(), omit the TextFormatFlags.HidePrefix option.

Not exactly WinFormsy, you'd favor a Label control instead of code. It is hackable, you'd have to intentionally send the message that puts the UI in the "show underlines" state for your own dialog. Do so in an override for the OnHandleCreated() method:

    protected override void OnHandleCreated(EventArgs e) {
        const int WM_UPDATEUISTATE = 0x0128;
        base.OnHandleCreated(e);
        SendMessage(this.label1.Handle, WM_UPDATEUISTATE, new IntPtr(0x30002), IntPtr.Zero);
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

Where "label1" is the control you want to show underlines. Repeat for other controls, if any. It is supposed to work by sending the message to the form, that this doesn't work is part of the pile 'o bugs. Yuck.

Fwiw: do not fix this by changing the system option as recommended in the duplicate. That's very unreasonable.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I found exaclly same answer in the link which the anwser in the duplicate given http://stackoverflow.com/questions/14054036/show-hotkeys-at-all-times/14054492#14054492. The answer in the link use Form_Paint event instead of OnHadleCreated. So I suppose your answer is better because Form_Paint is called a lot of time, am I right? - Can I ask why when I press ALT key on child Form, the MenuStrip of parent Form show access key (the underline character)? – Ryan Jul 20 '15 at 09:17
  • 1
    Right, using Paint is most definitely the wrong way to do this. Pressing ALT is supposed to put the *entire* UI in the "show underlines" state, so getting it turned on for all active forms is normal. I know, doesn't make much sense. – Hans Passant Jul 20 '15 at 09:27
  • I correct myself. Yours doesn't seem to work :( - I do it fine on Paint event, but to avoid being called many time I add this on Paint event `Form.Paint -= new PaintEventHandler(this.Form_Paint);` - With the ALT problem, do you suggest any idea to fix this? – Ryan Jul 20 '15 at 10:52
  • I posted tested code, I have no theory why it doesn't work for you. Other than the obvious one, not adapting my snippet to the actual controls you want to display with underlines. – Hans Passant Jul 20 '15 at 11:01
  • Hmm, weird. I even paste your code into mine. I think the OS or compiler maybe is the problem here, but thanks anyway – Ryan Jul 20 '15 at 11:07
  • I new to windows form, so can you say a little more clearly about ALT problem? Or maybe some code will do :) – Ryan Jul 20 '15 at 11:14
  • Hmya, perhaps the best way to explain it is that it isn't actually a problem. Most programs behave this way, the Notepad dialog is an unusual corner case. And you certainly *do* get underlines as soon as the user presses the ALT key after the dialog is displayed. Maybe you shouldn't try to so hard to fix a non-existing problem. If anybody dislikes this "feature" then they'll turn it off with Control Panel. So can you. – Hans Passant Jul 20 '15 at 11:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83735/discussion-between-ryan-and-hans-passant). – Ryan Jul 20 '15 at 11:20
0

You can use RichTextBox control and extension method for that:

public static class FontHelper
{
    public static void Underline(this RichTextBox txtBox, int underlineStart, int length)
    {
        if (underlineStart > 0)
        {
            txtBox.SelectionStart = underlineStart;
            txtBox.SelectionLength = length;
            txtBox.SelectionFont = new Font(txtBox.SelectionFont, FontStyle.Underline);
            txtBox.SelectionLength = 0;
        }
    }        
}

richTextBox1.Text = "Search for";
richTextBox1.Underline(7, 1);   // index and length of underlying text
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • Not really what I looking for. I finally found the answer in the link @Luaan give, but thanks anyway – Ryan Jul 20 '15 at 08:40