viewing image in picturebox using listview in vb.net 2008 - vb.net

I am working in vb.net 2008 with listview control. I added some images in listview.items using imagelist. now I want to show that image in picture box when I clicked on button. I had tried some time for this but getting error everytime.
Here is the code :
Public Class Form1
Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
PictureBox1.Image = Bitmap.FromFile(ListView1.Items.Item(1).ToString)
End Sub
End Class

If you want the picture to show when you click a button then I think you want to use Button_Click instead of ListView_Click
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
'Also, might be easier just to do this:
PictureBox1.Image = ImageList1.Images(0)
End Sub
Then you can set different buttons for your different pictures

Related

How do I add items to a listbox inside a user control?

I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.

Control Properties of LinkLabel when used for HelpProvider

I am working with Visual Studio 2017, working in VB. I am linking to a .CHM file from a LinkLabel which works fine with following code:
Private Sub LinkLabel2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkLabel2.Click
' Create link to help file
System.Windows.Forms.Help.ShowHelp(Me, "RPM_Help.chm", HelpNavigator.AssociateIndex)
End Sub
I use a simple image to indicate the link to the .chm file that is 32x32 pixels in size, I have been playing with the properties of LinkLabel2 but I just can't figure out how to make the entire label a link space. I did find that unless there is a Text property on the label a MouseOver cursor will not show up, but how can I make the entire label image a cursor link?
As shown in the code and image below this can be solved by using LinkLabel2.AutoSize = False and adding a ToolTip from Common Controls to the form in design mode.
Each control, such as Buttons and TextBoxes, will acquire a ToolTip on toolTip1 property when you add a toolTip1 to your designer view. You can access this property in the Properties pane to set the tool tips.
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LinkLabel2.BackColor = Color.CornflowerBlue
LinkLabel2.AutoSize = False
LinkLabel2.Width = 168
LinkLabel2.Height = 40
LinkLabel2.Text = ""
End Sub
Private Sub LinkLabel1_Click(sender As Object, e As EventArgs) Handles LinkLabel1.Click
' --- Open help file - Table of contents
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.TableOfContents)
End Sub
Private Sub LinkLabel2_Click(sender As Object, e As EventArgs) Handles LinkLabel2.Click
' --- Open help file - Index
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Index)
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
' --- Open help file - Search
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Find, "")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The code corresponds to the image shown. The values can of course also be set via the properties, but are included here in the FormLoad for documentation.
You may want to use a simple PictureBox1_Click event for your needs as shown below (third item in the "Show help" group box).

VB.NET Create a to a form attached Preview window

I want to create a little window which gets visible when I press a button and is attached to the main Form (like shwon below). I want to use this window to show preview of Images (I gonna have a Listbox and depending on which entry is selected, a picture is shown) How can I do this? And how can I be sure that the window will always be attached to the main Form (not depending on resolution). I tried to do it with a second form, but I can't fix it in the correct position.
regards
Assuming your preview form class is frmPreview and you open it this way:
Private mPreviewForm As frmPreview
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mPreviewForm = New frmPreview
mPreviewForm.Show()
AttachPreviewForm()
End Sub
Then you must reposition it every time the main form is changing size or location:
Private Sub AttachPreviewForm()
If mPreviewForm IsNot Nothing Then
mPreviewForm.AttachForm(Me)
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
AttachPreviewForm()
End Sub
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
AttachPreviewForm()
End Sub
And in the frmPreview:
Public Sub AttachForm(parent As Form)
Location = New Point(parent.Left + parent.Width, parent.Top)
Size = New Size(200, parent.Height)
End Sub

Setting focus to a textbox control

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.
If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?
EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.
Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form
Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Show()
TextBox1.Select()
End Sub
I think what you're looking for is:
textBox1.Select();
in the constructor. (This is in C#. Maybe in VB that would be the same but without the semicolon.)
From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx :
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TextBox1.Select()
End Sub
Using Focus method
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
yourControl.Focus()
End Sub
To set focus,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Focus()
End Sub
Set the TabIndex by
Me.TextBox1.TabIndex = 0
Quite simple :
For the tab control, you need to handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
I think the appropriate event handler to use is "Shown".
And you only need to focus the appropriate text box.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
myTextbox.Focus()
End Sub
create a textbox:
<TextBox Name="tb">
..hello..
</TextBox>
focus() ---> it is used to set input focus to the textbox control
tb.focus()

How to handle multiple click events with same Sub

I'm making a game for my visual basic course. I have multiple picture boxes that when clicked will reveal a hidden image individually. The point of the game is to find the matching pictures (simple enough).
On the easiest level, I have 16 picture boxes. The number of picture boxes increases as the difficulty increases.
For each picture box, I currently have an event handler as follows (default created by visual studio):
Private Sub pictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pictureBox1.Click
Inside, I plan to use this to change the image in the picture box, as follows:
pictureBox1.Image = (My.Resources.picture_name)
I would like to know if there is a way to have one Sub handle ALL the button clicks, and change the appropriate picture box, instead of having 16 separate handlers. For example:
Private Sub pictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles pictureBox1.Click, pictureBox2.Click, pictureBox3.Click, ... pictureBox16.Click
And do the following:
' Change appropriate picture box
Here's what it looks like (for now):
To find out which PictureBox was clicked you just have to look at the sender variable. Obviously you have to convert it from the Object type to the PictureBox type:
Dim ClickedBox As PictureBox
ClickedBox = CType(sender, PictureBox)
Personally what I would do would be to attach your common EventHandler to your PictureBox, give each PictureBox a Tag for an index, unless you want to do your selection on the name. Then you do something like this.
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click, ...
Dim pb As PictureBox = CType(sender, PictureBox)
Select Case CInt(pb.Tag)
Case 0
pb.Image = My.Resources.PictureName1
Case 1
pb.Image = My.Resources.PictureName2
...
End Select
End Sub
According to what I've read, DirectCast is preferred over CType
DirectCast can be combined with 'With/End With' as shown here:
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click, ...
With DirectCast(sender, PictureBox)
Select Case CInt(.Tag)
Case 0
.Image = My.Resources.PictureName1
Case 1
.Image = My.Resources.PictureName2
...
End Select
End With
End Sub
I've tried the following also but this causes strange problems (controls disappearing).
Using cbMe as CheckBox = DirectCast(sender, CheckBox)
cbMe.Checked = True
End Using
Iterate through all controls for example
For Each ctr As Control In Me.Controls
If TypeOf ctr Is PictureBox Then
If ctr Is ActiveControl Then
' Do Something here
End If
End If
Next