MessageBox without focus on MessageBoxButton

Is there a way to show a MessageBox in C # without focusing on a button in a message box? For example, the following code fragment (which does not work as desired):

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

I want the MessageBox to appear without an emphasis on [Yes] or [No]. The premise is that the client scans several barcodes that have a carriage return in and. Therefore, when a message box pops up, and they scan further barcodes without noticing the message box, they β€œclick” a button.

+7
c #
source share
3 answers

This is not possible in the standard MessageBox - you will need to implement your own if you want this functionality.

See here to get started.

+3
source share

Well, you can do this, of course, with a 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 code above assumes that when the BeginInvoke is called a MessageBox , it is displayed and focuses on the button. This will be the case, as far as I know. If you want to make sure the message box is already shown, you can use this code to find it and then remove the focus.

+7
source share
  • add a new form to your msg project name
  • add a label to this form, write your message in the text of this label.
  • add button1 with its text property set to OK
  • add textBox1 with Visible property set to false
  • add this code to msg_FormLoad() :

    txtBox1.Focus ();

  • add this code to buton1_Click :

    this.Close ();

  • when you want to show your message, you can simply:

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

  • done!

PS: not verified, because there is no IDE at the moment, but I think that it can be configured to work with minimal effort.

0
source share

All Articles