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

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?

Related

Open different forms on different button click in VB.NET

A noob query I have, is there any way to use a single command to open different forms on different button click events. I have 24 buttons in one form and will use these buttons to open 24 different forms.
So instead of doing it for 24 times as:
Private Sub BtnCh1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh1.Click
FormCh1.Show()
End Sub
Private Sub BtnCh2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh2.Click
FormCh2.Show()
End Sub
Private Sub BtnCh3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh3.Click
FormCh3.Show()
End Sub
Private Sub BtnCh4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh4.Click
FormCh4.Show()
End Sub
Can it be done with a single command?
In your form's load event add the forms in a List(Of Form)
Private list As List(Of Form)
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
list = New List(Of Form)
list.Add(New Form1())
'
'
'
list.Add(New Form24())
End Sub
Set your button's Tag property with the form's index and set them all to use the same click event:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
list(CType(sender, Button).Tag).Show()
End Sub
Attach all the handlers to your method and then branch behaviour based on the Select Case:
Private Sub Button_Click_Handler(sender As Object, e As EventArgs) Handles Button66.Click, Button67.Click, Button68.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Button66.Name
Dim f1 As New Form1
f1.Show()
Case Button67.Name
Dim f2 As New Form2
f2.Show()
Case Button68.Name
Dim f3 As New Form3
f3.Show()
End Select
End Sub

Moving through pictures with picturebox and next previous buttons

I'm beginner level learner in using VB and currently I am trying create two buttons that will allow me to slide through pictures in an online folder. I need some kind of a way to change the image number in the URL so that the buttons will allow me to move through the pictures.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=1&key=xxxxx")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=2&key=xxxxxxx")
Try this, notice the private variable.
Private _CurrentImage as Int32 = 1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load(string.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage ))
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.loadstring.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage )))
End Sub
I am assuming Button4 is PREVIOUS and Button5 is NEXT. You will want to do some work to prevent numbers out of range but you can see how easy it is to Increment/Decrement a number and then stuff it in a string.

Create list box in runtime and change its color via menu in runtime

I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...

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

Removing item from listbox

I have a form that has 9 texbox that when I click on a certain button it adds whatever is in it to a listbox, I also have a remove button that removes an item from listbox, is there a way I can remove the item and clear the textbox it came from?
Remove item from ListBox:
ListBox1.Items.Remove(sItemtext)
ListBox1.Items.RemoveAt(indexItem)
Clear the TextBox:
TextBox1.Text = ""
Try this ..
Public Class Form1
Dim oLB As TextBox
Dim aList As New List(Of TextBox)
Sub GetLB(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
oLB = CType(sender, TextBox)
End Sub
Private Sub btnMoveToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveToList.Click
ListBox1.Items.Add(oLB.Text)
aList.Add(oLB)
End Sub
Private Sub btnRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveItem.Click
Dim n As Integer = ListBox1.SelectedIndex
aList(n).Text = ""
ListBox1.Items.RemoveAt(n)
aList.RemoveAt(n)
End Sub
End Class