Solved

Hiding ContextMenuStrip and Spawned UserControl When Running Dynamically

Posted on 2014-02-12
3
589 Views
Last Modified: 2014-02-12
I need to programatically (dynamically) raise a specific contextmenustrip item called "Edit parameters" for controls on a Windows form (user GUI).   The loop below shows how the controls are looped over, and how the contextmenustrip items for each control are filtered for an "Edit" item, and then that item is dynamically fired.   The problem is that the user control is also visible -- which is the typical operation when a user wants to edit parameters -- that is, show the user control after right-clicking on the control's contextmenustrip item.  

Is there a way to (a) programatically load the user control (when the contextmenustrip item called "Edit parameters" is dynamically selected) , and then (b) select the "Apply" button, without making the user control visible?    All of this dynamic programming is performed to programatically raise all of the "Edit parameters" contectmenustrips, which spawns the user control, and then selects the apply button button.   There is actually no way to use default user control options in a separate method because every time the user changes the parameters, the controls and parameter values are serialized -- so yes, serializing is also being done.    However, the major issue is that I don't want the contextmenustrips to be visible during dynamic spawning and I don't want the user control to be visible when the "Edit parameters" item of the contextmenustrip is dynamically selected.  

How can this be done when keeping contextmenustrips invisible and user controls invisible?

The code below loops through each control's (in Me.Controls) contextmenustrip items to determine if an "Edit" item is found, and then raises the UC dynamically:

For Each ctitem In nxgContextMenuStrip.Items
             If InStr(ctitem.Text, "Edit") > 0 Then
                ctmseditparam_Click(ctitem, e)
                CurrentUCForm.Visible = False
                CurrentUCForm.SendToBack()
                Dim btn As Button = CurrentUCForm.Controls("ApplyButton")
                serializeBtn_Click(CurrentUCForm, e)
             End If
           Next

Open in new window


The code below raises the UC when the contextmenustrip item called "Edit parameters" of each control in Me.Controls is dynamically selected in the loop above.   Problem is, the UC is not invisible to the user.

        Dim myform As New Form
        myuc.autosize = True
        myform.AutoSize = True
        myform.Dock = DockStyle.Fill
        myform.Show()
        myform.Width = 0
        myform.Height = 0
        myform.Width = myuc.width
        myform.Height = myuc.height + 50
        myform.Left = 0
        If runbuttonclicked = 1 Then myform.Visible = False 'this does not hide the UC
        myform.Top = 0
        Dim btn As New Button
        btn.Text = "Apply"
        btn.Top = 10 ' myform.Bottom - 50
        btn.Left = 10 ' myform.Left + 10 'myform.Right - 60
        btn.Name = "ApplyButton"
        btn.BringToFront()
        myform.Controls.Add(btn)
        myform.Controls.Add(myuc)
    

Open in new window

0
Comment
Question by:lep1
  • 2
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39853643
Not sure I fully understand your problem.

There are couple of options based on my understanding of your problem.

1) Move code to separate functions which can be called from UI code when user uses the contextstrip and user control and which can also be called from other code programmatically.

2) Make the user control configurable or use a flag of some sort so that user control is not shown to the user when triggered programmatically.
0
 

Author Comment

by:lep1
ID: 39853699
I was able to hide the user control (form) however, I can't seem to hide the contexmenustrips when looping through their items to find an item with "Edit"

Does a contexmenustrip need to be visible in order to be used dynamically? I basically need to replicate what a user does when setting run parameters for a task control (in me.controls) by dynamically using g the contextmenustrip.

Thus, how can the items of a contextmenustrip be looped through without seeing the contextmenustrip during dynamic usage?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39853856
Again, can you not move code out of ctmseditparam_Click handler into a separate function which you can then call without needing to loop through the contextmenustrip.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form design in vb.net 7 35
Loop through Multiple Processes Async 2 29
Recommendation vb6 to vb.net or others 14 114
Write to file when application closes (by logoff) - VB.NET 1 23
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now