Is there any way to hide a MessageBox? - vb.net

Is there any way to hide a MessageBox at form load?
I have used Checkedlistbox, and already there is a checkeditems on load of a Form2.
What I want to do is, when I click Form1 it shows Form2 with Checkedlistbox. My problem is, when I click Form1, it show up MessageBox before Form2.
Here is my code on vb.net:
On Form1:
Private Sub cmdSubmitModifyQuant_Click(sender As Object, e As EventArgs) Handles cmdSubmitModifyQuant.Click
Form2.Show()
End Sub
On Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chklstBox1Fill()
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue = CheckState.Checked Then
question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
If question = MsgBoxResult.Yes Then
'Nevermind
ElseIf question = MsgBoxResult.No Then
e.NewValue = CheckState.Checked
End If
End If
End Sub
In my code you can see that i also need to check checklistbox1.

This issue is probably that inside the chklstBox1Fill method you are checking items in your check box list, this causes the event to raise that shows the checkbox. One way to avoid this would be to set a flag to indicate you are populating the list and not show the message box when the flag is set:
Private FillingList As Boolean
Private Sub chklstBox1Fill()
FillingList = True
'Rest of method here.
FillingList = False
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If FillingList = True Then
Return
End If
If e.NewValue = CheckState.Checked Then
question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
If question = MsgBoxResult.Yes Then
'Nevermind
ElseIf question = MsgBoxResult.No Then
e.NewValue = CheckState.Checked
End If
End If
End Sub
(Forgive my VB.Net, been several years since I wrote any!)

Add a boolean variable that indicates wether your load procedure is complete or not. Doing so will not execute the CheckedChanged until the variable is set to True.
Dim FormLoaded As Boolean = False
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chklstBox1Fill()
FormLoaded = True
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If FormLoaded = False Then Return 'Don't execute the rest of the code if it evaluates to False.
If e.NewValue = CheckState.Checked Then
question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
If question = MsgBoxResult.Yes Then
'Nevermind
ElseIf question = MsgBoxResult.No Then
e.NewValue = CheckState.Checked
End If
End If
End Sub

Related

opendialog to show a file and save it with checkbox vb.net

