selecting controls from flowlayoutpanel in vb.net2008 - vb.net

i m using flowlayout panel to dynimacally create pictureboxes on the widows form. My actual problem is that, i want to see specific image in picturebox when user clicks on the item in flow layout panel using click event..
I hade tried some code, but everytime i failed..please help.please suggest me my missing code with some details so i can understand it very well.
My code is -------------------------------------------------------
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
' Style Explorer - Thubnail Viewer
' FlowLayoutPanel1.SuspendLayout()
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" + "All files (*.*)|*.*"
OpenFileDialog1.Multiselect = True
OpenFileDialog1.Title = "Select Photos"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
For Each file As String In OpenFileDialog1.FileNames
Dim imageControl As New PictureBox()
imageControl.Name = "pic" & i.ToString()
imageControl.Height = 100
imageControl.Width = 100
Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim myBitmap As New Bitmap(file)
Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
imageControl.Image = myThumbnail
FlowLayoutPanel1.Controls.Add(imageControl)
Next
End If
End Sub
Public Function ThumbnailCallback() As Boolean
Return False
End Function

Inside the loop that add the pictureboxes to the FlowLayoutPanel you could add the name of the file to the Tag property of the dinamically created PictureBox and add an event handler for the click event
For Each file As String In OpenFileDialog1.FileNames
Dim imageControl As New PictureBox()
imageControl.Name = "pic" & i.ToString()
imageControl.Height = 100
imageControl.Width = 100
' Save the file from which you create the thumbnail
imageControl.Tag = file
' Set an event handler for the clickevent on the thumbnail
AddHandler imageControl.Click, AddressOf picClicked
.....
Next
Now write the event handler that responds to the click event
Private Sub picClicked(sender As Object, e As EventArgs)
Dim pic = DirectCast(sender, System.Windows.Forms.Control)
' The name of the file is in the Tag property....
Console.WriteLine(pic.Tag.ToString())
' Now its your job to display the image knowing the filename
End Sub

Related

VB.NET get control name from button created at run time

I have this code to create 3 buttons at run time, which seems to be working ok.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rawData As String
Dim FILE_NAME As String = "C:\temp\test.txt"
Dim data() As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
rawData = objReader.ReadLine() ' & vbNewLine
data = Split(rawData, ",")
'data 0 = X loc, data 1 = Y loc, data 2 = Part Num, data 3 = Reference Des
Dim dynamicButton As New Button
dynamicButton.Location = New Point(data(0), data(1))
dynamicButton.Height = 20
dynamicButton.Width = 20
dynamicButton.FlatStyle = FlatStyle.Flat
dynamicButton.BackColor = Color.Transparent
dynamicButton.ForeColor = Color.FromArgb(10, Color.Transparent)
'dynamicButton.Text = "+"
dynamicButton.Name = data(2)
dynamicButton.FlatAppearance.BorderColor = Color.White
'dynamicButton.Font = New Font("Georgia", 6)
AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
Controls.Add(dynamicButton)
Dim myToolTipText = data(3)
ToolTip1.SetToolTip(dynamicButton, myToolTipText)
Loop
End Sub
I want to get the control name that was created when I click a button, but I cannot seem to get what I need. I have been doing this on the dynamicButton_click event.
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For Each cntrl In Me.Controls
If TypeOf cntrl Is Button Then
MsgBox("")
Exit Sub
End If
Next
End Sub
All you need to do is cast the sender variable to button. It will tell you which control was the source of the event:
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show("You clicked: " & btn.Name)
End Sub
You attached an event handler to the dynamic button and that is hitting?
Couple ways you can do this:
This will give you the button you just clicked without needing to loop, the sender is the button. You need to cast it from the object to button:
MessageBox.Show(DirectCast(sender, Button).Name)
Otherwise, inside your loop, cast cntrl (the generic Control object) to the specific Button control object and then get the name.
Dim btn as Button = DirectCast(cntrl, Button)
MessageBox.Show(btn.Name)

VB 2013 How to Convert Working Code to Dynamic

