Auto Login Procedure (Hide Form) vb.NET Windows Forms - vb.net

Okay, I am having a bit of trouble here. I am creating a log in window for an application, but I am trying to get the application to automatically log in (i.e. perform the functions that happen when the user logs in) when it starts, without showing the log in screen, if the settings already have a stored email and password. I have a notification System Tray Icon that shows when the app is running, and when the form is not visible, a balloon notification pops up so the user knows that it is still running, and click on the icon to open the log in screen.
Take a look at the following code. I know that this If Not event is being called and working correctly, because it performs everything inside the statement EXCEPT hiding the form. Why does it not change to invisible? I also tried Me.Hide, and same issue. The Balloon Notification pops up, the text boxes fill with the previously stored data...but the form stays visible...
Private Sub RadFrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Checks settings to see if email and password have already been stored and enters them into text fields, proceeds to automatically update access list
If Not String.IsNullOrEmpty(My.Settings.Email) And Not String.IsNullOrEmpty(My.Settings.Password) Then
TxtEmail.Text = My.Settings.Email
TxtPassword.Text = My.Settings.Password
Me.Visible = False
'Displays Balloon Tip
ntfySystemTrayIcon.ShowBalloonTip(800)
End If
End Sub
As an added note, I added a test button to hide the form, and it works perfectly:
Private Sub BtnHide_Click(sender As Object, e As EventArgs) Handles BtnHide.Click
'Hides form(for testing notification tray icon and balloon tip
Me.Visible = False
ntfySystemTrayIcon.ShowBalloonTip(1000)
End Sub

(removed my stupid default debug instructions since they did not help at all)
Update
okay, so there were similar questions before, take a look here: C#/.NET - WinForms - Instantiate a Form without showing it
short explanation: usually something like form1.show is used, so it is always changed to visible = true after the form_load is finished.
Either use the instructed event form_shown and add the visible=false
or another user recommended to change start properties to minimized and activate to hide program in taskbar. This helps to prevent that annoying flickering. I guess after that you can change the options back.
Update 2 The following seems to work well:
Private _IsVisible As Boolean
Public Property IsVisible() As Boolean
Get
Return _IsVisible
End Get
Set(ByVal value As Boolean)
_IsVisible = value
If _IsVisible Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
Me.Visible = True
Me.Activate()
Else
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Visible = False
End If
End Set
End Property
If you want to get rid of the small taskbar flickering, then change the forms property showInTaskbar. When it is changed during the form_load, then there seem to be a short movement at the taskbar.
And to make it perfect, in form.Shown add following code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Visible = IsVisible
End Sub
now it is enough to use
IsVisible = False
in form_Load, or if you want to show it
IsVisible = True

Just some ideas:
If all your tasks are completed in the _Load event try just calling End. Of course that would remove your tray icon as well.
Another possibility is to call Me.Visible in the _Shown event. This may cause a flash on the screen. If so perhaps you could position the form off the screen in _Load.

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?

Forms acting unexpectedly

I'll try to explain this best I can.
I have windows form application that contaions numerous forms. The first form
to open is a non modal form that acts as command form to issue various formats to
the underlying reservation program. A second form also opens which is basically my
Main form for the application. This form contains multiple check boxes to run methods
that make changes to the reservation program. This form is also modeless. Each box that
is checked runs concurrently and performs methods to delete, change and add to the
res program below. Various boxes may be checked at any given time. Below is code to
handle the checkboxes:
Private Sub frmOWTMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Enter Then
Me.Hide() 'here i want to hide the OWTMain form
Call ckforPNR()
If Me.cbPricing.Checked Then
Call doPricing()
Me.cbPricing.Checked = false
End If
If Me.cbUdids.Checked Then
frmUdids.Show()
Me.cbUdids.Checked = False
End If
If Me.cbMod.Checked Then
Call doModUdids()
End If
If Me.cbFare.Checked Then
Call doFareSavings()
Me.cbFare.Checked = False
End If
End If
Me.show() 'This is the problem. This runs during method calls. All I'm
'looking to do is re display the OWTMain form.
End Sub
At this point I would want to re display the OWTMain form above. This is
where I am having an issue. As you can see, a number of methods are called
which involve showing other forms to the user. I do NOT want any of these
forms to be modal because I want the original first opened form to still be
able to issue commands to the res program toview information needed by the other forms.
Here is an example of some of the method calls:
Private Sub doPricing()
Dim myPrice As New Pricing 'a call to another class that handles pricing
If myPrice.getTQT = False Then
frmAddPricing.ShowDialog() 'showing new forms
Else
frmCurPricing.ShowDialog()
End If
End Sub
Private Sub doFareSavings()
Dim myPrice As New Pricing
If myPrice.checkForFS = False Then
frmFS.ShowDialog() 'showing new forms
End If
If myPrice.checkForFS = True Then
frmFSVerify.ShowDialog()
End If
End Sub
When I call any of these methods the form OWTMain shows prematurely while other
forms from the called method are still running. I expected the code at the top to run
in order of the calls but that is not the case. I just want to re display the OWTMain
form after all the code is run. I have not been able to figure this out without
creating a ton of code to minipulate the opening and closing of the windows.
Any help with this would be greatly appreciated. Thank you.
John
This will check if all sub forms are closed and then re-show the main form.
remove the exitsing me.show()
Public Sub ReShowMainForm()
' add all sub forms to this check
if frmFS.Visible = false andalso
frmUdids.Visible = false andalso
frmFSVerify.Visble = false then
Me.Show
End If
End Sub
change xyzForm.Show to xyzForm.Show(me) to make the main form available as parent to the sub forms.
Handle the FormClosed event in each sub form and add the following code
DirectCast( me.parent, frmOWTMain).ReShowMainForm()

