473,789 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically-created menus and mdi children

I posted a similar message earlier but we've geared away from the
original topic somewhat so I thought I'd post again in hopes of getting
input from others.

I am creating a MenuStrip dynamically. For each ToolStripMenuIt em, I'm
adding an event handler so that the same routine gets called regardless
of which menu item was clicked. All of this works great - my menu is
being loaded properly and my routine gets called properly.

Within the Tag property of each ToolStripMenuIt em I store the name of a
form that I want opened as an mdi child of my main form. I couldn't
get this to work properly, so for testing purposes I've reduced my
problem to the following scenario (which really has nothing to do with
the Tag property or the fact that my form names are stored in
variables):

Behind my main form I created the following routine:

Private Sub LoadForm()
Dim frm As New frmTest
With frm
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With
End Sub

I also added a button to my form and in the click event of the button,
I call the above routine.

In my routine that gets called when a menu item is clicked, I call the
same LoadForm routine.

If I click my button, frmTest loads as an mdi child as it should.
If I click a menu item, my form does not get loaded (and yes, I've
confirmed that the routine is getting called).
Incidentally, if I comment out the line that says '.MdiParent = Me', my
form will open as a normal Windows form. However, as soon as I
uncomment out that line, my form will not load.

I'm puzzled as to why such a simple thing would work when I click a
button but not when I click a menu item.

As an aside, I tried loading a menu with static menu items (instead of
loading it dynamically), and everything worked fine - my mdi child form
loaded properly.

Any help would be greatly appreciated.

Thanks,

Steve

Apr 15 '06 #1
12 3176
hi Steve,

show the piece of code where you hook the handlers to the menuitems.
Is it done within the MDIParent Load sub?

-tom

Apr 15 '06 #2
Hi Tom,

The following two routines are used to load my main menu. The first
one is for my "main" items, and the second one gets called recursively
to load all subitems. The first routine gets called during the load of
my main form.

Public Sub LoadMainMenu(By Val prmMenu As MenuStrip)
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim drd As SqlDataReader
Dim strSQL As String

prmMenu.Items.C lear()

con = New SqlConnection(T HAGlobal.Connec tString)
If IsECCI() Then
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormNam e, MenuItemFunctio nName, MenuItemECCIOnl yFlag FROM
MenuItems WHERE (MenuItemDispla yFlag=1) And (SubMenuOf Is Null) ORDER
BY MenuItemDisplay Order"
Else
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormNam e, MenuItemFunctio nName, MenuItemECCIOnl yFlag FROM
MenuItems WHERE (MenuItemDispla yFlag=1) And (SubMenuOf Is Null) ORDER
BY MenuItemDisplay Order"
End If
cmd = New SqlCommand(strS QL)
cmd.Connection = con
Try
con.Open()
Catch ex As Exception
Exit Sub
End Try
drd = cmd.ExecuteRead er

Try
Do While drd.Read
Dim tsi As New ToolStripMenuIt em

With tsi
.Name = drd("MenuItemID ")
.Text = drd("MenuItemNa me")
.Tag = vbNullString
If drd("MenuItemFo rmName").ToStri ng <> vbNullString
Then
.Tag = "F_" & drd("MenuItemFo rmName").ToStri ng
ElseIf drd("MenuItemFu nctionName").To String <>
vbNullString Then
.Tag = "U_" &
drd("MenuItemFu nctionName").To String
End If
If drd("MenuItemEC CIOnlyFlag") = 1 Then
.ForeColor = Color.Red
End If
End With
If tsi.Tag <> vbNullString Then
AddHandler tsi.Click, AddressOf
frmMain.MainMen uClicked
End If
Call LoadMenuItems(t si)
prmMenu.Items.A dd(tsi)
Loop
drd.Close()

Catch ex As Exception
Finally
con.Close()
End Try
End Sub

Private Sub LoadMenuItems(B yVal prmParent As ToolStripMenuIt em)
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim drd As SqlDataReader
Dim strSQL As String

