VB.Net MessageBox.Show() moves my form to the back - vb.net

I have an MDI application. When I show a message box using MessageBox.Show(), the entire application disappears behind all of my open windows when I dismiss the message box.
The code is not doing anything special. In fact, here is the line that invokes the message box from within an MDI Child form:
MessageBox.Show(String.Format("{0} saved successfully.", Me.BusinessUnitTypeName), "Save Successful", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Me.BusinessUnitTypeName() is a read only property getter that returns a string, depending upon the value of a member variable. There are no side effects in this property.
Any ideas?

Remove the last parameter, MessageBoxOptions.DefaultDesktopOnly.
From MSDN:
DefaultDesktopOnly will cause the
application that raised the MessageBox
to lose focus. The MessageBox that is
displayed will not use visual styles.
For more information, see Rendering
Controls with Visual Styles.
The last parameter allows communication of a background Windows Service with the active desktop through means of csrss.exe! See Bart de Smet's blog post for details.

Remove the MessageBoxOptions.DefaultDesktopOnly parameter and it will work correctly.
DefaultDesktopOnly specifies that "The message box is displayed on the active desktop" which causes the focus loss.

These answers are correct, but I wanted to add another point. I came across this question while working with someone else's code. A simple message box was causing the front most window to move to the back:
MessageBox.Show("Hello").
Turns out, there was a BindingSource.Endedit command before the MessageBox. The BindingSource wasn't connected to any controls yet, but it caused the window to change z-positions.
I am only including this note since my search brought me to this question and I thought it might be helpful to someone else.

Related

vbnet 2008 forms.designer.vb undo bug

I'm having this InvalidOperationException
An error occurred creating the form. See Exception.InnerException for
details. The error is: Object reference not set to an instance of an
object.
pointing at this line of code:
Private Sub BeginningBalancesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginningBalancesToolStripMenuItem.Click
inventory.MdiParent = Me <--error here
inventory.Show()
End Sub
EDIT:
Im only using drag and drop for creating controls from the toolbox. The thing here is, everything works fine until I relocated AND decided to return panel_001 to its previous location by pressing ctrl-z in the inventory.vb during design time to revert the change I made. The program runs fine without error prior to that specific panel relocation. I never edited any codes in the designer.vb and in the inventory class. I strongly believe that a piece of code wasn't properly restored at the ctrl-z action in the inventory.Designer.vb. Is this a visual studio bug?
EDIT: I tried creating a new winform and attempted to start fresh by foolishly copying & pasting all the controls and the forms' class and was no good. I assume the problem does not lie under the striked-out line above.
The Winforms designer has a knack for tripping really mystifying design-time exceptions. A side-effect from its strong WYSIWYG design, it will run some event handlers at design-time. Like Paint, making a control look the way it does at runtime. Nice, but that does come with a price. This can easily cause an exception if such an event handler was not written to operate correctly at design time. You are supposed to use the DesignMode property to keep the code safe.
Coming up with a way a Click event handler could run at design-time however requires massive amounts of imagination. That is an event that will not run at design time, the designer uses a layered window that intercepts any mouse clicks, using them for design use instead. Like selecting a control or displaying the design-time context menu.
I have actually seen the Click event of a ToolStripMenuItem run. The designer is not 100% watertight but it happened just once and I was hacking the code pretty hard. Coming up with a way that it could possibly run by using Undo is going to be difficult. Maybe you give it the Ctrl+Z shortcut, don't assume that guess is credible.
The way to deal with incomprehensible black magic like this is to just dismiss it and move on with your life. You just don't stand much of a chance to diagnose it and if you do then there's nothing you can do about it anyway because this isn't your code. Well, other than the need to use DesignMode, that may well be necessary. Not in this case. The only thing you have to watch out for is that such an exception did not destroy the InitializeComponent() method. That can happen too, you notice by controls being missing when you re-open the designer. Very unpleasant, you do need a good backup copy in source control to recover from that lossage.
OK! after giving it a few more tries, I found the answer. After the exception shows, copy exception details to clipboard and then I pasted it on Notepad. It had a lot of texts but the bottom part was the important one:
--insert wall of texts here-- in C:\Users\Ellen\Documents\Visual Studio
2008\Projects\SampleApplication\SampleApplication\Forms\inventory.Designer.vb:line
760 at CabuyaoWaterDist.inventory..ctor() InnerException:
It pointed me out to the specific line in the designer where the ctrl-z did not properly revert one of the label's caption

Error Closing a Form Through a User Control

In the program I'm writing, there is just one form. I've made a user control visible and it contains several buttons. One of the buttons is supposed to close the form when clicked. I can't find any way to do this without getting the error: "Cannot access a disposed object. Object name: 'ShapeContainer.'" I'm pretty sure I understand the problem; after the form has been closed, the user control no longer exists, so there's an error when Form.Close() (I've tried Form.Dispose() too) has completed and it tries to go back to the code inside the button click event. Does anyone know how I could accomplish closing the form through this user control's button without getting the error?
Okay I'm pretty new to vb but I think if you get rid of the Form.Dispose() & Form.Close then try Me.Close() as you are referring to the current Form that the button control is located on.
You Should have got this error:
(Error 1 'NameSpace_.Form1' cannot refer to itself through its default instance; use 'Me' instead.)

