Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 75
  • Last Modified:

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

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
GrahamSkan
Asked:
GrahamSkan
  • 3
  • 3
2 Solutions
 
KimputerCommented:
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
 
GrahamSkanAuthor Commented:
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
 
it_saigeDeveloperCommented:
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.

 
GrahamSkanAuthor Commented:
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
 
it_saigeDeveloperCommented:
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
 
GrahamSkanAuthor Commented:
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
 
it_saigeDeveloperCommented:
It was no trouble at all.  Glad to be of assistance.

-saige-
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now