Scrolling issues in a form with vb.net - 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

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.

Move Control Beyond Screen Resolution 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.

Best way to use transparent controls as invisible triggers

Once I develop a vb6 code to use transparent controls (Dont remember if I use Buttons or PictrureBoxes) with coordinates as invisible tags & invisible labels to show the names of eachone at groupal photos like facebook does. Now Im trying to recreate the same code at vb.net but I can't reach to get it work..
If I use Buttons with transparent .backcolor, no-text and no-borders, flat style, etc. to mark the photo area, they become opaque when I move the mouse over the control. if I disable becomes invisible for the mouse-over function.
If I use empty PictureBoxes instead for the same purpouse, as are empty they became invisible at runtime also for the "mouse over" function...
I dont know wich empty or invisible control must use to this finality. any suggestion?
Here is an example of what I was talking about in my comments:
Public Class Form1
Private ReadOnly actionsByRectangle As New Dictionary(Of Rectangle, Action)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'If the user clicks near the top, left corner, display a message.
actionsByRectangle.Add(New Rectangle(10, 10, 100, 100),
Sub() MessageBox.Show("Hello World"))
'If the user clicks near the bottom, right corner, minimise the form.
actionsByRectangle.Add(New Rectangle(ClientSize.Width - 110,
ClientSize.Height - 110,
100,
100),
Sub() WindowState = FormWindowState.Minimized)
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
For Each rectangle As Rectangle In actionsByRectangle.Keys
If rectangle.Contains(e.Location) Then
'We have found a rectangle containing the point that was clicked so execute the corresponding action.
actionsByRectangle(rectangle).Invoke()
'Don't look for any more matches.
Exit For
End If
Next
End Sub
'Uncomment the code below to see the click targets drawn on the form.
'Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' For Each rectangle As Rectangle In actionsByRectangle.Keys
' e.Graphics.DrawRectangle(Pens.Black, rectangle)
' Next
'End Sub
End Class
Note that I have added code there that can draw the boxes on the form if you want to see them, but those are just representations of the areas, not the Rectangle values themselves.

Scroll through a user control while timer is running

Whenever I click the area of the user control, it highlights the combobox, making it so that if I attempt to scroll to the bottom of the screen, it only scrolls through the combobox.
I tried putting somelabel.focus() in the click event for the user control. This kind of works. However, this does not allow me to scroll to a part of the control where somelabel is not visible, which seems a little strange to me. I obviously need to be able to scroll all the way to the bottom of the usercontrol.
Additional info: This is a winforms project, and the user control is running on a timer, where the only controls are the combobox and some labels that have their .text property updated every time the timer loops.
UPDATE: I tried disabling the timer, and it appears that after changing the focus to one of the labels, I am now able to scroll all the way to the bottom of the page. So it seems its an issue with the timer.
Here is some code that replicates the problem. If you click on the panel, it gains focus, but it will not let you scroll using the scrollwheel. It will bring the top of the panel back to the top of the screen. If you don't turn on the timer, however, you can freely scroll.
Public i As Integer
Public iString As String
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
iString = i.ToString()
Label1.Text = iString
Label2.Text = iString
Label3.Text = iString
Label4.Text = iString
Label5.Text = iString
i += 1
End Sub
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Panel1_Click1(sender As Object, e As EventArgs) Handles Panel1.Click
Panel1.Focus()
End Sub
You could add a Panel with Dock=Fill and AutoScroll=true in which you insert your controls. Then add the click event for the panel:
private void panel1_Click(object sender, EventArgs e)
{
this.ActiveControl = panel1;
}
This way if you click inside the panel (which fills the whole UserControl), the panel will be active and you can scroll. This won't affect controls inside the panel so you can work with them in the usual way.

How can I place/drop an image evertime you click the mouse button using vb.net?

