problem adding menu item

i have db connection that loads 2 fields, name and directory

then i need to add the 2 records to a menu so i have

file.dropdownitems.add(name, nothing, process.start(directory))

but VB is telling me that system.diagnostics.process cannot be converted to system.eventhandler

what am i doing wrong can someone point me in the right direction?!

thanks in advance!!

[394 byte] By [jwalker343] at [2007-12-24]
# 1

"file.dropdownitems.add(name, nothing, process.start(directory))"

To add two records, use the .addrange method.

Here is an example:

Me.MnuFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() _
{
Me.MnuFileNewDatabase, Me.ToolStripSeparator5, Me.mnuFileOpen, Me.ToolStripSeparator4, _
Me.mnuFileClose, Me.ToolStripSeparator8, Me.mnuFileExplore, Me.ToolStripSeparator6, Me.MnuFileExit})

This example loads and entire menu.

ReneeC at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
You are trying to add the statement you want to execute when the item is clicked. Instead, you need to pass a reference to an event handler. Do it like this:

Public Class Form1
Private Sub RunProcess(ByVal Sender As Object, ByVal e As EventArgs)
'Process.Start(directory)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolStrip1.Items.Add("Run", Nothing, AddressOf RunProcess)
End Sub
End Class

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

edit:

ive got another problem now, when it loads from the DB it sets the directory to the same place for all of them!(the last loaded record's directory)

this is the code im using, where am i ever going wrong!?

** my on load event run the sub calldata

Private Sub RunProcess(ByVal Sender As Object, ByVal e As EventArgs)

Process.Start(appspath)

End Sub

Sub calldata()

'dim connections'

Dim MyConn As ADODB.Connection

Dim MyRecSet As ADODB.Recordset

'open connection'

MyConn = New ADODB.Connection

MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & startuppath & "\apps\apps.mdb;"

MyConn.Open()

'select to a recordset with SQL SELECT statement'

MyRecSet = MyConn.Execute("SELECT ID, name, Category, path FROM apps")

'get and set individual records'

Do Until MyRecSet.EOF

appsname = MyRecSet.Fields.Item("Name").Value

appscat = MyRecSet.Fields.Item("category").Value

appspath = MyRecSet.Fields.Item("path").Value

fulleditors.DropDownItems.Add(appspath, Nothing, AddressOf RunProcess)

'goto next record and then loop forever

MyRecSet.MoveNext()

Loop

'close connection at the end of the records

MyConn.Close()

End Sub

jwalker343 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
The

event handler needs to know which program needs to be started.

You could use the Tag property of the ToolStrip button to pass that

information. This code demonstrates the technique:

Public Class Form1

Private Sub RunProcess(ByVal Sender As Object, ByVal e As EventArgs)

Dim item As ToolStripItem = CType(Sender, ToolStripItem)

If Not item Is Nothing Then Process.Start(CStr(item.Tag))

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim item As ToolStripItem = ToolStrip1.Items.Add("Run", Nothing, AddressOf RunProcess)

item.Tag = "c:\windows\system32\cmd.exe"

End Sub

End Class

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

It's set up to do exactly what you describe. All of you code it not here but... appspath is set to the last read record.

Process.Start(appspath)

You should do something like this:

1.) Put in a standard event handler... this will be important

Private Sub DropDown_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles DropDown.DropDownItemClicked

2.) In your loop, add items like in this fashion:

DropDown.DropDownItems.Add(appsname)
DropDown.DropDownItems(DropDown.DropDownItems.Count - 1).Name = appsname
DropDown.DropDownItems(DropDown.DropDownItems.Count - 1).Tag = appspath

3.) On item selection do this:

Private Sub DropDown_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles DropDown.DropDownItemClicked

Process.Start(e.clickeditem.tag + e.ClickedItem.Name)

End Sub

There ya go!

ReneeC at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

im still having troubles guys, im sorry im new to VB and im trying to learn as i got,

reneec when i tried what you said it did not even add a button on the dropdown menu
could someone please explain a little more?

sorry for being such a pain, thanks agian

jwalker343 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
Show us your code again and tell us what is not working.

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

lol forget i said that, i found the problem.

i always use subs for my on load event so that it stays nice and neat (ex my on_load only has 8 lines each line runs a seperate sub)

i had just forgot to add that sub into the on_load

thanks guys i got it working and it runs very smooth thank youl soooo much

jwalker343 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...