CTRL+ TAB when webbrowser is in focus

I have a windows application with a tabcontrol. One of the tab of the tabcontrol has a webbrowser control.Now the issue that I am facing is when the focus is inside the webbrowser control, the normal Ctrl+Tab functionality of the tabcontrol is not working.I want the Ctrl+Tab to change the selected tab of tabcontrol even when the focus is inside webbrowser control in selected tab.How to achieve this ?
I have already tries overriding ProcessCmdKey.but it does not get hit when focus is inside webbrowser control.
I also tried registerhotkey method ,it works but it locks the Ctrl+Tab hotkey within my application & system doesn't respond to any other Ctrl+Tab presses outside my application when application is running, which is expected behaviour of registerhotkey.
Here is the code you need:
If WB.ContainsFocus Then
MsgBox("We have focus, disappearing...")
WB.Document.Body.RemoveFocus()
End If
Now, the question is, when to run that code. If you put it in the WebBrowser1_GotFocus event, you'll need to turn it on and off. Turn the code off if the user is interacting with the WB Control, and turn it back on when they are not and when you expect to be experiencing the problem you've mentioned.
Of course, you could add another line to ensure a particular control/tab/panel etc gets focus after you remove focus from the body. Also, here are 3 other SO questions that have answers which may help you, but these will take you in directions different to the direction I've provided, probably due to the fact that the questions are not identical to yours, but are similar enough to be useful (not listed in order of preference).
Prevent WebBrowser control from stealing focus?
Webbrowser steals focus
Focusing WebBrowser control in a C# application
UPDATE:
I just wanted to add, instead of the .Body.RemoveFocus() you could do this:
WB.Document.Body.Parent.RemoveFocus()
Which I prefer, since the .Document object didn't have an explicit .RemoveFocus method on the intellisense I was gettign in VS2012 RC. This is probably referring to the HTML tag (and not the .Document object) and since the html tag is the only parent to the body tag, it makes sense, and there is no "HTML" object directly available in the intellisense under object, since you can get it via other means, so it's just more convenient doing it this way.
Cheers, and let me know if you need more info on anything.

Is it possible to have an extension library dialog box within a repeat control?

I'm running with an 8.5.3 UP1 server and I have a need to have many dialog boxes (for confirmation purposes) for a whole bunch of "action buttons" on an xpage. The code for these dialog boxes is almost exactly the same with the exception of the confirmation message being different and the client-side JS function they are calling if the Yes button is selected.
Since I really hate repeating code over and over, I was wondering if it is at all possible to put a xe:dialog control within a repeat control and specify the message and function call from an array of values? I know I can't compute the ID of the dialog control and without that I'm not sure how I would reference the dialog to open and close it.
Any ideas? Thanks
Yes, this is possible.
Make sure that you specify that the dialog box's property for keepComponents is set to False. You don;t have to do anything special for opening or closing the dialog box, just use whatever ID you give the dialog box in you client-side action to open the dialog box in the repeat such as XSP.openDialog('#{id:myDialog}')
The XPages renderer will automatically calculate the correct ID names for you.

Message Box pop up not cleared properly

I have an application in VB.net. I am using the msgbox inside the execution of a loop. Now the message box pops up fine, but when i click on "Ok" in the pop up, the message box
any help will be much appreciated.
I'm guessing that the end of the question is the same as the subject, namely, that in a loop, the message box pops up but when OK is clicked the message box is not "cleared properly".
It sounds like the loop is running tightly enough that the system cannot process any messages and refresh the window.
Without seeing any code, however, this is just speculation and an appropriate solution cannot be given. You might try adding a .Refresh() call after the message box to the form/window or whatever to get the screen to update.
Please provide more information if possible.