How to correctly close a Windows.Forms dialog and return something different than "Cancel"? - vb.net

I defined a form which I show as a dialog as per myForm.ShowDialog. The form sets the AcceptButton property correctly to my button. When I call the Close() method, the dialog result is always Cancel.
I searched about this issue and found this question where they suggest to manually set DialogResult when it should be different to Cancel.
Since VB.net offers me to set an "Accept" button I find this "solution" quite hackish though. The documentation for the AcceptButton property says that it determines which button is "clicked" when the user presses the Enter key. I also expected that would automatically set the DialogResult to "OK" but it does not seem to do so.
So, is manually figuring out which button was clicked and updating the DialogResult, which looks like cleaning up after the Microsoft guys who designed Windows Forms, the way to go or did I miss something?

The AcceptButton does not close the form automatically. This is because it is just the default button which gets "clicked" if you press Return. If you set the DialogResult in the buttons click event handler, the form gets closed directly so you don't need to call .Close() anymore, so this seems not to be a bug to me.

Related

How to prevent button clicks in a MS Access Form_A when Form_B is correctly displayed modal over Form_A

I realized while I was typing up this question what the problem might be and was then able to solve it. So, I have posted the question and then the answer in case it helps someone else.
I am using MS Access 2016. I have a Form_A that displays a menu of buttons--each button takes the user to a distinct form to perform whatever functions the user needs to perform. For example, one Form_A menu button could be [Add Clients] while another button could be [Add Staff], etc.
When the user first logs into the system, I use VBA code to do a number of "housecleaning" functions. Form_A and the buttons are visible, but I have made a Form_B that is a small dialog that shows the word "Working" with animated "..." while the VBA in Form_A does the housecleaning.
The problem that I cannot solve is that even though I have set Form_B's "Pop Up" property to "Yes" and its "Modal" property to "Yes", the user can still click buttons on the Form_A menu which then execute while Form_B is still doing its housecleaning.
I thought setting Form_B to modal should prevent button clicks on Form_A from getting registered.
More details:
In the Form_Load event, Form_A calls "housekeeping" code that resides in a Module. That module has a Public Sub called "RemakeTables." The RemakeTables code uses DoCmd.OpenForm "Form_B" to display the "Working" dialog.
The RemakeTables code calls several other Public subs. Between each call to the other Public subs, a DoEvents is called which allows for the "Working" dialog to animate the "..." (i.e., cycle between "Working", "Working.", "Working..", and "Working...").
My hunch is that the DoEvents is what is allowing clicks to get registered in Form_A.
How should I be doing this so that clicks cannot be registered while the Form_B "Working" dialog is showing but can still be animated?
As I was typing up the question, it occurred to me that the main problem was that the subs and functions I was calling during "housekeeping" used the DoEvents action to allow the Form_B "Working" dialog to update/animate. However, DoEvents also allows mouse clicks to register.
So, how to prevent the problem?
Include the "housekeeping" calls in the Form_Open event rather than the Form_Load event because the Form_Open event occurs first, before the Form_Load and before the form is displayed, so, there will not be any buttons visible to click if the "housekeeping" code is called in Form_Open.
Hope this helps someone else who might have had a similar problem.

Application fail to exit when a runtime created object is used

I'm using vb.net 2013, and I have configured Shutdown mode to "When last form is closed".
On my main form, I have a menu item which has this code to close the application:
Application.Exit
Everything is working fine, except one case:
When I open a specific form, where a Combobox is created on runtime and I've used Addhandler to subscribe to several events.
The combobox is created when pressing a button.
When I open this form and I don't create the combobox, everything is working ok. If the combobox is created, when I close this form and try to close the application using the menu item, nothing happens. The application is not closed and no error message is displayed. (the same situation occurs when I try to close the main form with "x" button)
On the form's (where I have the combobox) close event , I tried to put a line of code:
MyCombobox.dispose()
But the situation is the same.
What can I do? Thank you!
What I do from my little experience is
1.) remove the MyCombobox from its parent control (I'm think this is in your combobox close event).
2.) set the MyCombobox to Nothing
3.) Dispose() it.
MyCombobox = Nothing
MyCombobox.Dispose()
It would be useful to see some part of your code for the close event so we can help you check. More power to you!
Update based on OP's comment:
I have read the following from MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx
From this, it is important to note that:
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Triggering Visual Basic Keydown events without a specific function

I'm building on top of code that a previous developer has left me, and he left something that intrigued me quite a bit.
Basically on his menus, he has a TextBox to take in user input and a button next to it to submit the value of the TextBox (for example if the user wanted to select option 1, he would input 1 into the TextBox and click the button). However, the user could also press the Enter key while focusing the TextBox, and it would be treated as the submit button was clicked.
Now this is simple enough to do, but when I check the VB code behind the menu, there's no TextBox_Keydown(...) Handles TextBox.Keydown function anywhere, only the button click event. How is he doing this? He has several menus that are similar and I can't figure out how.
A standard dialog box, if not told to act otherwise, enter does default command button and escape does cancel. In VB look at the properties Default for the command button.
I discovered how he was doing it. He basically mapped the AcceptButton and CancelButton properties of the entire Windows Form to various button functions.

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.

Default Button on Form (VB 2008)

I want to find a way to make a specific button, the form's default button,
I.e. the button that is highlighted when the form opens for the first time.
I tried the AcceptButton property but when I run the program, that does not work.
Any idea?
Thank you in advance,
Tassos
You need to change the AcceptButton property of the containing form.
form1.AcceptButton = button1
Here form1 is the Form whose default button you need to set, and button1 is the name of the Button on that form.
The form's AcceptButton and CancelButton properties define the default behaviour for the Enter and Escape keys, rather than the highlighting.
To highlight the button use the Focus method, but when doing this in the form_load event you will need to call the Select method instead.
btnDefault.Select()
As mentioned in the comments, setting the control to the lowest taborder will achieve the same thing
The answer from 'chk' on 5/2/13 is the correct answer, but is shown as a string which of course is not the way to do it.
Also, in the form's property sheet you can find, under Misc, the property 'AcceptButton'. This will give you a list of buttons on the form - just select the one you want.
The button selected as the AcceptButton will behave as the 'default' button. It will be 'highlighted' with a darker border and will be clicked when you push the Enter key on your keyboard.
Setting up an AcceptButton is different than setting the button's focus. The AcceptButton's click event will be triggered by the Enter key no matter which control has the focus on the form.
You can also do this programmatically.
I have a maintenance form where initially I want the "search" button as the form accept button.
when I'm displaying the field maintenance area, I want the "ok" button to be the accept button.
You simply change this in the appropriate areas in your code to Me.AcceptButton = MyButtonName.