I looked at "How do I place an image with a mouse-click in Javascript?" but it had a small snippet of Java; immensely larger than my knowledge of Java. And that is the closest I've come to finding an answer in the past week.
Here's what I would like to do (don't know if its even possible):
I have a panel and a toolstrip with 3 buttons. Each button represents a different image. I want to click on a button (once) and then move into the panel and everytime I click the mouse button it drops the image where ever I clicked. This only ends when either I click back on the same button or one of the other buttons. I do not want to drag an image into the panel each time. In other words the button stays depressed and the event/action stays active.
Any help would be greatly appreciated.
Here is an example application. It's just a form with a ToolStrip on it, along with a couple of buttons with an image added to each button. The key property for each button is CheckOnClick=True, which will keep the button pressed down.
There isn't a radio button like feature for ToolStrips, so you have to "uncheck" the other ToolStripButtons yourself, which I have handled in the ItemClicked event.
Public Class Form1
Private _ActiveImage As Image = Nothing
Private Class ImagePoint
Public Location As Point
Public Image As Image
Public Sub New(ByVal image As Image, ByVal location As Point)
Me.Image = image
Me.Location = location
End Sub
End Class
Private _Images As New List(Of ImagePoint)
Public Sub New()
InitializeComponent()
Me.DoubleBuffered = True
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
For Each imageItem As ImagePoint In _Images
e.Graphics.DrawImage(imageItem.Image, imageItem.Location)
Next
End Sub
Private Sub ToolStrip1_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
For Each toolButton As ToolStripButton In ToolStrip1.Items.OfType(Of ToolStripButton)()
If toolButton.CheckOnClick Then
If e.ClickedItem.Equals(toolButton) Then
_ActiveImage = e.ClickedItem.Image
Else
toolButton.Checked = False
End If
End If
Next
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
If _ActiveImage IsNot Nothing AndAlso e.Button = MouseButtons.Left Then
_Images.Add(New ImagePoint(_ActiveImage, e.Location))
Me.Invalidate()
End If
End Sub
End Class
This example just uses a simple class to hold which image was placed at what location and the paint event just loops through the list and paints the image.
If deleting images is in your future, then you would have to call e.Graphics.Clear(Color.White) before painting any images.
For the button UI, check out the alternate style for radio buttons/check boxes. They have a "toggle button" mode which sounds like exactly what you need.
You could go through the motions of detecting mouse down events on the panel, getting the coordinates, creating an image control, and placing a copy of the image in it, but there's a better approach.
Fill the panel with a single image control (fill so that it handles resizes, the image control should always be the same size as the panel). Create a new Bitmap the same size as the image control and associate it with it (set the Image property). Obtain a Graphics object for the Bitmap (Graphics.FromImage() I think). Clear() it with the background color (Color.White?).
Preload your three images on startup and write the code to toggle between them, selecting the "active one" every time a different button is selected. On the mouse down event, you can get the coordinates of the click easily. Use myGraphics.DrawImage(...) to draw the active image at that location onto the Bitmap. You can then save the Bitmap to a file or do whatever you want with it. All of these concepts have lots of examples, Google them.
If you want to interact with the images after you "drop" them (like move them around again or something), then you will need to maintain a data structure that tracks what and where you've dropped. A simple class that has a Point and Image reference will be sufficient. Each drop should add an entry to a List(Of ...) these objects. You'll probably then need to write code such as "which image is under the current mouse location?". This can be accomplished by iterating through the list and doing point/rectangle intersection testing.
Private Sub ToolStripSound_Click(sender As Object, e As EventArgs) Handles ToolStripSound.Click
If ToolStripSound.Checked = False Then
ToolStripSound.Checked = True
Else
ToolStripSound.Checked = False
End If
End Sub
Private Sub ToolStripSound_CheckedChanged(sender As Object, e As EventArgs) Handles ToolStripSound.CheckedChanged
' ToolStripSound.Checked = True
If ToolStripSound.Checked = True Then
Me.ToolStripSound.Image = Global.Traffic_Lights.My.Resources.Resources.Oxygen_Icons_org_Oxygen_Status_audio_volume_high
Else
Me.ToolStripSound.Image = Global.Traffic_Lights.My.Resources.Resources.Oxygen_Icons_org_Oxygen_Status_audio_volume_muted
End If
End Sub