Question MenuStrip Help

janu

Member
Joined
Jan 6, 2012
Messages
16
Programming Experience
Beginner
I am very happy to be the member of this forum .. I observed that I can learn here as I am New for Vb.Net..

My Ist question on this forum is :

I have a project based on web references

I have ComboBox added with two items by which the web service is called :

5mmycw.jpg



VB.NET:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ComboBox.Items.Add(New TargetServer("Product1", New Uri("https://www.*******************_id=7"), New Uri("https://www.*******************?WSDL")))
 ComboBox.Items.Add(New TargetServer("Product2", New Uri("https://www.*******************_id=9"), New Uri("https://*******************?WSDL")))
End Sub

How can I achieve same in MenuStrip Like this ..

141juir.jpg


Please help with code..
 
The Services menu item has a DropDownItems property that you can Add items too much like the Items collection of the ComboBox. When calling Add you would specify the text, an image (which can be Nothing) and a Click event handler. You can then assign your TargetServer object to the Tag property of the menu item it returns. Inside the event handler, you can get back the TargetServer from the Tag of the 'sender' and then use it appropriately, much as you would get the SelectedItem in the SelectedIndexChanged event handler of the ComboBox.
 
You may WANT code provided but you don't actually NEED it. How about you use the information provided and have a go at it for yourself. I've told you about the DropDownItems property and the Add method. Have you made any effort to research them? Have you read the relevant documentation for them? Have you searched the web for existing examples? I'll wager the answer is "no". I'm happy to help with things that people try but are unable to do for whatever reason but when they don't even try then it's not helping; it's just doing your work for you.
 
thanks .. I have tried a lot as you know .. you have helped me in any manner on other places also .. I respect your help..
as you said that I should assign Target server in tag property of menustrip item .. that I did but in code window how do i assign URI to particular menu item .. as TargetServer is a class in my project ..
 
i tried it this way .. I put the TargetServer in menustrip property Tag
VB.NET:
Private Sub ServiucesToolStripMenuItem_Click(ByVal sender As TargetServer, ByVal e As System.EventArgs) Handles services.Click
         ([COLOR=#ff0000]New[/COLOR] TargetServer("Product1", New Uri("https://www.*******************_id=7"), New Uri("https://www.*******************?WSDL")))
        End Sub

syntax error at New
when debugging
2z8uwxj.jpg
 
You haven't done what I said. First of all, why are you handling the Click event of the Services menu item when the user will never click it? When exactly do you want to add the child menu items? Not when the user clicks the Services menu item I'll wager. Presumably you want to do it when the form first loads, so what event should you be handling to do that? Also, where have you actually done what I said to add the menu items?
The Services menu item has a DropDownItems property that you can Add items too much like the Items collection of the ComboBox.
I don't see any sign of your using DropDownItems or Add.
 
thanks I did as you said .. I placed MenuStrip on the form and manually added File -> Services and then i went to properties of Services DropDownItems added two items namely product1 and product2 .. and in both added items I assigned TargetServer in Tag ..whereas TagetServer is class in my project... Now I am confused where to put this line of code as I have used it in combobox and trigger one of the item as the form loads..

VB.NET:
ComboBox.Items.Add(New TargetServer("Product1", New Uri("https://www.***************_id=9"), New Uri("https://www.**************?WSDL")))
 
When you call the Add method of the DropDownItems collection, it returns the new menu item that was created and added. You get the menu item returned by the Add method and then set its Tag property.
 
Let me explain to you sir , what actually I have and what I want

I have

VB.NET:
 Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
[COLOR=#00ff00]             'add target[/COLOR]
  ComboBox.Items.Add(New TargetServer("Produc1", New Uri("https://www.*************_id=7"), New Uri("https://www.*************?WSDL")))
 ComboBox.Items.Add(New TargetServer("Produc2", New Uri("https://www.*************_id=92"), New Uri("https://www*************?WSDL")))
           .
          [COLOR=#00ff00]  ' set up Web service[/COLOR]
            _service = New POSTI_Interface()
            _service.Timeout = 6000
           [COLOR=#00ff00] ' set up events[/COLOR]
            AddHandler _service.GetReleasesForProductCompleted, AddressOf Me._service_GetReleasesForProductCompleted
            AddHandler _service.GetReleaseVariantsCompleted, AddressOf Me._service_GetReleaseVariantsCompleted
            AddHandler _service.GetProductListCompleted, AddressOf Me._service_GetProductListCompleted
           [COLOR=#00ff00] ' trigger loader[/COLOR]
           ComboBox.SelectedIndex = 1
        End Sub
ComboBox_SelectedIndexChanged
VB.NET:
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox.SelectedIndexChanged
              _service.Url = DirectCast(ComboBox.SelectedItem, TargetServer).ServiceUrl.ToString()
            Dim cc As New CookieContainer()
            Dim req As HttpWebRequest = DirectCast(WebRequest.Create(DirectCast(ComboBox.SelectedItem, TargetServer).SessionNegotiationUrl), HttpWebRequest)
            req.CookieContainer = cc
            req.BeginGetResponse(New AsyncCallback(AddressOf GetSessionId), req)
        End Sub

So, Sir My aim is that instead of ComboBox I wish that I should use MenuItems like
141juir.jpg
 
So if you're adding the items in the designer then simply create a Click event handler for each one. In the event handler you do whatever is appropriate for that menu item. You don't need your TargetServer class at all.
 
So if you're adding the items in the designer then simply create a Click event handler for each one. In the event handler you do whatever is appropriate for that menu item. You don't need your TargetServer class at all.

that is the thing which makes me confused how to put this line of code as I have put it for combobox ...
VB.NET:
 ComboBox.Items.Add(New TargetServer("Produc1", New Uri("https://www.*************_id=7"), New Uri("https://www.*************?WSDL")))
how to put the same for menustrip click event handler
 
Back
Top