I'm trying to connect to a database (mdb file of my choice) in a login screen and i want to save it for faster logon next times i boot the software.
I click on choose database button, opendialog lets me choose the file, i click OK and the db location shows in a textbox.
there's a checkbox beneath to save it before i connect to it.
But i can't manage to keep the checkbox checked, nor the textbox filled after i restart te program.
here's my current code:
Public Class LoginScreen
Private Sub Loginscreen_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 100
ProgressBar2.Visible = False
Panel1.Visible = False
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub btnExit2_Click(sender As Object, e As EventArgs) Handles btnExit2.Click
Application.Exit()
End Sub
Private Sub tmrLogin_Tick(sender As Object, e As EventArgs) Handles tmrLogin.Tick
ProgressBar2.Value = ProgressBar2.Value + 20
lblLoginMessages.Text = ProgressBar2.Value & "%" & " Completed"
If ProgressBar2.Value >= 100 Then
tmrLogin.Enabled = False
If txtUser.Text = "azert" And txtPassword.Text = "azert" Then
ProgressBar2.Value = 0
Else
lblLoginMessages.Text = "Wrong credentials, Try again!"
pboxClosed.Visible = True
PboxOpen.Visible = False
ProgressBar2.Value = 0
txtPassword.Text = ""
txtUser.Text = ""
End If
End If
End Sub
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
ProgressBar2.Visible = True
tmrLogin.Enabled = True
pboxClosed.Visible = False
PboxOpen.Visible = True
''navraag doen om dit correct in te stellen! ! ! ! !!
'If ProgressBar2.Value = 100 Then
'lblLoginMessages.Text = "Logging in..."
Me.Hide()
Mainscreen.Show()
'End If
If chkSavePassword.Checked = True Then
My.Settings.databaselocation = txtDatabaselocationshow.Text
My.Settings.SaveLocation = True
End If
End Sub
Private Sub btnDBConnect_Click(sender As Object, e As EventArgs) Handles btnDBConnect.Click
If Panel1.Visible = False Then
Panel1.Visible = True
Else
Application.Exit()
End If
End Sub
Private Sub btnChoose_Click(sender As Object, e As EventArgs) Handles btnChoose.Click
Dim strtext As String
OpenFileDialog1.Filter = "Database Files | *.mdb"
OpenFileDialog1.InitialDirectory = "F:\GoogleDrive\EINDWERK VBNET"
OpenFileDialog1.Title = "Choose your Database"
OpenFileDialog1.ShowDialog()
strtext = OpenFileDialog1.FileName
txtDatabaselocationshow.Text = strtext
'If OpenFileDialog1.ShowDialog = DialogResult.OK Then
' strtext = OpenFileDialog1.FileName
' txtDatabaselocationshow.Text = strtext
'Else
' MsgBox("Error: the database file could not be read, try again.")
'End If
End Sub
Private Sub tmrshowloginpanel_Tick(sender As Object, e As EventArgs) Handles tmrshowloginpanel.Tick
Panel1.Width += 5
If Panel1.Width >= 700 Then
tmrshowloginpanel.Stop()
End If
End Sub
End Class
i've scoured the net but can't really find what to do?
if you need more information, shoot!
Walk through this with me:
Make a new project, one form, add a textbox and a combobox.
Click the textbox:
In the properties grid at the top click (Application Settings), then the 3 dots next to Property Binding. Scroll to Text. Drop down the setting and choose New at the bottom.
Give it a name of ChosenDatabasePath or something useful and descriptive like that
Repeat from "Click the textbox" for the checkbox instead, and this time bind the Checked property not the Text property.. Call the checkbox's Checked binding SavePassword or similar
Close all the dialogs so you're back at the form
Click anywhere on the background of the form when switch to Events in the property grid, find FormClosing and double click it
Put My.Settings.Save() in the FormClosing event handler
Run the app, write something in the textbox, close the form (by the X, not by stopping debugging), then immediately open the app again (run it)

Run timer in background in visual basic form

