Start a process in a VB form without clicking a button - vb.net

Here is my situation.
I have a windows from application in VB.NET. In the my first form there are two checkboxes. If the one is checked it goes to Form2, if only the second one is checked it goes to Form3. If both are checked then i want first to go on Form2 and when this process is finished go to Form3. All good until this point.
However, I want Form3 to start executing its code (which is inside a background worker) without pressing any button (which is the case when only the second checkbox is checked). Is this possible?
Thanks In advance!
Extra info....
To navigate between the Forms I use a Show/hide/Close scheme. For example:
If CheckBoxTrain.CheckState = CheckState.Checked Then
Me.Hide()
Form2.Show()
ElseIf CheckBoxTrain.CheckState = CheckState.Unchecked And CheckBoxEvaluate.CheckState = CheckState.Checked Then
Me.Hide()
Form3.Show()
At the end of the code in form2 i check if the second checkbox is also checked:
If Form1.CheckBoxTrain.CheckState = CheckState.Checked And Form1.CheckBoxEvaluate.CheckState = CheckState.Checked Then
Form3.Show()
Me.Hide()
End If
Then in Form3 there is a button in which after pressing it i have all the code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To n_monte
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
Next
End Sub
All i want is for this last piece of code is to start running without pressing any buttons (only for the case in which both checkboxes are checked)

Declare a Boolean property in Form3 and set that property in Form1 based on whether or not both CheckBoxes are checked. In the Load event handler of Form3, start the BackgroundWorker or don't based on the value of that property.

Related

Radio buttons fire in VB net at form load

I have a simple VB net form with two radio buttons in a group box and button outside the group box that calls another form that sets up other parameters. The radio buttons both send data over the serial port.
Form1_load event has a boolean value form_loading = True. This is checked in the rbtn handler and if true should exit the subroutine. On debug, the check changed even fires one button's event that is checked at design time and at this point the form_loading value is set to false and I have no idea why. There is no form_loading = false statement. If I remove the rbtn handler, the form_loading = True persists when the other form is called and returned. The groupbox with buttons is activated as it sees a rbtn1-CheckChanged when the form loads and form_loading value get set to false. I suspect that the rbtn event is firing as the form begins to load, before the form_loading = True statement is reached, but how do I stop it firing the button event?
As its stands, when debug start, there is an IO exception error:
Serialport is closed, and the code associated with the button is in
the buffer to send to a (closed) com port
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnDon.CheckedChanged
If form_loading Then
Exit Sub
ElseIf rbtnDoff.Checked = True Then 'event fires when other button checkchanged = true, this stops it
Exit Sub
Else
data_out = (SOT + "N" + EOT)
SendtoBoard(data_out)
End If
End Sub`enter code here`
Thanks for your help Everyone. Elsewhere I found this tip: Remove the "handles rbtnDon checkchanged" from the sub:
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs)handles rbtnDon checkchanged
and add this to the form1_load sub:
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
That seems to have solved the problem completely.
I could not add the startup code on my original post as for some reason the box will not accept two lots of separate code - or I am not doing it right:
It is here:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ccbComPort.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
If ccbComPort.Items Is Nothing Then
lblMessage.ForeColor = Color.Red
lblMessage.Text = "No Serial Ports found"
Else
ccbComPort.Text = My.Settings.oldPort
End If
ccbBaudrate.Text = My.Settings.oldBaud
pnlComPorts.Visible = True
'form_loading = True
rbtnDon.Enabled = True
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
End Sub
Originally I had one form with a panel for the Com port setup and everything was fine, the issue only started when I moved the port set up to form2, after which the rbtnDon event fired before form1 started.
I did try the Sub New() approach but then I just get a small blank form on debug. Adding Form1_load to it results in "NotImplemented Exception".
The method described above seems OK and fairly simple to add but maybe it is not good practice?

Winforms - MDI Parent refresh/re-activate

