Embedding Controls In a ToolStrip

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
I have spent the best part of a day and half building a menu structure and learning how to embed my custom control into a toolstrip/toolstrip item with loads of help from people on here. Only to discover that if I overload the new sub with a parmeterised version in my control the control does not appear in the menu... :mad: :mad: :mad: :mad: :mad: :mad: :mad:

AAAaaaaaaaaaaarrrrrrrrrggggghhhhh:confused: :confused: :confused: :( :confused: :( :confused: :( :confused: :(
 
Last edited by a moderator:
Moorzee, i have changed the title of your thread to something a little less colourful, whilst i'm sure it would have grabbed someone's attention ( it certainly grabbed mine!!) Can you keep it relavent and clean please. Also moved this to the menus & toolbars forum.
 
Why would overloaded Sub New be a problem, Moorzee? (if Sub New is what you mean at all, you say 'overload the new sub' which is very unclear)
 
Sorry. My frustration got the better of me and I posted
a)With an inappropriate title
and
b)Without thoroughly checking my problem.:eek:

My problem was further down the line of execution. I had a routine in a class I have included as part of the control which erred withouth telling anybody about it. I really must learn this fancy try catch finally and implement correctly.:eek: :eek:

Apologies all. Lesson learnt;)

P.S. I had to include a routine which I wanted to occur in my main application, on a button click event in my custom control as I am not aware of a way around a prob. If I embed my control in a toolstrip as a toolstripitem how can I tell the app that the button on my ctrl has been clicked? Was thinking about threading and delegates but that stuff is way advanced for me at the mo. Out of interest, off the top of anyones head, would that be my direction?

Cheers.
 
Last edited:
The natural thing to do would be to declare this as a public event of your usercontrol, I would also identify the usercontrol instance in event sender parameter instead of the button instance inside the usercontrol. In usercontrol code you catch the click event of the button and raise your public event then. Example usercontrol code:
VB.NET:
Public Event ucButtonClick(ByVal sender As Object, ByVal e As EventArgs)
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    RaiseEvent ucButtonClick(Me, e) 'Me' is usercontrol instance
End Sub

When you add this usercontrol to the menustrip with ToolStripControlHost in code (which you have to do), subscribe to the event with the AddHandler statement. Example form code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    'add usercontrol to toolstrip
    Dim uc As New UserControl1
    AddHandler uc.ucButtonClick, AddressOf uc_ucButtonClick
    Dim host As New ToolStripControlHost(uc)
    ViewToolStripMenuItem.DropDownItems.Add(host)
End Sub
 
Sub uc_ucButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    MsgBox("ucbutton clicked")
    'do your stuff here 
    '(of course you could start any method asynchronous as a new thread if you wish, but that's another topic)
End Sub
 
Back
Top