first I'm new in this, and I have this code that shows a prompt to restart or postpone the restart for a while, the issue is that i want to hide the message and bring it back after the time specified by the user.
I'm using a "visual basic form" and the time that restart will be postponed it's selected from a "ComboBox"
My code is as follows.
Imports System.Management
Imports System.Security.Permissions
Imports System
Imports System.IO
Imports System.Collections
Imports System.SerializableAttribute
Public Class Form2
Dim PostponeReboot As Integer = 50
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
Label4.Text = SystemInformation.UserName
Button1.Enabled = False
ComboBox1.Enabled = False
Timer1.Interval = 1000
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
CheckBox2.Enabled = False
Button1.Enabled = True
ComboBox1.Enabled = False
ElseIf CheckBox1.Checked = 0 Then
CheckBox2.Enabled = True
Button1.Enabled = False
ComboBox1.Enabled = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
CheckBox1.Enabled = False
ComboBox1.Enabled = True
Button1.Enabled = True
ElseIf CheckBox2.Checked = 0 Then
CheckBox1.Enabled = True
ComboBox1.Enabled = False
Button1.Enabled = False
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text = "1 Hora" Then
PostponeReboot = 10
ElseIf ComboBox1.Text = "2 Horas" Then
PostponeReboot = 20
ElseIf ComboBox1.Text = "4 Horas" Then
PostponeReboot = 40
ElseIf ComboBox1.Text = "Seleccione" Then
Button1.Enabled = False
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked Then
MessageBox.Show("Rebooting")
'Shell("shutdown -r -f -t 60")
Form1.Close()
End
ElseIf CheckBox2.Checked Then
MessageBox.Show(PostponeReboot)
Timer1.Start()
Me.Hide()
End If
If PostponeReboot = 0 Then
Me.Show()
Else
Me.Hide()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PostponeReboot = PostponeReboot - 1
'Label5.Text = PostponeReboot
End Sub
End Class
In the first "If" sentence of below I want to start the timer and hide the form, and in the second "If" i want to bring it back the form, but the form remains hidden.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked Then
MessageBox.Show("Rebooting")
'Shell("shutdown -r -f -t 60")
Form1.Close()
End
ElseIf CheckBox2.Checked Then
MessageBox.Show(PostponeReboot)
Timer1.Start()
Me.Hide()
End If
If PostponeReboot = 0 Then
Me.Show()
Else
Me.Hide()
End If
End Sub
I've tried putting the second "If" sentence in another place but don't work, what I'm doing wrong.
I assume here that your Timer1 class raises the Timer1.Tick event every x time after Timer1.Start() is called. The fact that the form can hide tells me Timer1.Start() isn't a blocking method. As such, your second if statement will be verified right after you hide the form, without waiting for the PostponeReboot variable to reach zero. This particular button handler would then exit and your form would remain hidden. What I see is that you already have an event handler for each tick of your timer. Why not use this handler to verify the state of your PostponeReboot variable?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PostponeReboot = PostponeReboot - 1
If PostponeReboot = 0 Then
Timer1.Stop() 'I would assume
Me.Show()
End If
End Sub
Although, I would recommend you to try other solutions, like having your timer raise an event only when it reaches the elapsed time (so you don't have to handle each ticks unnecessarily). I would also recommend looking into an Universal Windows App with Toast Notifications as you could set a Notification to appear at a set time (handled by Windows) so that you don't have to have a thread running in the background for this.

Alternatives for 'Handles Me.FormClosing' in modules

I would like to know of any alternatives to the 'Handles Me.FormClosing' in modules.
I have created code that will display a confirmation message upon clicking the 'X' button. The issue is, I need to put this code into a module to use on multiple forms where i can call it, however, when I attempt to do this the 'Handles Me.FormClosing' will not work.
Here is the code i am using:
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
FrmLogin.Close()
Else
e.Cancel = True
End If
End Sub
Whenever you create a new form:
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.Close
This will route all closing events you want through that sub. Then just remove the Handles Me.Closing unless there is something you aren't showing us that makes it relevant.
Try This. I made changes to work as you want :
Module YourModule
Public Function AskFormClosing() As Boolean
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Return True
Else
Return False
End If
End Function
End Module
And then
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If YourModule.AskFormClosing Then
Application.Exit
Else
e.Cancel = True
End If
End Sub
One possibility ist to create a function in your module which shows the MessageBox and exits Application if "Yes" is clicked else returns False.
Module YourModule
Private dontAskAgain As Boolean
Public Function AskFormClosing() As Boolean
If dontAskAgain = False Then
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
dontAskAgain = True
Application.Exit()
End If
End If
Return dontAskAgain
End Function
End Module
And then you only need to set e.Cancel to the inverted result of the function.
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not YourModule.AskFormClosing
End Sub
If you prefer to use AddHandler as suggested by other people, you could use the following method that will lead to the same result.
Module YourModule
Public Sub AskFormClosing(sender As Object, e As FormClosingEventArgs)
If dontAskAgain = False Then
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
dontAskAgain = True
Application.Exit()
Else
e.Cancel = True
End If
End If
End Sub
End Module
And then add the Handler like this:
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.AskFormClosing
But for your main-Form you will need to add the Handler in the Loading Event, like this:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Me.FormClosing, AddressOf YourModule.AskFormClosing
End Sub
Rather than placing this code in a module for reuse, consider create a base Form that has the behaviour you wish to share and then have you other forms inherit from this base Form (rather than from System.Windows.Forms.Form).
See How to: Inherit Windows Forms on the Microsoft website.

vb.net radiobutton progromatically setting 'Checked' and not cause 'Click' event

I am loading a form with values from stored settings from the registry
Typically :
If sFormat = TG_ReportFormatDft Then
RadioButton1.Checked = True
........
Else
RadioButton2.Checked = True
..........
End If
TG_ReportFormatDft is a string constant and is of no significance. The radio buttons are grouped correctly and manually clicked behave correctly.
Later in the procedure I check if there has been a manual change :
'Follow what the user is doing
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
msReports_Format2 = TG_ReportFormatDft
DoButtons(True)
End Sub
Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
msReports_Format2 = TG_ReportFormatAlt
DoButtons(True)
End Sub
Imagine my surprise when these downstream subroutines are triggered without a mouse click. It would appear that :
RadioButton1.Checked = True --- triggers the mouse click event.
I can understand an 'On Change' event but the mouse click has not happened.
How can I prevent this 'click' event from propagating ?
Declare a class level boolean variable "ClickedFromCode". Now when you are setting the values from code set the boolean to true. See below sample code.
Private ClickedFromCode As Boolean
Private Sub Intialize
ClickedFromCode = True
If sFormat = TG_ReportFormatDft Then
RadioButton1.Checked = True
Else
RadioButton2.Checked = True
End If
ClickedFromCode = False
End Sub
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
If ClickedFromCode Then
Return
End If
msReports_Format2 = TG_ReportFormatDft
DoButtons(True)
End Sub
Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
If ClickedFromCode Then
Return
End If
msReports_Format2 = TG_ReportFormatAlt
DoButtons(True)
End Sub

VB.net ByVal and ByRef doesnt work

So i have a couple of subs and i want to share variables between them... I'm new to programming and i don't understand how to use ByVal and ByRef. I'm trying to make a login page.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles LoginButton.Click (ByRef BlnCorrectPassword As Boolean, ByRef BlnCorrectLogin As Boolean)
If blncorrectlogin = True And blncorrectpassword = True Then
MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
Else
MsgBox("The login or password is incorrect, please try again.")
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim BlnCorrectLogin As Boolean
If TextBox1.Text = "login" Then
BlnCorrectLogin = True
End If
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
Dim BlnCorrectPassword As Boolean
If TextBox1.Text = "password" Then
BlnCorrectPassword = True
End If
End Sub
End Class
You can dramatically reduce the complexity if the username and password are checked in a sub and only check them on the button click event. You don't need to check if they're correct every time someone changes the values of the textboxes.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
If CheckLoginDetails() = True Then
MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
Else
MsgBox("The login or password is incorrect, please try again.")
End If
End Sub
Private Function CheckLoginDetails() As Boolean
If TextBox1.Text = "login" AndAlso TextBox2.Text = "password" Then
Return True
Else
Return False
End If
End Function
if you want to use BlnCorrectPassword or BlnCorrectLogin with other Subs or Functions define them as global variables within that class as such
Public Class Form1
Private BlnCorrectPassword as Boolean = False
Private BlnCorrectLogin as Boolean = False
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
If blncorrectlogin = True And blncorrectpassword = True Then
MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
Else
MsgBox("The login or password is incorrect, please try again.")
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "login" Then
BlnCorrectLogin = True
End If
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If TextBox1.Text = "password" Then
BlnCorrectPassword = True
End If
End Sub
End Class
EDIT: also you do not have to check the entered Text with every letter (TextChanged) as long as you do not use it, so just check the entered text when you click the LoginButton, in that way you don't need boolean variables and have two less Subs
The code is still ugly and can definitely be simplified:
1.) blncorrectlogin = True And blncorrectpassword = True => You do not need to write = TRUE ; AND should not be used , use AndAlso.
2.) MsgBox is deprecated. Use MessageBox.Show instead.
3.) Textchanged can be concatenated. If like this:
If TextBox1.Text = "login" Then
BlnCorrectLogin = True
End If
are totally crap and senseless.
4.) Strings should not be compared with "=" , use .Equals for that.
Option Strict On 'Always write this
Option Infer Off 'Always write this
Imports System.IO
Public Class Form1
Private BlnCorrectPassword As Boolean = False
Private BlnCorrectLogin As Boolean = False
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
If sender.Equals(TextBox1) Then
BlnCorrectLogin = TextBox1.Text.Equals("login")
End If
If sender.Equals(TextBox2) Then
BlnCorrectPassword = TextBox2.Text.Equals("password")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not (BlnCorrectLogin AndAlso BlnCorrectPassword) Then
'do some stuff
MessageBox.Show("The login or password is incorrect, please try again.")
Exit Sub 'exit the sub
End If
'login data correct
MessageBox.Show("This part is still being developed!")
End Sub
End Class