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
Related
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)
Sub Tabs()
Dim shtWork1 As Short
Dim dbpath As String = "db\... "
Dim conn As New OleDbConnection(dbpath ... )
For shtWork1 = 1 To shtMaxTabs
Dim TabPageAll As New TabPage
Try
'define DataGridViews
Dim DataGridView1 As New DataGridView
DataGridView1.Parent = TabPageAll
.
.
.
DataGridView1.AutoGenerateColumns = True
TabControl1.TabPages.Add(TabPageAll)
Catch ex as ...
Message
Finally
Close()
End Try
Next shtWork1
End Sub
' The Grids are created on the TabPages and work fine.
But if I click on a cell it should give me the possibility to execute some other code i.e.
filling textboxes on that specific TabPage.
Any idea would be apreciated.
TIA Frank
Just add an event handler when adding your DataGridView
AddHandler DataGridView1.CellClick, AddressOf cellClickHandler
This is an example handler code
Private Sub cellClickHandler(sender As Object, e As DataGridViewCellEventArgs)
MessageBox.Show(String.Format("Row '{0}', Col '{1}'", e.RowIndex, e.ColumnIndex))
End Sub
Edit:
Complete solution
Add a tab control to a new winforms app. Just add this code to Form1 class.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tabPageAll As New TabPage()
Dim dataGridView1 As New DataGridView()
Dim d As New Dictionary(Of String, String)() _
From {{"A", "1"}, {"B", "2"}, {"C", "3"}}
dataGridView1.AutoGenerateColumns = True
dataGridView1.DataSource = _
(From ds In d
Select New With {.Key = ds.Key, .Value = ds.Value}).ToArray()
AddHandler dataGridView1.CellClick, AddressOf cellClickHandler
tabPageAll.Controls.Add(dataGridView1)
TabControl1.TabPages.Add(tabPageAll)
TabControl1.SelectedTab = tabPageAll
End Sub
Private Sub cellClickHandler(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs)
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
' DataGridView.Rows() works just fine here
Dim selectedRow = CType(sender, DataGridView).Rows(e.RowIndex)
End If
End Sub
I have a drop down list with a bunch of product names in it with an add button on the side. Everytime I press the add button, I want a Linkbutton to appear with the name of the product. I did the pnl.Controls.Add(New LinkButton), but it only adds one and then goes away next time I go to click. Here is the code I have right now that is making a LinkButton appear at click, just can't figure out how to alter the logic to add a new one each time.
Private Sub btnAddLinkedProjects_Click(sender As Object, e As EventArgs) Handles btnAddLinkedProjects.Click
lbLinkedProject.Visible = True
lbLinkedProject.Text = ddlParentProject.SelectedItem.Text
End Sub
I have a linkbutton in my designer that is set to invisible until it's clicked. Ultimately, when the link button of the project is clicked on, it fills in all of the fields like this
Private Sub lbLinkedProject_Click(sender As Object, e As EventArgs) Handles lbLinkedProject.Click
Dim intParentRecID As Integer
Dim pid As Project = Nothing
Dim intCityState As Integer = 0
Dim strState As String = ""
Dim cs As nsCityState = Nothing
intParentRecID = Integer.Parse(ddlParentProject.SelectedValue)
pid = oDesignCon.getProjectByRecID(intParentRecID)
If pid Is Nothing Then
Else
intCityState = pid.CityState
cs = New nsCityState(intCityState)
If cs Is Nothing Then
Else
strState = cs.StateShort
Me.ddlAddState.SelectedValue = strState
Call HandleAddStateChanged()
End If
Call nsLinqFormBinder.LoadContainer(Me.pnlCreateNewPID, pid)
Me.ddlAddAssignTo.SelectedIndex = 0
End If
End Sub
What's wrong if you just create new LinkButton as usual :
Private Sub btnAddLinkedProjects_Click(sender As Object, e As EventArgs) Handles btnAddLinkedProjects.Click
'create & prepare new LinkButton'
Dim newLinkedProject As New LinkButton
newLinkedProject.Visible = True
newLinkedProject.Text = ddlParentProject.SelectedItem.Text
'register event handler'
AddHandler newLinkedProject.Click, AddressOf Me.lbLinkedProject_Click
'add the LinkButton to panel'
pnl.Controls.Add(newLinkedProject)
End Sub
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 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.