I have a MDI Parent form which has a menustrip for the application. My application startup file is the MDI Parent form which on load calls a child login form. Code as below:
Dim myForm As Form = New Login
Dim formResult As DialogResult = myForm.ShowDialog()
If formResult = Windows.Forms.DialogResult.OK Then
If LoginSucceeded = True Then
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
myForm.Dispose()
End If
End If
The menustrip has a Logout label which when clicked disables the menu strip and displays the login form again.
The boolean field LoginSucceeded determines a successful validation of the user credentials and sets the menu according to the access given to that user. My problem is the first time the main menu on the MDI parent is set properly based on the user's access. After logging out and logging in again, i wanted to set the main menu accordingly again which is not happening.
The Form_Load event on the MDI Parent is being executed only once.
Any tips of re-painting the MDI parent when it receives focus the 2nd time onwards.
Thanks,
ZK
My code for the Logoff is as below:
Dim blnLogout As DialogResult = MessageBox.Show("Are You Sure You Want To Logout?", "eApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If blnLogout = Windows.Forms.DialogResult.Yes Then
SetToolbarMenuStyle()
tabMainMenu.Visible = False
LoginSucceeded = False
blnShowLoginTab = True
Dim myForm As Form = New Login
myForm.MdiParent = Me
myForm.WindowState = FormWindowState.Normal
myForm.Show()
End If
Move your login code to its own method in your main form so you can call it multiple times:
Public Class Form1
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown ValidateLogin()
ValidateLogin()
End Sub
Private Sub LoginLogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoginLogoutToolStripMenuItem.Click
ValidateLogin()
End Sub
Private Sub ValidateLogin()
' disable appropriate main form elements so they can't access anything:
Me.tabMainMenu.Visible = False
Using myForm As New Login
If myForm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' login succeeded: re-enable main form elements
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
Else
MessageBox.Show("Login Failed")
End If
End Using
End Sub
End Class
You also don't need the "LoginSucceeded" variable. You can pass a success/failure back to the main form by setting DialogResult to OK in your Login form:
Public Class Login
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If True Then ' <-- perform your check
Me.DialogResult = Windows.Forms.DialogResult.OK ' only return OK if login has succeeded
End If
End Sub
End Class
Here are presumptions on your code, I'm guessing you added the code about on the Form.Load event. The Form.Load event only gets raised when the form is shown for the first time.
According to MSDN
Form.Load Event
Occurs before a form is displayed for the first time.
And now, when you Log-Off, you're setting the visibility of the form to false. So what I suggest is you move your code from the Form.Load event to the Form.VisibleChanged event.
According to MSDN
Form.Load Event
Occurs when the Visible property value changes.

Visual Basic SplashScreen not working with Version Select Form

I'll try and keep this brief without leaving out details. I'm working in Visual Stuido 2012 using .NET 4.5
I have a Splash Screen, a "version selection" form as my main startup form, and then from there it branches out in two ways based on the user's choice.
The Version Select can save the user's choice for the future, and checks if they have a saved setting, and if they do it skips the selection form and goes straight to their version. The problem I'm encountering is that when the user has a saved version, the splash screen remains up and never closes unless I force it to.
I tried using the MinimumSplashScreen time in the application events but that hasn't helped. This only happens if the user has a version saved.
Any thoughts on this? I can post more details as needed. Thanks in advance
From Comments
Private Sub Version_Selection_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.VersionSelected = "OSRS" Then
'Code to close initial form and load old school
Dim OSmain As New OldSchoolMain
OSmain.Show()
Me.Close()
ElseIf My.Settings.VersionSelected = "RS3" Then
'Code to close intital form and load RS3
End If
End Sub
Private Sub btnConfirmSelection_Click(sender As Object, e As EventArgs) Handles btnConfirmSelection.Click
If radOSRS.Checked = True Then
If cboxSaveVersion.Checked = True Then
My.Settings.VersionSelected = "OSRS"
End If
Dim OSmain As New OldSchoolMain
OSmain.Show()
Me.Close()
ElseIf radRS3.Checked = True Then
If cboxSaveVersion.Checked = True Then
My.Settings.VersionSelected = "RS3"
End If
Dim RS3main As New RS3Main
RS3main.Show()
Me.Close()
End If
End Sub
Looking at your code, I think the problem with your Splash Screen is that you are closing the startup form before it finishes. I would try using the Form Shown event since that doesn't get fired until the SplashScreen has finished, I also am Minimizing the form to try to keep it from flashing briefly on the screen. See if something like this works for you.
Private Sub Splash_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
If My.Settings.StartupForm = "Form1" Then
Dim frm As New Form1
frm.Show()
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Close()
ElseIf My.Settings.StartupForm = "Form2" Then
Dim frm As New Form2
frm.Show()
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Close()
End If
End Sub

Change Close button in window form

Is there a way to change the command of the (x) Button?
I want to show another form and hide the current form instead of closing the program.
where should I put this? :
Me.Hide()
Form.Show()
I tried puttin it on the Form Closing/Closed Event, but nothing happen, am I missing something?
Sure, the FormClosing event is made to do that. You'll need to pay attention to the reason the form is being closed, something like this:
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
MyBase.OnFormClosing(e)
If Not e.Cancel AndAlso e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
Dim frm = New Form2()
frm.Show()
Me.Hide()
End If
End Sub
Do note that you have to give a way for your user to terminate the app.

How can we show the parent form after showing the report Form?

I have a problem in showing the parent form and report form at the same time.When user click on the parent form for print it should pop up with yes or no button when user click on yes button it should print the form screen shot image,if we click "No" button it should show the crystal report.
When we click 'No' button it should show the crystal report.So to show the messagebox i have done like this
me.hide()
if MsgBox('Do you want to print screen shot image?') then
'Print screen shot image
me.Show()
else
'Show CxReport
me.Show()
end if
When I Did like this the parent form is strucking and unable to perform operations.
It's not standard practice to hide a form when showing a dialog. Completely remove the me.hide and me.show lines and try again.
I know that this question is an old one and you probably have figured it out by now, but I thought I would add an answer for future reference. All Forms have a FormClosing and FormClosed Event that you can attach a handler to in your creating Form. Here is a simple example of what I am trying to say.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
If MsgBox("Open Report?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim frm2 As Form2 = New Form2
AddHandler frm2.FormClosed, AddressOf ReportClosing
frm2.Show(Me)
Else
Me.Show()
'Do your work for printing form here
End If
End Sub
Private Sub ReportClosing(sender As Object, e As FormClosedEventArgs)
'Remove handler to prevent memory leaks
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf ReportClosing
Me.Show()
End Sub
End Class
Atlast I Found the way to get the access the parent Page hiddenField Value as given below
function getParentPageHField() {
var parentPageHField = window.opener.document.getElementById('hSelectedStandard').value;
document.getElementById('hStandard').value = parentPageHField;
}
Thanks for your inputs :-)