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
Related
I am looking for a way to make a panel which can be expanded and collapsed with a little arrow or button. I tried to find some examples, nothing worked so far. I tried to resize a group to 0px but the problem was when i did that the panel below it stayed in a same place, and I wanted it to slide upside, to save screen real estate.
I have a bunch of labels and text boxes which provides me data, but takes up a lot of space, and i grouped them together.
All help would be appreciated.
David
Simple in window Form
Insert a button and write code
Simple Example:
Public Class Form1
Dim panelshow As Boolean = False
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If panelshow = False Then
panelshow = True
Panel1.Show()
Else
Panel1.Hide()
panelshow = False
End If
End Sub
End Class
You can change name of variables, buttons and panels.
Set the panelshow to false if on application start your panel is collapsed otherwise to true if your panel is visible on startup and want to hide later by user.
Most people have answered the question about the collapse and expand, but to answer your question about "animation" so that it really has a good slide feel. For winforms i recommend this library on github DotNet Transitions
you can then write very simple code to give it animated transition.
using the transitions library, this code would collapse the panel in half a second:
Transitions.Transition.run(pnl, "Height",
initialValue:=pnl.Height,
destinationValue:=0,
transitionMethod:=New TransitionType_EaseInEaseOut(500)
)
If you want to use animation in win forms like when panel collapsed and the additional space should be covered with content then you can use
Control.Top() And Control.Left() Property ' control = your control name on which content is written like label, textbox etc.
Or second choice
To resize your content (like increasing the size etc).
Depend on your choice as I can't see your form presentation but advise you on the base of my imagination.
For example my panel height is 100 and when i collapsed I want my next control should move upward by 100 (Just suppose)
Suppose, my next control is RichTextBox
Then, you can do change here :
Public Class Form1
Dim panelshow As Boolean = True
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If panelshow = False Then
panelshow = True
Panel1.Show()
RichTextBox1.Top = RichTextBox1.Top + 100
RichTextBox1.Height = RichTextBox1.Height - 100
Else
Panel1.Hide()
panelshow = False
RichTextBox1.Top = RichTextBox1.Top - 100
RichTextBox1.Height = RichTextBox1.Height + 100
End If
End Sub
End Class
Just add a method in addition to our previous code to do this thing.
Don't mind my grammar mistakes.
I am not perfect in English.
For more help comment or post your code (if you have any kind of problem)
I am using a theme from the web for my vb.net application and the textbox does not have scrollbars or a scrollbar property. The theme did come with a VScrollBar Control, but I don't know how to add code to it to make it scroll the textbox like normal. Can anyone help me?
These are Custom Controls.
It's a Windows Form. (WinForms)
Textbox and its Properties:
Vertical scroll bars can be added to TextBox form objects, but however they must be Multiline:
This can either be done by setting Multiline to True and ScrollBars to Vertical:
or it can be done via code, programmatically, as per se:
TextBox1.Multiline = True
TextBox1.ScrollBars = ScrollBars.Vertical
You can set ScrollBars to be only horizontal, vertical, both, or none (default):
Remember, you should:
Be sanitizing the user's input if you're sending the textbox's contents off to a database
Limit the amount of characters that the user can input (see below)
Be using proper programming technique by naming your objects properly, for example, try not to name your textbox TextBox1
As mentioned above, you may want to show the amount of characters the user can input, for example:
the code for this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
text1.MaxLength = 140
charsLeft.Text = "0/" + CStr(text1.MaxLength)
End Sub
Private Sub textHasChanged() Handles text1.TextChanged
charsLeft.Text = CStr(text1.TextLength) + "/" + CStr(text1.MaxLength)
End Sub
Consider an empty WinForms application created using VS2010.
It has a custom TextBox class with the following code:
Public Class DummyTextBox : Inherits TextBox
Private Const FONT_SIZE As Single = 14.25!
Private Const FONT_FAMILY As String = "Microsoft Sans Serif"
Private Sub Me_ParentChanged(sender As Object,
e As System.EventArgs) Handles Me.ParentChanged
'this one does not work, it causes designer
'to lose its controls once in a while
Me.Font = New Font(Me.Parent.Font.FontFamily, FONT_SIZE)
'if I use a constant value instead, like below, it works fine
'Me.Font = New Font(FONT_FAMILY, FONT_SIZE)
End Sub
End Class
So basically a TextBox with increased font size, same family as the parent form.
What happens is that after being put on the form, and then built, the control sometimes disappears from designer view. If you run the project, it's usually showing fine. Close/reopen a form and it's there again.
Sometimes, however, the control will disappear completely (I was not able to reproduce this 100% of the time), so you'd have to add it again and set the properties. If multiple controls are placed in one shot, usually only one of them disappears like that. Controls are more likely to disappear after being moved around on the form.
What's going on?
According to my research, Me.Parent can sometimes be Nothing inside ParentChanged, so that line throws an exception, which is never displayed to the user (it only happens at design time). Putting a Try/Catch around it helps verify this fact. It looks like Windows Form Designer likes detaching controls and attaching them back at its own discretion.
To solve the problem, need to verify there exists a parent, and only then set the Font.
So changing this:
Me.Font = New Font(Me.Parent.Font.FontFamily, FONT_SIZE)
To this:
Dim parent As Control = Me.Parent
If parent Is Nothing Then Return
Me.Font = New Font(parent.Font.FontFamily, FONT_SIZE)
Makes the issue go away and does not affect runtime in any way.
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.
I have a 1080p touchscreen application. When a modal pops up, i want to emphasize that by darkening the main form.
Right now i use a second form, the size of the main form, that is black and has 50% opacity. Whenever a modal needs to appear, i open the opaque form, and then open the desired modal.
I feel this is a bit devious for my purpose. Its also not asshole-proof that when the user alt tabs, the forms will glitch out of sequence.
Is there a better way to achieve the darkening effect. Perhaps by darkening the main form from within itself?
Solved it myself by doing the following:
Place a hidden picturebox with dock:fill on the main form,
Take a screenshot of the current screen and darken it
assign the image to the picturebox and make it visible
open the modal in a new win
when the modal is dismissed
hide the picturebox
It really stupid that VB.net doesn't have this function built into it. Here's what you do to get around it:
Make a new form and call it Shade. I'm going to assume your main form is called frmMain. For the sake of clarity, lets assume the form you're launching is called dlgX.
Add the following lines in the Load event of dlgX (that's the sub with dlgX.Load or Me.Load or MyBase.Load):
Shade.Opacity = 0.001
Shade.Show()
Shade.Location = frmMain.Location ' Form location will only update if the form is visible.
Shade.Hide()
Shade.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'This gets rid of the windows Titlebar and window border.
Shade.Size = frmMain.Size
Shade.BackColor = Color.Black
Shade.Opacity = 0.5
Shade.Show() ' Form size will only update the next time you show it.
Shade.TopMost = True ' Puts Shade over main form
Me.TopMost = True ' Puts current form over shade
Under all events that dismiss the form dlgX (OK.click, Cancel.click, etc), add the following lines:
Shade.Close
Or you can even make your own sub that handles all events where the form is closed:
Private Sub DispelShades(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
Shade.Close()
End Sub
This is way simpler than the PictureBox scenario and you don't have to mess with layering issues and having to ensure that the PictureBox renders on top of everything (for example, tabs really do not like having things rendered above them and they will not let you render a picture box above them). Rendering a black semi transparent form above your main form gets around all these headaches.
If you have multiple forms to shade, just make a Shad1, Shade2, Shade3 etc.
This is pretty obvious but it's worth stating: if you're shading the main form, you'll also want to make it unclickable by opening dlgX via dlgX.ShowDialog and not dlgX.Show
Here is some code, very similar to the method in Thomas's answer. Note to use the Darkness property in a Try...Finally block, to make sure you never leave the form in the dark state.
Public Class Form1
Private _PB As PictureBox
Public WriteOnly Property Darkness
Set(value)
If value Then
Dim Bmp = New Bitmap(Bounds.Size.Width, Bounds.Size.Height)
Me.DrawToBitmap(Bmp, New Rectangle(Point.Empty, Bounds.Size))
Using g = Graphics.FromImage(Bmp)
Dim Brush As New SolidBrush(Color.FromArgb(125, Color.Black))
g.FillRectangle(Brush, New Rectangle(Point.Empty, Bmp.Size))
End Using
_PB = New PictureBox
Me.Controls.Add(_PB)
_PB.Size = Bounds.Size
_PB.Location = Bounds.Location - PointToScreen(Point.Empty)
_PB.Image = Bmp
_PB.BringToFront()
Else
If _PB IsNot Nothing Then
Me.Controls.Remove(_PB)
_PB.Dispose()
End If
End If
End Set
End Property
Private Sub btnDialog_Click(sender As Object, e As EventArgs) Handles btnDialog.Click
Try
Darkness = True
MsgBox("Modal dialog")
Finally
Darkness = False
End Try
End Sub
End Class