Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2017-04-07
7
Medium Priority
?
64 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 36

Assisted Solution

by:Kimputer
Kimputer earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

715 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