Hello I have a working code for a project i am making...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderDlg = FolderBrowserDialog1
'Checks if there is a value to the Selected folder
If FolderDlg.SelectedPath = "" Then
'If no value asks to set one
MsgBox("Please Set a Directory")
FolderDlg.ShowDialog()
If DialogResult.OK Then
Button1.Text = Path.GetFileName(FolderDlg.SelectedPath)
End If
Exit Sub
End If
Process.Start("explorer.exe", FolderDlg.SelectedPath)
End Sub
I want to be able to have "AddNew" Button that creates a a copy of this code. I know how to create dynamic buttons but they all call the same FolderBrowserDialog..
EDIT This is My Add New Button That Creates Buttons With Tags And Also Creates FolderBrowserDialogs with Tags.
Private Sub AddNew_Click(sender As Object, e As EventArgs) Handles AddNew.Click
Dim count As Integer = FloLay.Controls.OfType(Of Button)().ToList().Count
Dim button As New Button()
count = FloLay.Controls.OfType(Of Button)().ToList().Count
'button.Location = New System.Drawing.Point(150, 25 * count)
'button.Size = New System.Drawing.Size(60, 20)
button.Name = "button_" & (count + 1)
button.Text = "Button_ " & (count + 1)
button.Tag = "Button_" & (count + 1)
AddHandler button.Click, AddressOf Set_Dir
Dim FolderDlg As New FolderBrowserDialog
count = FloLay.Controls.OfType(Of FolderBrowserDialog)().ToList().Count
FolderDlg.Tag = "FolderDlg_" & (count + 1)
FloLay.Controls.Add(button)
End Sub
EDIT 3 This is My NEW NEW Event Handler
Private Sub Set_Dir(sender As Object, e As EventArgs)
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
End Using
End If
Dim path = CStr(btn.Tag)
' MsgBox(path)
Process.Start("explorer.exe", path)
End Sub
Now only to Add the Button.Name = GetFileNAme and persistance after app restart or reboot
EDIT 4
Private Sub Set_Dir(sender As Object, e As EventArgs)
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
btn.Text = path.GetFileName(fbd.SelectedPath)
End Using
Exit Sub
End If
Dim Folderpath = CStr(btn.Tag)
' MsgBox(Folderpath)
Process.Start("explorer.exe", Folderpath)
End Sub
Now It Works like a Charm!
You can't create a copy of the code. What you need to do is have the code in the event handler determine what to do based on the Button that was clicked. When you create a Button, you can store some data in its Tag property. In the event handler, the sender parameter is a reference to the object that raised the event, i.e. the Button that was clicked. You can then get that data back from its Tag property and use it. You would use that data to configure the FolderBrowserDialog in whatever manner is appropriate for that Button. E.g.
Creating the Button:
Dim btn As New Button
AddHandler btn.Click, AddressOf ButtonClicked
Me.Controls.Add(btn)
Inside the event handler:
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
End Using
End If
Dim path = CStr(btn.Tag)
'Use path here.
Note that the Tag property is type Object so it can be anything you want. It can be as simple as an Integer or as complex as a DataSet. You simply cast it as the appropriate type in the event handler and then access all the data as normal.

Is there a way in VB.NET to have buttons and menu bars that are built in the code show up in the design view?

