MsgBox YesNo solution needed - vb.net

I have done the following in a LoginForm:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If UsernameTextBox.Text = ("username") And PasswordTextBox.Text = ("password") Then MainMenu.Show(Me.Hide) Else MsgBox("Wrong")
End Sub
But it gives me the following error:
Error 1 Expression does not produce a value (on Me.Hide)

If the LoginForm is the "startup" for the application, note that other forms will become children of this form. You'll be able to close the application one with Application.Exit() which is really not a good practice. You should close the application by closing the "startup" form.
With that said, Ranhiru is correct.
MainMenu.Show 'Show the MainMenu form
Me.Hide 'Hide the LoginForm

Are you trying to hide the Main Menu and Hide the current form ?
If so, try
If UsernameTextBox.Text = ("username") And PasswordTextBox.Text = ("password") Then  
MainMenu.Show
Me.Hide
Else
MsgBox("Wrong")

Related

Have to Click Form To Give Focus

I have a main form with buttons that open a custom message box form. It works fine if it's just a message and the user just needs to click OK. But if the answer to that message box is important, like "Are you sure you want to delete this file?" I use a while loop to wait for the user to respond and once they do then a flag is set from false to true and the response is recorded.
For some reason any response that uses a while loop to wait is causing the message box form to not have focus after being called. Requiring the user to first click on the form, and then click on OK.
So far I've tried using form.Activate() instead of form.Show(), as well as calling Application.DoEvents() inside the while loop since I believed the while loop was taking focus away from the message form immediately after being called. Neither solved the issue.
Code from a message box that works as intended:
If cmbLoadProgram.SelectedItem = "" Then
frmMessageBox.lblHeader.Text = "Set-Up"
frmMessageBox.lblMessageText.Text = "No Program Selected!"
frmMessageBox.Show()
Exit Sub
End If
Code from a message box that needs to be clicked twice:
If btnGetHexStart.Visible = False And cmbStartCondition.SelectedItem = "Pixel" Then
frmMessageBox.lblHeader.Text = "Hex Set-Up"
frmMessageBox.lblMessageText.Text = "Reset Hex Code Data?"
frmMessageBox.Show()
Me.Hide()
While Flag = False
If frmMain.OKCancel = "OK" Then
btnGetHexStart.Visible = True
btnGetHexStart.Enabled = True
btnGetHexStart.PerformClick()
Flag = True
End If
frmMain.delay(20)
End While
End If
I'm wanting both options to only need to be clicked once in order to confirm or cancel the action. Instead of the while loop questions needing to be clicked twice.
This just an idea from me, just how to open msgboxform, here we need MessageForm and one module1, for example:
Public Class MessageForm
'You can assign any variable to show any data in messageform display
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
theResult = MsgBoxResult.Ok
Me.Close()
Me.Dispose()
End Sub
Private Sub bttcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttcancel.Click
theResult = MsgBoxResult.Cancel
Me.Close()
Me.Dispose()
End Sub
End Class
Module Module1
Public theResult As MsgBoxResult
'You can add some parameter here to submit to MessageForm
Public Function myMessageBox() As MsgBoxResult
myMessageBox = MsgBoxResult.Cancel
MessageForm.ShowDialog()
myMessageBox = theResult
End Function
End Module
and then you can call the myMessageBox anywhere like this:
'if myMessageBox procedure have parameter, apply the paramters too
dim myRslt = myMessageBox()
You don't need to create a form,You can use DialogResult and MessageBox.Show, code:
Dim Result As DialogResult = MessageBox.Show("Set-Up" & vbCrLf & "No Program Selected!", "Warning", MessageBoxButtons.OKCancel)
If Result = DialogResult.OK Then
ElseIf Result = DialogResult.Cancel Then
End If

Closing parent form when child form also closes