Checkbox events when Form is opened and closed fire when form is reloaded

First... I am open to skinning this cat a different way if I am going at it wrong to begin with. Using VB 2010 .net 4.0 and I am very much a beginner.
I am making a product billing application that has a main form and a subform with additional options. Whenever that subform is reopened after being opened once, the checkbox events that were selected are blank by default. If I recheck them (so someone can uncheck) then any that are rechecked all refire and increase the variable again.
I ultimately need to be able to open that second form after closing it, display any checkboxes that were selected before as selected again and not increase the variable in the process.
Main form Checkbox code to set booleans and increase or decrease subtotal variable of most used products.
Private Sub chkbox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox1.CheckedChanged
If chkbox1.Checked = True Then
bChkbox1 = True
Subtotal += 15
Else
bChkbox1 = False
Subtotal -= 15
End If
End Sub
Main form button to launch subform with all products listed.
Private Sub btnAllProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllProducts.Click
Form3.Show()
End Sub
Subform checkbox code works perfectly the first time it is opened but not when relaunched.
Private Sub chkbox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox2.CheckedChanged
If chkbox2.Checked = True Then 'also tried without the nested if with same results
If Me.IsHandleCreated = True Then 'me.visible behaves the same way
MsgBox("form visible true")'launches after clicking button but before form is actually on screen
Form1.bcheckbox2 = True
Form1.Subtotal += 105
End If
Else
Form1.bcheckbox2 = False
Form1.Subtotal -= 105
End If
End Sub
Booleans are used to check boxes that were checked on the main page or when it was open before.
If Form1.bcheckbox2 = True Then
chkbox2.Checked = True
End If
As I said, I can completely rework the code if it makes sense to do so or just fix something if I have made some sort of mistake.
For example, I was thinking of changing to wipe the subtotal on each form load and rebuild it based off the toggled booleans but it seems like there should be a much more elegant way with less overhead and I am just doing something incorrectly.
It is not common to have to tell checks and radios to ignore events while loading the form. You just need an Ignore or Loaded flag:
Public Class Form1
Private ignore As Boolean = True
...
Private Sub Form1_Load(...
' do normal stuff
ignore = False ' should be the ONLY place it is set
End Sub
Private Sub CheckBox2_CheckedChanged(...
If ignore Then Exit Sub
End Sub
The Form Designer code will fire events as it creates the form and controls, which CAN be handy for initializing stuff but often it causes trouble. Some controls will even get the same event twice. There isnt really a "reload" action for forms. If you hide them, Show() won't fire the Load event again.
You can avoid the flag and manually add the handlers for the troublesome controls when the form loads, but that can be tedious if there are lots of them. Flags can be abused and misused, but if it is set in that one spot only, its fine.
If someone is looking for alternative or have similar problem here's my workaround to detect event change so checkbox wouldn't get triggered on re-load:
If ((Me.CheckBox2.Value <> Sheets(1).Range("t6").Value) And (Me.CheckBox2 = True)) = True Then
' do your stuff
Me.CheckBox2.Value = False
Else
Me.CheckBox2.Value = True
End If
Where Sheets(1).Range("t6").Value is where checkbox2 value is being stored.
I have this assigned to a msgbox input so when vbno event is being triggered else is executed.
Cheers.

VB.NET - Background fade like in the UAC message?

Hello,
When the UAC message is displayed in Windows Vista, 7 or 8 the background becomes inaccessible until the user selects from the message dialog. Can this be done with VB.NET program to make the background inaccessible until the user selects from the Form?
What I want is what happens to the background when UAC or similar message is shown like the image below,
That's pretty easy to do, just display a black borderless form with opacity and the dialog on top of it. Do keep in mind that this of course cannot provide the same level as protection as the UAC prompt provides, you cannot use the secure desktop yourself.
Public Shared Function Plexiglass(dialog As Form) As DialogResult
Using plexi = New Form()
plexi.FormBorderStyle = FormBorderStyle.None
plexi.Bounds = Screen.FromPoint(dialog.Location).Bounds
plexi.StartPosition = FormStartPosition.Manual
plexi.AutoScaleMode = AutoScaleMode.None
plexi.ShowInTaskbar = False
plexi.BackColor = Color.Black
plexi.Opacity = 0.45
plexi.Show()
dialog.StartPosition = FormStartPosition.CenterParent
Return dialog.ShowDialog(plexi)
End Using
End Function
Tweak the Opacity value as desired, the higher the value the darker the background. Looks like this on a little test program:
You are looking for ShowDialog(). This means it will bring the control in front of others and here's a quick example.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim nform As New Form
nform.Text = "Test Form"
nform.ShowDialog()
End Sub
If you use BringToFront() method all this will do is bring it to front in the z-order. Same with TopMost(), but the only difference in TopMost() is that window will always be at top.
Thanks!
MrCodexer

How to hide button permanently after checked

I have a button on my form that I would like to hide permanently if the box is checked. In other words, if the user checks the box, the next time the form shows up, the button should not show up.
I would think that I have to capture the check event on the main form load event and store that somehow, but I am not sure how to that.
I wrote the following, but when the forms loads again, the button shows up.
Private Sub hideMe_CheckedChanged(sender As Object, e As EventArgs) Handles hideMe.CheckedChanged
If hideMe.Checked = True Then
frmRegistration.Show()
hideMe.Enabled = False
End If
End Sub
You need to save something as a setting or such and reload it each time the app starts (guessing at names and functionality):
Form Load maybe:
If My.Settings.IsRegistered = True Then
btnRegister.Visible = False
chkSomething.Checked = True
End If