con = New SqlConnection(T HAGlobal.Connec tString)
If IsECCI() Then
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormNam e, MenuItemFunctio nName, MenuItemECCIOnl yFlag FROM
MenuItems WHERE (MenuItemDispla yFlag=1) And (SubMenuOf = " &
prmParent.Name & ") ORDER BY MenuItemDisplay Order"
Else
strSQL = "SELECT MenuItemID, MenuItemName,
MenuItemFormNam e, MenuItemFunctio nName, MenuItemECCIOnl yFlag FROM
MenuItems WHERE (MenuItemDispla yFlag=1) And (SubMenuOf = " &
prmParent.Name & ") And (MenuItemECCIOn lyFlag = 0) ORDER BY
MenuItemDisplay Order"
End If
cmd = New SqlCommand(strS QL)
cmd.Connection = con
Try
con.Open()
Catch ex As Exception
Exit Sub
End Try

drd = cmd.ExecuteRead er

Try
Do While drd.Read
Dim tsi As New ToolStripMenuIt em
With tsi
.Name = drd("MenuItemID ")
.Text = drd("MenuItemNa me")
If drd("MenuItemFo rmName").ToStri ng <> vbNullString
Then
.Tag = "F_" & drd("MenuItemFo rmName").ToStri ng
ElseIf drd("MenuItemFu nctionName").To String <>
vbNullString Then
.Tag = "U_" &
drd("MenuItemFu nctionName").To String
End If
If drd("MenuItemEC CIOnlyFlag") = 1 Then
.ForeColor = Color.Red
End If
End With
If tsi.Tag <> vbNullString Then
AddHandler tsi.Click, AddressOf
frmMain.MainMen uClicked
End If
Call LoadMenuItems(t si)
prmParent.DropD ownItems.Add(ts i)
Loop
drd.Close()

Catch ex As Exception
Finally
con.Close()
End Try
End Sub

You can see that I added an Event Handler to the Click event of my menu
items (only the relevant ones). The Click event calls the following
routine, which is located in my main form.

Public Sub MainMenuClicked (ByVal sender As System.Object, ByVal e
As System.EventArg s)
Call LoadForm()
End Sub

LoadForm looks like this:

Private Sub LoadForm()
Dim frm As New frmParticipantS earch
With frm
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With
End Sub

I also added a button with the following code:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Call LoadForm()
End Sub

As I said before, if I click my button everthing works fine - my form
loads as an mdi child.

I've confirmed that my MainMenuClicked event is getting called when I
click on any menu item that has a Tag assigned to it, as it should. If
I step through the code at runtime using breakpoints, it'll call
LoadForm and process every line with no errors, but I won't see my
form.

I removed it from here, but I previously added event handlers to the
Load and FormClosed events of my test form just to see if the form was
being loaded and immediately closed, but those events didn't fire (they
were MsgBoxs that did fire when I clicked my button and subsequently
closed my test form).

The thing that gets me most is that if I comment out the line that
makes my test form an mdi child, it works. I just can't assign my test
form as an mdi child if it's being called via the menu.

Also, I added a static menu and behind the click of one of the items I
called LoadForm. Everything worked.

Again, thanks for assistance.

Steve

Apr 15 '06 #3
Let's do a first attempt. Try replacing:

AddHandler tsi.Click, AddressOf frmMain.MainMen uClicked()

with

RemoveHandler tsi.Click, AddressOf frmMain.MainMen uClicked()
AddHandler tsi.Click, AddressOf frmMain.MainMen uClicked()

and let me know if anything changes...

-t

Apr 15 '06 #4
Clearly, in all the occurrences of addhandler (at least 2) ...

Apr 15 '06 #5
Tried it...no luck...

Apr 15 '06 #6
If you have not already done it, try putting a RemoveHandler before
*each* AddHandler, everywhere.
As you describe the problem, it seems a reentrancy problem...

-t

(PS.
Probably it would be easier if you reorganize some parts of code. In my
experience it is usually not advisable to rely on the names of the
controls in the code, as it makes difficult maintenance...)

Apr 15 '06 #7
Yes, I put it before every AddHandler, everywhere.

As for your PS, I'm not sure what you mean unless you're referring to
my use of frmMain. Can you elaborate a little (but not so much as to
get away from the real issue here :) )

Thanks again. I'll be checking back in the morning. I'm done for the
night.

Apr 15 '06 #8
hi Steve,

I pasted you code in my editor connecting to an mdb I made on the fly.
Actually it seems to be working. The child forms get open.

One thing I am not clear. How come in :

SubMenuOf = " & prmParent.Name & ") ORDER BY

you do not need quotes as in:

SubMenuOf = """ & prmParent.Name & """) ORDER BY

did they get lost in the paste?

-tom

Apr 15 '06 #9

Actually, the Name of the Parent is an integer, as is the value of the
SubMenuOf field. I thought about changing this but haven't done so
yet.

I'm going to try creating a new application and pasting my code into
it. Maybe something is just screwy with my app.

I'll let you know what I find out.

Thanks.

Apr 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2271
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML source:
7
3262
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
1
1022
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
5
9772
by: Angel | last post by:
Is there a way to create an IFRAME dynamically via VB.NET. In other words creating the HTML element in the server side code? thanks in advance....
7
6787
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al = LoadOracleData(sql) '____Do amazing things
9
7042
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related to VB, plus the address bar would show something like this: www.somethinghere?id=3 and if you change number 3 from the address bar to (for example) 34 or 71 you would get different page with the same formatting like I.e: www.somethinghere?id=34...
6
11082
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
7
2152
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ", "")), adminsection2).RefreshSection() Next End Sub
5
4454
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve the values of the controls when you perform a postback, why add the controls in the firstplace?...
7
6672
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.