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

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.

Related

How to dispose of a form within a panel upon closing the main form or upon another button click event?

Disclaimer: I only have beginner to slightly intermediate experience with VB.net
Hello, I was tinkering around with some design ideas for a project and I ran into a problem that I haven't found a solution to. I have a win form with some buttons and a panel. When the user presses a button, a border-less form is loaded into the panel. The problem is this: when the main form is closed, Visual Studio does not stop debugging, presumably because the forms in the panel are not disposed of.
Win Form image
The instance of the panel form is declared in the button click event. How can I destroy that instance from another sub? If I click another button, the first panel form doesn't go away. Is there a better way to accomplish this? I'm still learning, so I'm often not aware of all the different ways to solve a problem. Thanks everyone!
Public Class frm_Clients
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim search As New Search
search.TopLevel = False
search.Dock = DockStyle.Fill
Panel1.Controls.Add(search)
search.Show()
End Sub
Private Sub frm_Clients_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
' What should I write here?
End Sub
End Class
Here's a snippet of what to do to close the windows when another button is pressed.
For Each form In Panel1.Controls.OfType(Of Form).ToList()
form.Close()
Next
Then I would suggest that you set search.Owner to the Form holding the Panel. That means that when the Owner is closed, so are the children.
search.Owner = Me

Setting a windows form's screen position based on another form's current position

I am creating an application with multiple windows forms. The main form is movable, and I want a confirmation window to flash based on where the main form is located.
For example, the main form opens, user drags it 200 points to the left. How do I make sure the confirmation window, upon button-press, opens up exactly to the left of that window?
The built-in properties (center screen, center parent, etc.) don't provide this functionality.
I'm aware of these functions:
Form1.Left += 200
and
Dim frmAccounts as new Form()
Set FrmAccounts.DesktopLocation = new Point(100,100)
but neither of these take into consideration user dragging.
Any ideas?
Thanks for your help.
To keep the buddy glued to the main form, you have to use the main form's LocationChanged event to know when to move it. And you have to position it before it is displayed, that's a bit tricky due the form possibly getting rescaled on a machine with a different DPI setting. Best time to do it is when the buddy's Load event fires, it is rescaled by then. Some sample code:
Public Class Form1
Dim buddy As Form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If buddy Is Nothing Then
buddy = New Form2
AddHandler buddy.Load, AddressOf MoveBuddy
AddHandler Me.LocationChanged, AddressOf MoveBuddy
AddHandler buddy.FormClosed, Sub() buddy = Nothing
buddy.Show(Me)
End If
End Sub
Private Sub MoveBuddy(sender As Object, e As EventArgs)
buddy.Bounds = New Rectangle(Me.Left - buddy.Width, Me.Top, buddy.Width, buddy.Height)
End Sub
End Class

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

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

How Can I Create a Mouseover Tooltip on an Image in VB.NET?

Can I create a tooltip that will show up when a user moves his/her cursor over an image? I can't find such a property in Visual Studio, and I've scoured Google to no avail. I'm using an image in a PictureBox.
Here's to anyone out there on StackOverflow instead of some awesome Halloween party! Yay!
yea, for some reason the Picturebox doesnt have one.
imports System.Drawing
dim tt as new ToolTip()
tt.SetToolTip(picPicture, "This is a picture")
and dont worry, the weekend has only started, plenty of time to party.
Typically I create the interface then throw a ToolTip object from the Toolbox on to the form.
This then gives each object the "ToolTip" property (towards the bottom of the list) which can then be configured to your delight.
Drag a ToolTip control from the toolbox on the left onto your form (the designer will then put it below your form, since it's not meant to be visible normally). By default it will be named "tooltip1".
Then select your checkbox and go over to its properties window. You should see a property labeled "Tooltip on tooltip1" - set this to whatever you want. When you run the app and hold the mouse over your checkbox, you should see the tooltip text.
Assuming that you have added a picture box member with the WithEvents modifier you can use the following
Private tt As ToolTip = New ToolTip()
Sub OnPictureMouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.MouseHover
tt.Show("the message", Me)
End Sub
Sub OnPictureMouseLeave(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.MouseLeave
tt.Hide()
End Sub