Is there a way in VB.NET to make components like buttons and menus bars show in design view of you added them in programmatically?
I now in Java i you add a button in the code it will show in design view if you switch back and forth. Can this be done in VB.NET.
Code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'defining the main menu bar
Dim mnuBar As New MainMenu()
'defining the menu items for the main menu bar
Dim myMenuItemFile As New MenuItem("&File")
Dim myMenuItemEdit As New MenuItem("&Edit")
Dim myMenuItemView As New MenuItem("&View")
Dim myMenuItemProject As New MenuItem("&Project")
'adding the menu items to the main menu bar
mnuBar.MenuItems.Add(myMenuItemFile)
mnuBar.MenuItems.Add(myMenuItemEdit)
mnuBar.MenuItems.Add(myMenuItemView)
mnuBar.MenuItems.Add(myMenuItemProject)
' defining some sub menus
Dim myMenuItemNew As New MenuItem("&New")
Dim myMenuItemOpen As New MenuItem("&Open")
Dim myMenuItemSave As New MenuItem("&Save")
'add sub menus to the File menu
myMenuItemFile.MenuItems.Add(myMenuItemNew)
myMenuItemFile.MenuItems.Add(myMenuItemOpen)
myMenuItemFile.MenuItems.Add(myMenuItemSave)
'add the main menu to the form
Me.Menu = mnuBar
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
'create a new TreeView
Dim TreeView1 As TreeView
TreeView1 = New TreeView()
TreeView1.Location = New Point(5, 30)
TreeView1.Size = New Size(150, 150)
Me.Controls.Add(TreeView1)
TreeView1.Nodes.Clear()
'Creating the root node
Dim root = New TreeNode("Application")
TreeView1.Nodes.Add(root)
TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 1"))
'Creating child nodes under the first child
For loopindex As Integer = 1 To 4
TreeView1.Nodes(0).Nodes(0).Nodes.Add(New _
TreeNode("Sub Project" & Str(loopindex)))
Next loopindex
' creating child nodes under the root
TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 6"))
'creating child nodes under the created child node
For loopindex As Integer = 1 To 3
TreeView1.Nodes(0).Nodes(1).Nodes.Add(New _
TreeNode("Project File" & Str(loopindex)))
Next loopindex
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
End Sub
Private Sub openInWeb()
Try
Dim url As String = "http://www.stackoverflow.com"
Process.Start(url)
Catch ex As Exception
MsgBox("There's something wrong!")
Finally
End Try
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\Users\itpr13266\Desktop\Asnreiu3.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
openInWeb()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim myStream As Stream = Nothing
Dim openFileBox As New OpenFileDialog()
openFileBox.InitialDirectory = "c:\"
openFileBox.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileBox.FilterIndex = 2
openFileBox.RestoreDirectory = True
If openFileBox.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileBox.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
'**************************
' your code will go here *
'**************************
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
End Sub
Private Sub ToolTip1_Popup(sender As System.Object, e As System.Windows.Forms.PopupEventArgs) Handles ToolTip1.Popup
End Sub
Private Sub Button(p1 As Object)
Throw New NotImplementedException
End Sub
Private Function myButton() As Windows.Forms.Button
Throw New NotImplementedException
End Function
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
AboutBox1.Show()
End Sub
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Dim lbl As New Label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size (if required)
lbl.Location = New System.Drawing.Point(12, 190) 'set your location
lbl.Text = "You just clicked button 5" 'set the text for your label
Me.Controls.Add(lbl) 'add your new control to your forms control collection
End Sub
Public Sub HellowWorld()
MsgBox("Hello World!")
End Sub
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
HellowWorld()
End Sub
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim ProgressBar1 As ProgressBar
Dim ProgressBar2 As ProgressBar
ProgressBar1 = New ProgressBar()
ProgressBar2 = New ProgressBar()
'set position
ProgressBar1.Location = New Point(10, 200)
ProgressBar2.Location = New Point(10, 250)
'set values
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 200
ProgressBar1.Value = 130
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 100
ProgressBar2.Value = 40
'add the progress bar to the form
Me.Controls.Add(ProgressBar1)
Me.Controls.Add(ProgressBar2)
' Set the caption bar text of the form.
End Sub
End Class
The Designer does only show Controls that are created in the FormName.Designer.vb file. It does not run code in the FormName.vb file. When adding controls in the Designer, they are added to the InitializeComponent() method in the FormName.Designer.vb file.
So the only way to show the controls in the Designer is to add them in the Designer or to edit the FormName.Designer.vb file manually. If you decide for the latter, you should mimic the code that is generated by the Designer very closely in order to avoid problems when the Designer is shown. Also, be prepared that the Designer regards the FormName.Designer.vb file as completely generated code and might decide to recreate the file sooner or later omitting parts that it cannot handle.
Side note: in order to see the FormName.Designer.vb file, you should select "Show All Files" for the project in Solution Explorer.

Identify source chart/control from ContextMenuStrip Click

