Forcing form to be on the screen - vb.net

Is there a way to force a form to be active and shown?
From another form i am doing:
Me.Hide
Form2.Show
When this code is run, the Form2 is minimized.
I've also tried:
Me.WindowState = FormWindowState.Normal
in the load event, and it still loaded it in minimized mode.

Try Me.BringToFront(); or you can set the property TopMost true and thus the form will be always on top.

Related

Minimize form when minimize button is clicked in VB.NET

I have a form which is maximized with this property:
Me.WindowState = FormWindowState.Maximized
and with FormBorderStyle = None. This means that when this form is opened, it can not be minimized, which is the behavior I want.
From this main form, there is a button which open another small form called 'Console' where I can see some messages thrown by the main form. This 'Console' form is with FormBorderStyle = FixedSingle, and this means that can be minimized.
The problem with this, is that when I minimize the 'Console' form, I can not opened again because the main form occupy the whole screen.
What I am trying to do is to minimize the 'Console' and be able to see it to maximize it again when I wish.
I tried to use events of the 'Console' form like SizeChanged or KeyUp and control when the minimize button is pressed.
All things I tried have gone wrong, and always when I press the minimize button, the form minimizes normally.
It is possible when I minimize the form by clicking on the minimize button, to see the minimize window down to maximize it when I want?
I accept any suggestions!
Finaly I've used this option: when I click to minimize, I just relocate the form and resize it. Maybe is a simple and manual option and for sure it will be better ways to do it, but this gives me what I need for now.
#HansPassant thanks for your help! I am going to look for information about docking layout manager as you suggested.
Here is the code:
Private Sub frmConsole_Move(sender As Object, e As EventArgs) Handles MyBase.Move
If Me.WindowState = FormWindowState.Minimized Then
Me.Size = New System.Drawing.Size(247, 0)
Me.WindowState = FormWindowState.Normal
Me.Location = New Point(padre.GMapControl.Location.X, padre.GMapControl.Location.Y + 1000)
Me.TopMost = True
ElseIf Me.WindowState = FormWindowState.Maximized Then
Me.Size = New System.Drawing.Size(447, 900)
Me.WindowState = FormWindowState.Normal
Me.Location = New Point(padre.Location.X + padre.GMapControl.Width * 0.9, padre.Location.Y)
End If
End Sub
I've used some buttons location of the main form to locate the 'Console' form, but it can be any value.
Thanks!

How can I "unhide" this form without triggering the _load event?

I'll just get straight to it, I have this code:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
startup.Show()
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Hide()
End Sub
This is going to be the form which loads first, having the entire project shutting down when this form closes (hence why I have to load this form first & calling the startup from this)
After the startup form has finished it's code, I have this code:
...
frmMain.ShowInTaskbar = True
frmMain.WindowState = FormWindowState.Normal
Me.Close()
How can I get the main form to load again without actually triggering it's _load event (thus avoiding it to trigger an infinite loop).
did you mean show the main form?
frmMain.Show()
frmMain.BringToFront()
Try this in order to show the hidden form:
frmMain.Show()
I'd suggest a better way to tackle this is to show the startup form from the application startup event. Your main form can then be a main form instead of being hidden.
startup.WindowState = FormWindowState.Normal
Call startup.Show()
Call startup.BringToFront()
Call startup.Activate()
The essential step to unhide from windowstate minimized, which is the windowstate of hidden forms, is to change the windowstate to normal. An example of this is shown in my first line of code. The other lines are for showing, bringing to the front and activating the formerly hidden form with the name startup. Goodluck!

All Controls Click event on form

Is there a simple way to Activate the form when any controls in the form is clicked like datagirdview, panel, menustrip, button, textbox, label, etc....
it happens that my project can show many different form and it's hard for me to activate one form when it's on the back of the active form. I need to clicked the border of the form to activate it.
most likely your problem arises because your form is MDI child.. you'll have to click the menu bar to activate the form.. but if you have a borderless form, clicking on controls wont activate the form.. again, this usually happens on MDI but it shouldnt happen on a regular winform.. unless you have other running events in the background that interferes with the process.
You question is not clear at all, and I don't know what you are trying to archieve, but for executing something with a click event you have to add the handler for every control.
If you are declaring it not dinamically just:
Private Sub ControlsClick(sender As Object, e As EventArgs) _
Handles Panel1.Click, Button1.Click, TextBox2.Click ' etc.
Me.Activate 'Or Whatever
End Sub
You have to add the handler for each control. The same if you do it dinamically:
Private Sub InitializeClickHandlers(sender As Control, Optional bChilds As Boolean = True)
For Each elem As Control In sender.Controls
AddHandler elem.Click, AddressOf ControlsClick(elem, New EventArgs)
If bChilds AndAlso elem.Controls.Count > 0 Then
Call InitializeClickHandlers(sender)
End If
Next
End Sub
Then, for every control in the form, you call it like: Call InitializeClickHandlers(Me)
Or, for every control inside a panel: Call InitializeClickHandlers(Panel1)

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

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.

How to have an invisible start up form?

I have an application that is a part of a solution of projects. In this project I would like for it to start up form to be invisible, but still have a notification icon in the tray visible for this form.
I know that adding me.hide into the form_load doesn't work. I tried adding a module that instantiates the startup form and I set it as the startup object. Although that didn't work either. I am running out of ideas to have this form invisible. Could anyone help out? I am using VB.NET.
Paste this in your form code:
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
Me.CreateHandle()
value = False
End If
MyBase.SetVisibleCore(value)
End Sub
The way that works is that the very first request to show the form, done by the Application class, this code overrides the Visible property back to False. The form will behave as normal after this, you can call Show() to make it visible and Close() to close it, even when it was never visible. Note that the Load event doesn't fire until you show it so be sure to move any code in your event handler for it, if any, to the constructor or this override.
Put this in the form's Shown event
Me.Visible = False
The easiest way is to set the opacity of the form to 0%. When you want it to appear, set it back to 100%
Here is another way that I've found to do this.
Set the form properties with
ShowInTaskbar = False
Then in the form's constructor add
WindowState = FormWindowState.Minimized
This is very easy to implement and works with no flicker. In my case I also use a NotifyIcon to access the program from the notification tray and just set
WindowState = FormWindowState.Normal
Show()
BringToFront()
In the Notify_MouseClick event handler.
To hide the form again after displaying it, just minimizing again doesn't quite do the job. In my case I use the Form_Closing event and just hide the form.
Hide()
Use Me.Opacity = 0 to hide the form on load event.
Then use the following code in the form.Shown event
Me.Hide()
Me.Opacity = 100
Just to throw out a completely different approach, have you considered not using the overload of Application.Run() that takes (and automatically shows) a Form? If you use the one that passes in an ApplicationContext (or more tyoically, your own subclass of ApplicationContext) then you can choose what your behavior is. See here for more details:
http://msdn.microsoft.com/en-us/library/ms157901
Try this:
Sub New()
MyBase.SetVisibleCore(False)
End Sub