Move Control Beyond Screen Resolution VB.Net - vb.net

If you move a control or form beyond the capability of what the screen can display, where does it actually go.
Here is an example I have been testing as I continue to learn.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button2.Location = New Point(23000, 200)
Button2.Text = Button2.Location.X
MsgBox(Button2.Text)
End Sub
When button1 is pressed it moves button2 to location 23000 on the x axis which is obviously beyond the screen resolution width.
My question is where is button2 been moved to if you know what I mean.

Remember what controls really are: some memory, a GDI resource, and some pixels painted on the screen.
If a control moves off the visible area the memory and GDI resources remain. The only difference is when it comes time to paint; in this case, since it's not visible, nothing is drawn.
However, just because it's not drawn doesn't mean it's not there. You can still get focus on the item and click it by tabbing (or other keyboard shortcuts) through the controls. If you just want to hide the item, you're better off setting .Enabled and .Visible to False.

Related

VB.net: How to hide the Windows Media Player visualisation pane, leaving control bar visible, on a modal form?

Using VS2017 VB Forms, I've made a little program to play .mp3 files using the AxWindowsMediaPlayer, and I can't seem to reliably fix the size of the control at runtime.
In design, I've set the Size and MaximumSize properties of the control ("size=120,45") so it snuggles up to a PictureBox, and at run time it "misbehaves" when a modal form is displayed for the second time - the player expands in size to show the visualisation pane, which I'm attempting to hide, by setting the control's Height (and Maximum Height) property so only the control bar portion of the player is visible.
I've replicated this behavior with a little code below. Form1 has a button which shows Form2 modally. Form2 has a Picturebox and the AxWindowsMediaPlayer. Clicking the PictureBox plays the "test.mp3" file from the Application.StartupPath. All is well...the first time the audio is played, the player behaves nicely...no visible visualisation!
Closing Form2, clicking the "Load Form2" button on Form1, and then clicking the PictureBox on Form2 again results in the undesired behaviour... the player expands and shows a waveform pattern in the visualisation pane. Showing Form2 NON-modally ( by using Show in place of ShowDialog) does NOT exhibit this behaviour, presumably because the control is displayed using the initial Size set in design. However, I would like to display Form2 modally.
I would be very grateful on suggestions on how I might overcome this unexpected size change. I've researched a little of the extensive documentation of the player, but I've been unable to hide that visualisation pane. Thank you.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim AudioFile As String
AudioFile = (Application.StartupPath & "\test.mp3")
AxWindowsMediaPlayer1.Visible = True
AxWindowsMediaPlayer1.URL = AudioFile
End Sub
End Class ```
Well, it's a workaround, really: I uses "Show()" in place of "ShowDialog()", and passed data to Form2 (as it initializes) to retain the state of several picture boxes.
Job done, but I'm wary of the media player.

Darken a .Net Form

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

vb.net How to get working scrollbars when moving nested form controls inside a parent panel

I have a container (a panel) which can contain multiple Form controls.
(Form.TopLevel = False)
When the user moves the Forms around I would like to display scrollbars when a form is out of the panel bounds.
When I register the Form.Move event, I can set the AutoScrollPosition. This works unless the user uses the scrollbars.
The problem is that the form.move event is also fired when the scrollbars are used. The result is that the scrollbars don't work. (And I have currently no idea how to find out whether the form has been moved by the mouse or by the scrollbar)
So the question is: How can I make the scrollbars of the panel appear/work when a form (or multiple) forms of the panel exceed the boundaries? I think there must be a simpler way than to handle the move event..
Note:
The panel is placed inside a Infragistics DockableControlPane. (Managed by an UltraDockManager)
(So there are multiple panels which contain at least one form per panel)
The reason is that the "panels" should appear as tabs, can be moved around using the DockManager and display their "sub" forms (Which also can be moved around on their panel).
Any idea would be great
It looks like the LocationChanged event could be used. Example with only one form:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Dim f As New Form
f.TopLevel = False
AddHandler f.LocationChanged, AddressOf Form_LocationChanged
Panel1.Controls.Add(f)
f.Show()
Call Form_LocationChanged(f, EventArgs.Empty)
End Sub
Private Sub Form_LocationChanged(sender As Object, e As EventArgs)
With DirectCast(sender, Form)
Panel1.AutoScrollMinSize = New Size(.Bounds.Right, .Bounds.Bottom)
End With
End Sub
Using an MDI form seems to be more appropriate though for something like this.

Scrolling issues in a form with vb.net

I have a form, in that form has a split container.
In one of the panels there is a picturebox with an image. I am increasing then decreasing the size of the picturebox (aka zooming the image) using the mouse wheel. When the picturebox becomes bigger than the panels visible area it creates the V and H scroll bars, which is what I want.
The problem that I am running into is that the scroll bar is scrolling at the same time that my image is zooming, also sometimes the panels scrollbar is stealing my mousewheel events completely away from the picturebox and stops zooming the image.
Any suggestions on how I can grab the mouse wheel events and consume them completely without the scroll bar using them?
EDIT: On second thoughts I think this is a focus issue. Set the focus to the PictureBox when it is entered.
Imports System.IO
Imports System.Xml
Public Class Form1
Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Focus()
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
PictureBox1.Focus()
End Sub
End Class

Button not hiding when mouse leaves from bottom

I have a form where I want buttons at the very bottom edge of the form, with no gap to the border. These buttons shall be "auto-hide", so they only show when the mouse is in for example the lower 20 pixels of the form. So I use the MouseMove event to trigger this, like code below. However, if mouse leaves the form across the bottom edge, where the buttons are, then the buttons will obviously remain. But I want them to hide. So I need for this purpose to hide the buttons by some other event. Hence I try to hide them in the form's MouseLeave event. But this makes the buttons unclickable and in an erratic state, flashing on and off when the mouse goes over the button.. Why is this? And how can I avoid this problem to get such autohide feature?
Private Sub ZgScale_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Y > Me.ClientSize.Height - 30 Then
Button1.Visible = True
Else
Button1.Visible = False
End If
End Sub
Private Sub ZgScale_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Button1.Visible = False
End Sub
The MouseLeave event fires when the mouse is no longer directly on that control (or form).
If the mouse moves on to a child control, the event will fire.
You need to check whether the mouse is no longer on the form, like this:
If Not Me.ClientRectangle.Contains(Me.PointToClient(e.Location)) Then
Button1.Visible = False
End If
EDIT: Fixed
Windows has direct support built-in for this scenario. Also exposed in Windows Forms and WPF. Once you get the MouseMove event, set the Capture property on the control to True. That forces all mouse messages to be directed to the control, even if the mouse moves outside of the control window.
Once you see it moving outside of the control bounds, set Capture back to false and hide your control. Beware that capture is turned off when the user clicks the mouse so you'll probably have to turn it back on afterwards. Although it should be automatic, you'll get another MouseMove event. Could fail if the user moves the mouse really fast.