I'm using MessageBox to give some information to the user, but when such a box pops up, I want it to block access to the main window. So, until the user has clicked "OK", they should not be able to click (or even focus on) the window that's below it.
Does anybody know how to do this? I've noticed that MessageBox has very few functions, so maybe I'll even have to use a different object for this.
A quick and dirty solution could be a dialog form (Project->Add Windows Form->Dialog). This gives you visual extensibility of your MessageBox.
You can call your MessageBox's ShowDialog method and it will of course block access to the parent window.
There are of course other methods.
Example:
' This is your Dialog you created by going Project->Add Windows Form->Dialog
Public Class MessageBoxDialog
Public Overloads Sub ShowDialog(ByVal message As String)
Me.txtMessage.Text = message
ShowDialog()
End Sub
End Class
' This is the form you want to call the message box on
Private Sub btnShowMsgBox_Click(sender As System.Object, e As System.EventArgs) _
Handles btnShowMsgBox.Click
Dim messageBox As New MessageBoxDialog
messageBox.ShowDialog("This is another way to show a MsgBox but allows greater extensibility.")
End Sub
Just after I posted here, my friend came up with the answer. By calling MessageBox.Show(mf, "text") where mf is the main form, mf will be disabled as long as the OK button has not been clicked. I suppose this question was a bit silly to post after all, but I hope it might help others if they're stuck with the same problem.
Related
OK. I have a code, and it should work, it DOES close the login form, but doesn't open the menu form.
If username.Text = "lolman8776" Then
If password.Text = "#########PASSWORD HIDDEN FROM THE INTERNET#########" Then
Form2.Show()
Me.Close()
End If
End If
What I don't understand, is that this code has no syntax errors and SHOULD WORK!
It shows form2 and THEN it closes itself, but Form2 never shows up.
I'm running VB.Net 2013 Community, as it was a free download. (I also registered it so it wasn't trial).
I have tried removing Me.Close() from Form1 and putting a line of code into Form2 that would close Form1:
Form1.Close()
But, still nothing. I do not know why ANY METHOD won't work. I've searched high and low for an awnser, but to no success. Does anyone have a solution?
According to your comment: "I tried not even hiding/closing form1, but just showing Form2 will cause the app to break."
I believe your issue lies around not instansiating your Form. The Show() method is not static and cannot be called directly on Form2. It must be called on an instance of your Form. For example:
Dim myForm As New Form2()
myForm.Show()
Please refer to this for a little more information.
Hi Guys i'm new to Visual Basic Coding, and i can't seem to get where's my mistake on my coding, i'm trying to create a button that opens a new form while closing the current form.
i have two forms, form 1 is MainForm, form 2 is SearchForm
Whenever i use this code:
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
MainForm.Close()
SearchForm.Show()
End Sub
End Class
it will generate an error and says i need to replace MainForm.Close() into Me.Close()
When i Use this
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
Me.Close()
SearchForm.Show()
End Sub
End Class
It closes both Forms and it doesn't leave any Form Open. Kindly direct me to the proper path, thanks in advance.
You need to Hide the form rather than closing it. Since it's your main form, when it closes, the application exits.
Standard UI guidelines are to leave the main form open, and open search form on top of that. If you need to block the main form, while search criteria are selected, use .ShowDialog, instead of just .Show.
.NET WinForms programming pattern kind of implies that you never close your main form. If you deviate from this approach, you are guaranteed to encounter all sorts of layout and display issues. So please don't. You can .Hide the main form, if it needs to go to system tray or run in background.
*edit: OK, so this is my real problem, below scenario happens only when the form is MDIChild.. thanks for anyone that could provide me with the code
I have a form with labels, panels, buttons etc. Where I'm having problem is, while form2 is my active window/form and I clicked on a control inside form1, the form1 does not activate itself. What I would like to happen is for form1 to activate even when it's not the form I clicked, only the control inside it (any control)..
I'm thinking that if I clicked a control on the form, there's an event fired on the form. If I could only know of that certain event, that would help - maybe (coz I could just add Me.activate on that event if it exists). I've tried searching for series of events when a control (ex. label) is clicked but to no avail. I hope that someone could help me with this one.
Thanks in advance.
*edit
i will just try to make my question more understandable..
How can I activate the form when only the control is clicked (say, label or textbox)? My forms does not activate or focused when I click inside it except the form itself..
I can do this on one control..
Private Sub Label1_Click - Handles Label1.Click
Me.Activate()
End Sub
But what if I have 20 controls (labels, buttons, textbox, combobox, etc)? See? =)
EDIT: this answer does not apply to MDI applications.
I think what you really want to know is which one of your forms is currently the foreground window (if any). The first thing you need to understand is that a form instance lives inside a window, but the window's behavior is controlled somewhere higher up. Similar to how a form instance is identified by a variable pointing to the instance, a window can be identified by what's known as a window handle.
Knowing this, the proper way to find out whether a form is the "active" form is to:
find out the window handles of the windows containing our instances of Form1 and Form2
find out the window handle of the foreground window (which can be any window)
compare the value found in step 2 to all of the values found in step 1
Perhaps you'd then like to fire an event if the foreground window changes, but I'll leave the actual implementation up to you. There are probably several ways to perform step 1 and 2, but I can't give any solutions off the top of my head. Hopefuly I've put you back on the right track.
EDIT
Alternatively, you can use the form's Containsfocus property. If its value is True, you can safely assume that your form is the foreground window. I didn't find out about this property until after I wrote my own implementation, which I'll show you anyway:
One module containing only a windows API call
Friend Module NativeMethods
Friend Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
End Module
Calling this method will return the window handle of the foreground window (if any).
One module containing the extension method for the Form class
Imports System.Runtime.CompilerServices
Public Module FormExtensions
<Extension>
Public Function IsForeground(f As Form) As Boolean
Return (f.Handle = NativeMethods.GetForegroundWindow)
End Function
End Module
Calling this method returns whether the specified form f has the same window handle as the foreground window.
Usage example
You could use a Timer that periodically checks whether a form is the foreground window.
Public Class Form1
Private WithEvents timer As New Timer With {.Enabled = True}
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
If Me.IsForeground() Then
Console.WriteLine("this instance of Form1 is the foreground window")
End If
End Sub
End Class
Like I said before, you can use Me.ContainsFocus instead of my extension method and it will work just fine.
In non-MDI forms, the form is automatically activated when you click any control inside it.
I want to create a custom form (in visual basic .NET) that will stop other process responsiveness until the form is acknowledged. It would be a nice bonus if I can add a beep when trying to access the main program UI while this form is displayed as well (like how a messagebox does).
My initial idea was to create another thread for the messagebox-type form and have the main thread sleep until the messagebox-type form is responded too, however I think this would create a bug-like appearance on the main program as it simply wouldnt respond or update its UI (also worth noting, I have little experience working with multithreading, so this may be incorrect).
I really don't have much idea of how to proceed with this, any ideas and/or guidance are greatly appreciated. thank you! :)
I think that this type of behavior is that for which there is ShowDialog()
this sample is on the MSDN page for ShowDialog()
Public Sub ShowMyDialogBox()
Dim testDialog As New Form2()
' Show testDialog as a modal dialog and determine if DialogResult = OK.
If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
' Read the contents of testDialog's TextBox.
txtResult.Text = testDialog.TextBox1.Text
Else
txtResult.Text = "Cancelled"
End If
testDialog.Dispose()
End Sub 'ShowMyDialogBox
When the code calls ShowDialog() the program continue on the testDialog and exits only when your user press OK, Cancel, Close or whatever method, which, in the called dialog, set the property DialogResult to any value different from DialogResult.None.
I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color