7

Is there a way to show an MessageBox in C# without a focus on a button in the message box? For example the following code snippet (which is not working as wished):

 MessageBox.Show("I should not have a button on focus",
                        "Test",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button3);

What i want is that the MessageBox is shown without a focus on [Yes] or [No]. Background is that the customer scans several barcodes which have an carriage return at the and. So when the messagebox pops up and they scan further barcodes without noticing the message box they "press" a button.

Daniel W.
  • 449
  • 10
  • 29
  • I'm afraid that isn't possible with a vanilla MessageBox, as it just consists of a bunch of buttons and a static, the latter of which cannot have focus. Thus at least one button always has focus. You could maybe add Cancel to the mix, make it default, and just re-show the message box if Cancel was pressed. – Joey Feb 20 '15 at 08:20
  • I think most easiest way will be implement MessageBox by your own form.... – Andrey Korneyev Feb 20 '15 at 08:20
  • It certainly can be done. If you own the computer, you p0wn the computer. But `MessageBox` doesn't provide a _convenient_ way for there to be no default. Pressing **Return** will always dismiss the dialog with some value. Note, of course, that you can make a third button the default (i.e the `Cancel` button of a `YesNoCancel` box) and require the user to press something other than `Cancel` to proceed; you just redisplay the box until they do. – Peter Duniho Feb 20 '15 at 08:22
  • I am pretty sure that it is not possible. You can either implement a custom MessageBox or you can push the focus to a button which wont be performing anything(this is a dirty trick). – Nilay Vishwakarma Feb 20 '15 at 08:22

3 Answers3

9

Well you can do it certainly with some trick.

[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);

private void button1_Click(object sender, EventArgs e)
{
    //Post a message to the message queue.
    // On arrival remove the focus of any focused window. 
    //In our case it will be default button.
    this.BeginInvoke(new MethodInvoker(() =>
    {
        SetFocus(IntPtr.Zero);//Remove the focus
    }));

    MessageBox.Show("I should not have a button on focus",
               "Test",
               MessageBoxButtons.YesNo,
               MessageBoxIcon.Question,
               MessageBoxDefaultButton.Button3);
}

Note that the above code assumes that when BeginInvoke is called MessageBox is shown and it got the button focused. It will be the case usually upto my knowledge. If you want to make sure message box has shown already you can use this code to find it and then you can remove the focus.

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • This works but it has an annoying side effect, that is, you can use the keyboard at all (e.g., `Tab`) unless you activate another window and then "reactivate" the message box. Clicking the title of the message box (or anywhere else other than the buttons) doesn't make it "activated". – 41686d6564 stands w. Palestine Mar 31 '19 at 01:33
4

This isn't possible with the standard MessageBox - you'll need to implement your own if you want this functionality.

See here to get you started.

RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
  • 1
    I believe this is right. .NET's `MessageBox.Show` is just a wrapper of the Win32 MessageBox function which does not have any such option: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx – vesan Feb 20 '15 at 08:25
0
  • add a new form to your project name it msg
  • add a label to this form, write your message in this label's text
  • add a button1 with its text property set to OK
  • add a textBox1 with the Visible property set to false
  • add this code in msg_FormLoad():

    txtBox1.Focus();

  • add this code to buton1_Click:

    this.Close();

  • whenever you want to show your message you can just:

    msg msgBox = new msg(); msgBox.ShowDialog();

  • done!

P.S: not tested because the lack of IDE for the moment but I guess it can be adjusted to work with little effort

chouaib
  • 2,763
  • 5
  • 20
  • 35