A problem on right-click menu using Vb.Net - vb.net

I want to add a right click menu to a dynamic added button by using ContextMenuStr. My code are as follows:
Public Class Form1
Private mybutton As New Button
Private i As Integer
Private mybutton_cms As ContextMenuStrip = New ContextMenuStrip()
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "addbutton" Then
***Dim mybutton As New Button***
Me.Controls.Add(mybutton)
i = i + 1 'give a ID to the dynamic added button
mybutton.Left = 200
mybutton.Top = 200
mybutton.Name = "mybutton"
AddHandler mybutton.MouseMove, AddressOf mybutton_MouseMove
ElseIf e.Node.Text = "deletebutton" Then
Me.Controls.RemoveByKey("mybutton")
i = i - 1 'delete the the ID of button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
mybutton_cms.Items.Add(tsm)
mybutton.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
OBJ_Resize.ResizeOBJ(sender, e, Me)
Dim file As New IO.StreamWriter("data2.xml", OpenMode.Append)
file.WriteLine("width=" & sender.width)
file.WriteLine("height=" & sender.height)
file.WriteLine("x=" & sender.left)
file.WriteLine("y=" & sender.top)
file.Close()
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
End Classa
there is a bug that if I delete the Code referenced with *,namely :
Dim mybutton As New Button
when I run the code, there is no right click menu on the button dynamiclly added.
And if I add this code,there is only one dynamical added button on the form1. But the rigtht click menu will show.
What I want to do is click “addbutton” once to get a button, and each button can have a right-click menu
What's wrong with my code?
Beside,could you plese tell me How to add left-click events to the right menu item?
Thanks!

Related

How to disable and get name for control in FlowLayoutPanel from ContextMenuStrip in VB.Net

My program contains buttons in a FlowLayoutPanel.
I want to disable any button when right click on it and click "Disable" in the ContextMenuStrip.
My code is:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 30
Dim btn As New Button
btn.Name = i
btn.Text = i
btn.ContextMenuStrip = ContextMenuStrip1
FlowLayoutPanel1.Controls.Add(btn)
Next
End Sub
End Class
declare a public varibale for keeping a control
public ctrl as button = nothing
you can create a right click by putting this code on mouse down...
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
ctrl = sender
Dim item1 = cms.Items.Add("Disable")
item1.Tag = 1
AddHandler item1.Click, AddressOf Disable
end if
and in the diable sub you can code like this...
Private Sub Disable()
ctrl.enabled = false
End Sub

A question about adding a click event to a dynamically added right-click menu

I want to add click events to the dynamically added right-click menu.The mybutton is a dynamical added button. my codes are as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
AddHandler tsm.Click, AddressOf mybutton_changeicon
mybutton_cms.Items.Add(tsm)
sender.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
Module changeIcon
Function changeicon(ByRef Sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs, Optional ByVal Offset As Integer = 10)
As Boolean
Dim openfiledialog1 As New OpenFileDialog
Dim imagepath As String
If openfiledialog1.ShowDialog = DialogResult.OK Then
imagepath = openfiledialog1.FileName
Sender.Image = Image.FromFile(imagepath)
End If
End Function
End Module
when I run it , there is a problem that:
Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs?
what's wrong with my code?

How do I load a form inside another form?

I created a registration form and added a form with features "Update, Delete, Refresh," along with a DataGridView to show data from the registration form.
Here's my form:
Since I inserted a TreeView, when I click the Update button as in the picture, I get the error:
Object Reference Not set to an instance of an object
I think my code in the TreeView form is wrong.
This is what I entered:
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "Update/Delete Student" Then
Dim regstu As New Registered_Students
regstu.MdiParent = Me
regstu.Show()
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "Update/Delete Student" Then
Dim regstu As New Registered_Students
Form_father.IsMdiContainer = True
Form_father.TopLevel = False
regstu.MdiParent = New Form_father
Form_father.Panel1_Container.Controls.Add(regstu)
regstu.Show()
End If
End Sub

Resize UserControls in every instance of TabPage

