Solved

Difficulty in writing procedure to determine which item of ToolStrip dropdown was clicked.

Posted on 2017-04-07
7
40 Views
Last Modified: 2017-04-14
I am rewriting a VB6 application in VB 2017
The new version has a ToolStrip dropdown button named TStripDropD_Sort. It is populated at run time. The code so far is based on the example here:
https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitemclickedeventargs(v=vs.110).aspx

This is the procedure statement:  
Private Sub TStripDropD_SortItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles TStripDropD_Sort.ItemClicked

Open in new window


It gives a compile-time error which I don't know how to resolve:
'Handles clause requires a WithEvents variable defined in the containing type or one of its base types'
0
Comment
Question by:GrahamSkan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 250 total points
ID: 42083576
It's usually better to provide full code.

In this case, it seems your procedure is written for form based designed controls.
If you add runtime controls, you need to add handlers, and map them to other code.

If you follow this example, you'll probably figure it out: https://msdn.microsoft.com/en-us/library/ms229625(v=vs.110).aspx
0
 
LVL 76

Author Comment

by:GrahamSkan
ID: 42083785
Thanks. I added the dropdown control at design time. Only the dropdown items are added at run time. Because of that difference, I am not sure what to take from the example.

Not sure which files to attach, so I've added all the files in the root of the project folder.
Form1.vb
FamilyRewrite.vbproj
App.config
fammod.vb
Form1.Designer.vb
Form1.resx
0
 
LVL 34

Expert Comment

by:it_saige
ID: 42089901
You just need to specify the control that you are implementing the event on.  In your case change this:
Private Sub TStripDropD_SortItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles TStripDropD_Sort.ItemClicked

Open in new window

To this:
Private Sub TStripDropD_SortItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles TStripDropD_SortItem_DropdownItemClicked.DropDownItemClicked

Open in new window


-saige-
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 76

Author Comment

by:GrahamSkan
ID: 42091409
Thanks it_saige. Unfortunately this produces a compile-time error as before:
BC30506        Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 250 total points
ID: 42091601
In your designer file, you should see an object declaration, initialization and property definitions for your toolstrip; e.g. (in order of appearance) -
' Inside of and near the top of the InitializeComponent() Sub
' Object Initializations
Me.MyControl = New System.Windows.Forms.ToolStripDropDownButton()

' Farther down in the designer file - still inside of the InitializeComponent() Sub
' Object property definitions
'
'MyControl
'
Me.MyControl.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
Me.MyControl.Image = CType(resources.GetObject("MyControl.Image"), System.Drawing.Image)
Me.MyControl.ImageTransparentColor = System.Drawing.Color.Magenta
Me.MyControl.Name = "MyControl"
Me.MyControl.Size = New System.Drawing.Size(29, 22)
Me.MyControl.Text = "This is my control"

' Near the end of the designer file - Outside of the InitializeComponent() Sub
' Object declararions
Friend WithEvents MyControl As ToolStripDropDownButton

Open in new window

When I look in your attached Designer file, I do see that you have your control (TStripDropD_SortItem_DropdownItemClicked) decorated with the WithEvents keyword.

I also tested your attached Designer, Code and Module files.  After commenting the databindings [since I don't have the database], I debugged the solution and got the following feedback:

Initial load -Capture.PNGDrop-down with runtime added items -Capture.PNGFirst message box after selecting 'Forenames, YOB,Surname Order' -Capture.PNGSecond message box after selecting 'Fornames, YOB,Surname Order' -Capture.PNGThird message box after selecting 'Fornames, YOB,Surname Order' -Capture.PNGFourth message box after selecting 'Fornames, YOB,Surname Order' -Capture.PNGFinal message box after selecting 'Fornames, YOB,Surname Order' -Capture.PNG
Since you are not naming the items, you want to look at their Text property in order to know which items was clicked; e.g. -
MsgBox(e.ClickedItem.Text)

Open in new window

Produces the following first message box -Capture.PNGYou could also create New ToolStripItems and Provide names for them in your SetUpNavToolStrip() Sub; e.g. -
Sub SetUpNavToolStrip(BindingSource1 As BindingSource)
    TStripTB_Forename1.TextBox.DataBindings.Add("Text", BindingSource1, "Forename1")
    TStripTB_Forename2.TextBox.DataBindings.Add("Text", BindingSource1, "Forename2")
    TStripTB_Surname.TextBox.DataBindings.Add("Text", BindingSource1, "Surname")
    TStripTB_YOB.TextBox.DataBindings.Add("Text", BindingSource1, "YOB")
    TStripTB_Sex.TextBox.DataBindings.Add("Text", BindingSource1, "SEX")
    TStripTB_Member_Id.TextBox.DataBindings.Add("Text", BindingSource1, "MEMBER_ID")

    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("Date Order") With {.Name = "tsmiDO"})
    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("Surname, Forenames Order") With {.Name = "tsmiSFO"})
    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("Surname, YOB, Forenames Order") With {.Name = "tsmiSYFO"})
    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("Forenames, Surname Order") With {.Name = "tsmiFSO"})
    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("Forenames, YOB,Surname Order") With {.Name = "tsmiFYSO"})
    TStripDropD_SortItem_DropdownItemClicked.DropDownItems.Add(New ToolStripMenuItem("ID Order") With {.Name = "tsmiIO"})
End Sub

Open in new window

Which would now give you the following in the first message box -Capture.PNG
-saige-
0
 
LVL 76

Author Closing Comment

by:GrahamSkan
ID: 42092934
it_saige,
Thanks for all the trouble you have taken to help with this question.
I have now changed the event in the Handles clause to match the  declaration in the Designer code and, of course, it now works.
I can get on with capturing the text from the clicked item.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 42093016
It was no trouble at all.  Glad to be of assistance.

-saige-
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question