How Can I Create a Mouseover Tooltip on an Image in VB.NET? - 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

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

Send data to another xaml in Windows Phone 8.1 - VB.NET

How can I get a variable in one xaml(when the button is clicked) to appear in another xaml in a textbox.
Basically once the button is clicked in Menu.xaml the name variable is displayed in a text box in Basket.xaml. How can I go about doing that?
Button in Menu.xaml
Private Sub VanButton_Click(sender As Object, e As RoutedEventArgs) Handles VanButton.Click
Dim Name As String = "Vanilla"
End Sub
Variable Name into Basket.xaml textbox once button is clicked in Menu.xaml
Private Sub IcecreamChosen_TextChanged(sender As Object, e As TextChangedEventArgs) Handles IcecreamChosen.TextChanged
End Sub
The app is developed in VB.NET , Visual Studios 2015. I am new to this and have no idea on how to make it work this way.
You have to give a name to your textbox and the in the button click event handle you can write yourtextboxname.Text = variabile
Anyway I suggest you to start using databinding approach. Xaml is databinding based! :)

Tooltip showing twice

I'm showing a ToolTip on a certain button's MouseHover event. If I go over it once it works but if I leave, wait for the tooltip to disapear and come back on the button, it appears twice. I tried cancelling it on MouseLeave but it still appear twice the seocnd time.
Private Sub someButton_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles someButton.MouseHover
Dim tooltipSearch As New ToolTip()
tooltipSearch.Show("I'm a tooltip"), someButton)
End Sub
Private Sub someButton_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles someButton.MouseLeave
Dim toolTip As New ToolTip()
toolTip.GetToolTip(someButton)
toolTip.Hide(someButton)
toolTip.Dispose()
End Sub
Am I missing something obvious?
You are using the ToolTip not in the way it should be used (also look at the documentation :) ). It is just like any other control, start by dragging it onto your form (like you do with other controls).
If you have no dynamic text to be displayed in your tooltip, you can easily set the text in your constructor, using the method SetToolTip. If you do want some dynamic text, you can use this method in your MouseHover event.
Apart from that, you shouldn't do anything anything else. Just set the right delays on your tooltip and it should work all fine.
Private Sub someButton_MouseHover(sender As Object, e As System.EventArgs) _
Handles someButton.MouseHover
ToolTip1.SetToolTip(someButton, "My name is Steve chamba from south Africa")
End Sub
The answer about dragging the ToolTip control to the form is great. There is a subtlety with Button controls though. They seem to automatically set the tool tip to the text of the button, so if you also call ToolTip1.SetToolTip(myButton, "Button Text") then dot-net draws two copies of the tool tip text when you hover over the button.

Setting Focus on a Tab

I have a tab in a windows form called Wafer Map that has three sub-tabs. The First sub-tab is the called Map and has a Load and Skip button. I am trying to set the focus on the Wafer sub-tab on the Load button click. This is the following code I have tried to use.
Private Sub Load_Wafer_Layout_Map_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Load_Wafer_Layout_Map.Click
Wafer_Info.Enabled = True
Wafer_Info.Show()
End Sub
The Wafer_Info.Enabled = True is used to enabled all of the controls on the Wafer tab and works properly when the button is clicked. I have tried using .Focus() and .Show() to bring focus to the next tab but I am not have any luck getting to switch. Anyone have any suggestions?
Just set it:
tabControl.SelectedTab = yourTab
On the Tab Controls Tab Pages, just ensure you name the tab you are attempting to reference. Additionally, see MSDN TabControl.SelectedTab
The code that worked for me is Tab_WaferMap.SelectTab(1). Tab_WaferMap is my main tab and the 1 is the index of the sub tab I wanted to show
I came across this thread as i was looking for a solution to my own focus issue. I have a TabControl with many TabPages. Each TabPage is set to auto scroll due to overflowing content. The problem I ran into was the mouse scroll wheel would not function if the TabPage did not have focus. Since there is not an event for each tab click it made setting focus to each TabPage a challenge. It was not hard, but a challenge none the less. So, here is my code (assuming auto scroll true).
On form load sets focus to main TabPage:
Private Sub frmParent_Load(sender As Object, e As System.EventArgs) Handles Me.Load
TabControl1.TabPages(0).Focus()
End Sub
Sets focus to current TabPage by getting the index then setting focus.
This is triggered by TabControl1.SelectedIndexChange event.
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim intTabIndex As Integer = TabControl1.SelectedIndex
TabControl1.TabPages(intTabIndex).Focus()
End Sub
I hope someone find this useful. It was very useful for me.
Joshua
You can also set the Selected Index of the tab (and sub-tab) using a (zero based) numeric value:
TabParent.SelectedIndex = 3
TabSub.SelectedIndex=2

Right click: menu options

there is a feature I'd like to implement in my application:
The user right clicks on my picturebox object. Good.
When that happens, some code of mine executes and will generate a list of options.
Then a menu appears where the mouse right-clicked, made up of these options.
When the user clicks on one of these options, the menu is deleted and some code is run given the option index as parameter.
My two problems:
How can I tell when the user right clicks? I can see an event handler for "click", but that includes left clicks....
How do I create one of these menus? I mean, go ahead and right click something. That's the kind of menu I'm looking for.
You need to implement the picturebox' MouseUp event. Check if the right button was clicked, then create a ContextMenuStrip with the menu items you want. You can use, say, the Tag property of the items you add to help identify them so you can give them a common Click event handler. Like this:
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
Dim item1 = cms.Items.Add("foo")
item1.Tag = 1
AddHandler item1.Click, AddressOf menuChoice
Dim item2 = cms.Items.Add("bar")
item2.Tag = 2
AddHandler item2.Click, AddressOf menuChoice
'-- etc
'..
cms.Show(PictureBox1, e.Location)
End Sub
Private Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
'-- etc
End Sub
To your first question: you actually handle just the "click" event, there is no separate event for right-click. But look at the EventArgs object you get passed for the event: it includes information of which button was pressed (and would give you more info if a mouse click had anything beyond that). So you check the button within an if block, and you're good to go.
To your second question: http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx. If your menu is pre-defined, just look for that component on the Designer and prepare the menu from there and call its Show() method from the click handler. If you need to decide the menu entries on the fly, the linked documentation page actually includes an example on that ;)
PS: oops, I just noticed Jon's comment on the question. The answer I gave you is for Windows Forms. If you are on WPF let us know and I'll update with the details (although the concepts ain't too different).
There is actually an easier way to do this. Double-click on the control you wish to be able to right click. Now go to the top of the page and it should say in comboboxes; 'Control' and 'Click' Click on the 'click' combobox and look for: Right-Click. Use a ContextMenuStrip for your right click menu.
Now you can choose which function you want.
Private Sub PictureBox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.RightClick
ContextMenuStrip1.Show()
MsgBox("Content Activated.", MsgBoxStyle.Information, "Success!")
End Sub
Hope I could help. :)
Coridex73