This has been killing me for a couple of weeks now. I have a browser that I've made and it's using a TabControl. Every time the TabPage "+" is clicked, a new one is added. Each one of these TabPages has userControls (WebBrowser, Address Bar, Back Button, Forward Button, etc..). When the user resizes Form1, I want all userControls to fit to the TabPage/Form1.
Basically, I want my userControls to fit the current size of the Form but I can't figure it out. Right now, I have the TabControls working but that was done by simply using the Anchor property. That option isn't avialable for the UserControls. I manually anchored it programatically but it doesn't resize. It just sticks everything right in the middle with the smaller size....
here's the Code:
'THIS IS THE CODE FOR THE FORM
Imports System.IO
Public Class Form1
'The Global Variables
Dim theControls1 As New theControls
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TabPage1.Controls.Add(theControls1)
theControls1.theBrowser.Navigate("http://google.com")
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
'For Auto Sizing theControls to Form1
theControls1.Width = TabControl1.Width - 8
theControls1.Height = TabControl1.Height - 25
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
'Add new tab with the same controls.
Dim theNewTab As New TabPage
Dim theOtherControls As New theControls
Dim theTabCounter As Integer = TabControl1.TabPages.Count
theOtherControls.AutoSize = True
theOtherControls.Width = TabControl1.Width
theOtherControls.Height = TabControl1.Height
theOtherControls.Anchor = AnchorStyles.Right & AnchorStyles.Left & AnchorStyles.Bottom & AnchorStyles.Top
Dim theSelectedTab As String = TabControl1.SelectedTab.Text
If theSelectedTab = "+" Then
TabControl1.TabPages.Insert(theTabCounter - 1, theNewTab)
theNewTab.Controls.Add(theOtherControls)
theControls1.theBrowser.Navigate("http://google.com")
theOtherControls.theBrowser.Navigate("http://google.com")
TabControl1.SelectTab(theTabCounter - 1)
End If
End Sub
End Class
'THIS IS THE CODE FOR THE USERCONTROLS
Imports System.IO
Imports System.Data.OleDb
Public Class theControls
'The History Database Connection String
Dim theHistoryDatabaseConn As New OleDbConnection
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles theAddressBar.KeyDown
'Navigate to Webpage stated in theAddressBar
If e.KeyValue = Keys.Enter Then
theBrowser.Navigate(theAddressBar.Text)
e.SuppressKeyPress = True
End If
End Sub
Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click
theBrowser.GoForward()
End Sub
Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click
theBrowser.GoBack()
End Sub
Private Sub theBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles theBrowser.DocumentCompleted
'Set Tab Text to current web page and Address Bar
Form1.TabControl1.SelectedTab.Text = theBrowser.Url.Host.ToString
Me.theAddressBar.Text = Me.theBrowser.Url.AbsoluteUri.ToString
'Read the History
theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Marc Wilson\Documents\Visual Studio 2010\Projects\myBrowser\myBrowser\bin\Debug\TheHistoryDB.accdb"
theHistoryDatabaseConn.Open()
'Populate theAddressBar with the contents from the History
theAddressBar.Items.Clear()
Dim readTheHistory As String
Dim getTheHistory As OleDbCommand
readTheHistory = "SELECT [Host Name] FROM TheHistory"
getTheHistory = New OleDbCommand(readTheHistory, theHistoryDatabaseConn)
Dim theData As New OleDbDataAdapter(getTheHistory)
Dim theTable As New DataTable("TheHistory")
'theHistoryDatabaseConn.Open()
theData.Fill(theTable)
For Each row As DataRow In theTable.Rows
theAddressBar.Items.Add(row.Item("Host Name"))
Next
'Writes history to TheHistory Database (No Duplicates!)
If theAddressBar.Items.Contains(theBrowser.Url.Host.ToString) Then
Else
'Write The History
Dim writeTheHistory As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Host Name], [Absolute Path]) VALUES (theBrowser.URL.Host.ToString, theBrowser.URL.AbsoluteUri.ToString)", theHistoryDatabaseConn)
writeTheHistory.Parameters.Add("#Host Name", OleDbType.Char, 255).Value = theBrowser.Url.Host.ToString
writeTheHistory.Parameters.Add("#Absolute Path", OleDbType.Char, 255).Value = theBrowser.Url.AbsoluteUri.ToString
theHistoryDatabaseConn.Close()
theHistoryDatabaseConn.Open()
writeTheHistory.ExecuteNonQuery()
theHistoryDatabaseConn.Close()
End If
theHistoryDatabaseConn.Close()
End Sub
Private Sub theBrowser_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles theBrowser.ProgressChanged
'Status Bar Text
Label1.Text = theBrowser.StatusText.ToString
End Sub
Private Sub theAddressBar_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles theAddressBar.SelectedValueChanged
Me.theBrowser.Navigate(Me.theAddressBar.Text)
End Sub
End Class
Whenever you create an instance of your UserControl, set its Dock() property to Fill:
Dim theOtherControls As New theControls
theOtherControls.Dock = DockStyle.Fill

assigning object to button.tag

I created a Sub I want to run everytime one of two buttons is clicked. I added the handles of both buttons to the sub so that clicking either one will fire the subroutine.
I placed listview object A in buttonA.tag, and listview object B in buttonB.
When the button is clicked I do my best to extract the listview instance tucked into the button's tag. The problem is there is no instance in the tag. It is simply "nothing."
Private Sub Execute(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click, btnB.Click
Dim buttonSender As Button = Nothing
buttonSender = CType(sender, Button)
Dim btnListView As ListView = buttonSender.Tag
End Sub
-------------------Edit-1
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.btnA.Tag = Me.lvA
Me.btnB.Tag = Me.lvB
End Sub
Your time is appreciated.
Try this,
Dim buttonSender As Button = CType(sender, Button)