Aside from my main form I have another form, frmAddFixture, which can open frmAddReport.
I'm simply trying to close frmAddFixture when "No" is selected from the msgbox, which also closes (successfully) Me (frmAddReport). If "Yes" is selected frmAddFixture should stay open, which it does. But I can't get it to close for "No". I've tried adding my own handler in to detect when frmAddReport is closing, but I couldn't get that working.
frmAddReport Code (run after a "Submit" button has been clicked):
Private Sub showMsg()
Select Case MsgBox("Do you want to add another player report for this fixture?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Add further reports")
Case MsgBoxResult.Yes
isNewFixture = False
Me.Close()
Case MsgBoxResult.No
isNewFixture = True
Me.Close()
''Close frmAddFixture
'frmAddFixture.Dispose()
'frmAddFixture.Close()
'frmAddFixture.Hide()
End Select
End Sub
Attempted:
Private Sub frmAddReport_FormClosed(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.FormClosed
frmAddFixture.Dispose()
End Sub
Here is what I think you are trying to do.
Let's ignore the message box for a moment and say that we have two forms : Form1 and Form2. We want that as soon as Form2 is colsed, Form1 will be disposed (closed) as well. I'ts pretty simeple :
Public Class Form2
' Occurs when Form2 is closed (note the event handler).
Private Sub Form2_FormClosed(sender As Object, e As EventArgs) Handles MyBase.FormClosed
Form1.Dispose()
End Sub
End Class

Form2 won't close and if I close it form1 will close as well. vb.net

So I made this game in vb.net, and when you run it, it will ask you for a name, that's form2. The thing is, when you put a name, form2 will not close/disappear, and if you close it the whole game will close.
This is the code for form2:
Public Class Form2
Public Shared myMoney As Long
Public Shared welcome As String
Private Sub PositronButton1_Click(sender As Object, e As EventArgs) Handles PositronButton1.Click
Form1.welcome = txtName.Text
Form1.lblWelkom.Text = "Welcome," & " " & Form1.welcome
MsgBox("Welcome," & " " & Form1.welcome & "." & "You recieved 500 money.")
Form1.myMoney = 500
Form1.lblMoney.Text = Form1.myMoney
Form1.Show()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
End Sub
End Class
It seems you have wrong settings in your project.
Go to "Project", "Settings" and then have a look after "Shutdown mode".
Yours is probably set to "When last form closes". But you have to set "start form".
Also do not use Form1.Show because this is wrong, create an instance of it, then call it.
Dim frm As New Form1
frm.Show()
Also use ShowDialog for showing the Form2, it returns a DialogResult, and if it is "OK", you can close the form.
Firstly, instead of setting Me.TopMost on the load event, you should call Form2.Focus() when you first open the Form in your Form1 code.
Secondly I am not sure how you are opening Form2, I assume you are using ShowDialog() In that case, in order to close Form2, you should call Me.DialogResult = DialogResult.OK
Hope it Helps

Check if any dialog is open

Does anybody see my mistake here?
I am unable to recognize if a form is shown as a dialog in my app.
Public Class Form1
Private m As Form2
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Me.Text = DateTime.Now.ToLongTimeString & " " & IsAnyDialogShown()
End Sub
Public Function IsAnyDialogShown() As Boolean
For Each f As Form In Me.OwnedForms
If f.Owner Is Me Then
Return True
End If
Next
End Function
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
m = New Form2
m.ShowDialog()
End Sub
End Class
What you are looking is for the property modal.
Check if the form's modal property is true (that is meaning that the form is showed with ShowDialog)
For Each f As Form In Me.OwnedForms
If f.Modal=True Then
'your code here
End If
Next
Now for your mistake (I haven't visual studio to try it right now) but your IsAnyDialogShown(), it seems that it returns always true :
For Each f As Form In Me.OwnedForms ' (So f belongs to Me)
If f.Owner Is Me Then 'f.Owner is always me because you are seaching in forms that have as owner the Me form
Return True
End If
Next
Hope I helped a little.
Tell me if I can do something more
So after your comments.
Try this:
For Each frm as Form In Application.OpenForms
If frm.Modal=True Then
'do something
'Actually you should have only one form because only one can be in modal state
end if
Next
You need to check Visible property of the form, which is the boolean.
If it is true, then form is shown, else it's hidden.
That's just doing forms owned by Me. Nothing to do with whether they are dialog forms.
ie.e it will pick up normal forms.
Also if you want this to work as expected , you should use the overload where you pass the owner.
as in m.ShowDialog(Me);
Not something I've ever done but if Owner isn't me in Me.OwnedForms I want my money back.

Errorprovider shows error on using windows close button(X)

Is there any way to turn the damned error provider off when i try to close the form using the windows close button(X). It fires the validation and the user has to fill all the fields before he can close the form..this will be a usability issue because many tend to close the form using the (X) button.
i have placed a button for cancel with causes validation to false and it also fires a validation.
i found someone saying that if you use Form.Close() function validations are run...
how can i get past this annoying feature.
i have a MDI sturucture and show the form using
CreateExam.MdiParent = Me
CreateExam.Show()
on the mdi parent's menuitem click
and have this as set validation
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If String.IsNullOrEmpty(TextBox1.Text) Then
Err.SetError(TextBox1, "required")
e.Cancel = True
End If
If TextBox1.Text.Contains("'") Then
Err.SetError(TextBox1, "Invalid Char")
e.Cancel = True
End If
End Sub
Any help is much appreciated.
googling only showed results where users were having problem using a command button as close button and that too is causing problem in my case
The ValidateChildren() method prevents the form from closing. Paste this code in your form to fix that:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
}
This is quite simple fix, in your Form's Closing Event, set a flag to indicate leaving the form, for example blnLeave, when the Form gets loaded, set the flag to False, when the Closing event gets triggered, set that to True within that event handler, then the change as follows would be
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If (blnLeave) Then
e.Cancel = False;
Return
End If
If String.IsNullOrEmpty(TextBox1.Text) Then
Err.SetError(TextBox1, "required")
e.Cancel = True
End If
If TextBox1.Text.Contains("'") Then
Err.SetError(TextBox1, "Invalid Char")
e.Cancel = True
End If
End Sub
Edit: Amended this answer for inclusion as per OP's comments. My suggestion is to handle the Form's Closed Event as shown
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
blnLeave = True
End Sub
And handle it here in the Form's window procedure override as shown here....
Private Const SC_CLOSE As Integer = &HF060
Private Const WM_MENUSELECT As Integer = &H11F
Private Function LoWord(ByVal Num As Integer) As Integer
LoWord = Num & &HFFFF
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_MENUSELECT Then
If LoWord(m.WParam.ToInt32()) = SC_CLOSE Then
' Handle the closing via system Menu
blnLeave = True
End If
End If
MyBase.WndProc(m)
End Sub