I have a form with numerous charts and have added a ContextMenuStrip when a chart is right clicked so the user can copy the chart image to the clipboard
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Then I would like to identify which chart was clicked on so that I can copy that chart to the clipboard.
This seems like a simple problem to me but I cannot figure out why no matter what I do trying to identify the Owner,Parent,SourceControl of the right click menu always returns me a Nothing value.
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(menuItem.Owner, ContextMenuStrip)
Dim _owner As Control = CType(cms.SourceControl, Chart)
Select Case menuItem.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
_owner.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End Sub
Any ideas how to identify the source chart?
Have a messy solution. Going to leave this open in case a more elegant solution can be formed.
Add a name to your new ContextMenuStrip that reflects the Sender. This can then be used in the MouseClick method to find the origin
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
'Add the name to menu
cmus.Name = sender.Name & "_CMS"
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Now find the control that corresponds to the new name
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cms As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim _owner As ContextMenuStrip = CType(cms.Owner, ContextMenuStrip)
'This is where you use the name attached to the ContextMenuStrip
Dim _chartname As String = Replace(_owner.Name, "_CMS", "")
Dim parentObject As Chart = Nothing
Try
parentObject = CType(Me.Controls.Find(_chartname, True)(0), Chart)
Catch ex As Exception
End Try
If Not parentObject Is Nothing Then
Select Case cms.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
parentObject.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End If
End Sub
Still think there should be an easier solution than this using the likes of Parent,Owner and SourceControl
My VB is rusty, but the below code works fine for me.
Dim menu As New ContextMenuStrip()
menu.ItemClicked += New ToolStripItemClickedEventHandler(menu_ItemClicked)
menu.Items.Add("Save As Image")
chart1.ContextMenuStrip = menu
chart2.ContextMenuStrip = menu;
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
If e.ClickedItem.ToString() = "Save As Image" Then
Dim menu As ContextMenuStrip = TryCast(sender, ContextMenuStrip)
If menu IsNot Nothing AndAlso menu.SourceControl IsNot Nothing Then
Dim chart As Chart = TryCast(menu.SourceControl, Chart)
Dim dlg As New SaveFileDialog()
If chart IsNot Nothing AndAlso dlg.ShowDialog() = DialogResult.OK Then
chart.SaveImage(dlg.FileName, ChartImageFormat.Jpeg)
End If
End If
End If
End Sub

How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox on another Form?

How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox Control on another Form?
Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.ShowDialog()
photo = OpenFileDialog1.FileName.ToString
I'm guessing this is wrong but I'm lost to what to do here.
Then once I have selected an Image; what would be the appropriate Code to put that Image into a Picturebox Control on the other Form ?
If I understood you correctly then it is pretty easy:
Sub OpenAnImageInPicturebox(ByRef pb As PictureBox)
Dim ofd As New OpenFileDialog
ofd.Filter = "Bitmap|*.bmp|JPEG|*.jpg" 'If you like file type filters you can add them here
'any other modifications to the dialog
If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
Try
Dim bmp As New Bitmap(ofd.FileName)
If Not IsNothing(pb.Image) Then pb.Image.Dispose() 'Optional if you want to destroy the previously loaded image
pb.Image = bmp
Catch
MsgBox("Not a valid image file.")
End Try
End Sub
try this:
photo = image.Fromfile( OpenFileDialog1.FileName)
Hope it helps
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Title = "Please select a file"
OpenFileDialog1.InitialDirectory = "c:"
OpenFileDialog1.ShowDialog()
PictureBox1.ImageLocation = OpenFileDialog1.FileName.ToString
PictureBox1.Visible = True
End Sub
I came across this question when I was looking for Filter, as an update to previous answers.
You want to use OpenFileDialog to select an Image to put in a PictureBox Control in another Form. I suggest :-
Use Module : to use the Image on any Form
Use Default Image : to display in PictureBox Control whenever Error occurred. Use Project Resources to add Existing Resource (I.E. PNG Image file : noPhotoUsr).
Code for Module1
Module Module1
Public Function _GetImgOFD(Frm As Form, PicBx As PictureBox) As Bitmap
Dim _ErrBitmap As Bitmap = My.Resources.noPhotoUsr
Dim ChosenBitmap As Bitmap
Using OFD As OpenFileDialog = New OpenFileDialog
With OFD
.Filter = ("Image File (*.ico;*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png;*.ico")
.RestoreDirectory = True
.Multiselect = False
.CheckFileExists = True
If .ShowDialog(Frm) = DialogResult.OK Then
ChosenBitmap = Bitmap.FromFile(.FileName)
Else
ChosenBitmap = _ErrBitmap
End If
End With
End Using
Return ChosenBitmap
End Function
End Module
Code to use in any Form, PictureBox Click Event
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Image = Module1._GetImgOFD(Me, PictureBox1)
End Sub