Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sharing forms between 2 menuitems and changing name property based on each selection using VB.Net 2003

Posted on 2006-11-28
9
Medium Priority
?
164 Views
Last Modified: 2010-04-23
I have a windows application (VS 2003) with a 2 menu item

Menu1 has the following submenus
form1
form2
form3

Menu2 has the following submenus
form1
form2
form3

Only one copy each of form1, form2 and form3 exist.
I want to be able to use the threee forms between the two submenus
so that when a user clicks on Menu1- form1 he gets this
and when a user clicks on Menu2 - form1 he still gets form1.
The name property should show where the form is been called.


I will be reading sqlconnection strings from the app.config file
and they each vary for Menu1 and Menu2(totally different)


How can I code this so that when the user clicks on Menu1 - form1,
the connectionstring from app.config is retrieved and when
user clicks on Menu2 - form1, another connection string from app.config is used.


Thanks.

0
Comment
Question by:Sirdots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18032291
Hi Sirdots,

in your menu item click event, set the name property of the form:

form1.Name = "Form1 - from menu1"

and from the other:

form1.Name = "Form1 - from menu2

Cheers,
Hillwaaa
0
 

Author Comment

by:Sirdots
ID: 18032327
Thanks Hillwaaa, Maybe I should explain better. I want to be able to determine where (i.e from which menuItem) the forms are been called. This is because

Menu1 - form1 has a different connection string

Menu2 - form2 has a differenct connection string.

I hope this helps.

Thanks.
0
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 2000 total points
ID: 18032371
maybe something like

In form1

'declare variable
Dim calledFrom as String

'add sub
    Public Sub setCalledFrom(ByRef from As String)
        calledFrom = from
    End Sub

'in the onload:
if calledFrom = "Menu1" then
    'called from Menu1
    ....
else
    'called from Menu2
    ....
end if

-----

and in the MenuItem click event, use:

form1.setCalledFrom("Menu1") or form1.setCalledFrom("Menu2") as appropriate.
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.

 

Author Comment

by:Sirdots
ID: 18032484
Thanks Hilwaaaa. Now what does the ByRef "from"  stand for?
Am I going to pass the name of Menu1?
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18032512
No worries-  the "from" is just a string.

So when you click on Menu1 - form1, you would call the "setCalledFrom" function in form1 passing in this string, which can be whatever you want it to be, but the idea is that it indicates that the form was opened using Menu1.  This string then gets set to the "calledFrom" string - which should be a variable declared in form1.

Then when you are building your connection string, you can just use the IF statement to see if "calledFrom" indicates if the form was opened using menu1 or menu2.

Does that help?
0
 

Author Comment

by:Sirdots
ID: 18032669
Thanks. Now I am very close. What is happening is that I do not have access to the setCalledFrom("Menu1") if I do a  form1.setcalledFrom("Menu1")
from my menuclick event. I thought once I hit the dot it should give me the setCalledFrom in the intellisence but it is not doing that.

0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18032967
Did you set the "setCalledFrom" sub as "private"? - this will prevent access from your menuclick.  If so, change it to "public" or "friend".
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18036781
May I suggest a different approach ?

in Form1 create a sub new(ConnectionString as string)
and that should be it !

Like so

public class Form1
               inherits system.windows.forms.form
...
...
...
'these are the new lines to add
              Private ConnString as string = string.empty
              '
              Public sub New(ConnectionString as string) 'New In the form code
                     me.new()
                     me.ConnString = ConnectionString
                     me.ConnectionVariable = new Connection(ConnString)

                     'where ConnectionVariable is  the name of the connection variable you have in the form
                     'new Connection --- should be new SqlConnection or whatever connection type you have in the form
                   
              end sub

end class

in your menuItem Click event where you create the form do this

private sub Menu1Form1_click(sender as object, e as eventargs) handles menu1Form1.click
        dim frm as new form1("Menu1 Connection String should go here")
        frm.show()
end sub

private sub Menu2Form1_click(sender as object, e as eventargs) handles menu2Form1.click
        dim frm as new form1("Menu2 Connection String should go here")
        frm.show()
end sub

this should do it for you :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18036796
note that you need to reinitialize your database objects after setting the new connection string
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 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