Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on project for the past one month but I am really stucked. I have a form called MainForm which has 2 panels. One is called MainContainer and other called SubContainer. I have two other forms, one called Login and other SQLSettings. SubContainer is found inside MainContainer. SubContainer is suppose to hold all open forms. When MainForm loads at startup it opens Login form inside SubContainer. Login contains a linklabel which is also suppose to open SQLSettings form in SubContainer but nothing happens when i click on the linklabel in the Login form.

What I have tried:

this is the code for the linklabel which is suppose to open SQLSettings form:
this.Close();
            MainForm f = new MainForm();
            Form cur = new SQLSettings();
            f.SubContainer.Dock = DockStyle.None;
            f.SubContainer.Anchor = AnchorStyles.None;
            f.SubContainer.Size = cur.Size;
            f.SubContainer.Location = new Point(f.MainContainer.Width / 2 - f.SubContainer.Width / 2,
                f.MainContainer.Height / 2 - f.SubContainer.Height / 2);
            cur.TopLevel = false;
            f.SubContainer.Controls.Remove(f.currentForm);
            f.SubContainer.Tag = cur;
            f.SubContainer.Controls.Add(cur);
            f.SubContainer.Tag = cur;
            cur.BringToFront();
            cur.Show();
Posted
Updated 25-Jul-20 15:13pm
Comments
Otekpo Emmanuel 22-Jul-20 9:29am    
Try the following options;

1. Check if the link label or form is disabled.

2. Set the Login form as your startup form, then interact with the link label to see if it execute your codes

3. If it fails to perform the specified action, remove all the codes then just write code to show message

4. Try and add another link label then may be write a code to show message box and see if it works

Give your feedback
Richard MacCutchan 22-Jul-20 9:50am    
I am not sure exactly what you are trying to do here, but in the above code you have:
MainForm f = new MainForm();

and then you try
f.SubContainer.Controls.Remove(f.currentForm);

But you have not set the value of currentForm in the f variable. Maybe using proper names for your references would make things clearer.
MaximusDebois 22-Jul-20 10:32am    
I am trying to remove the login form from subcontainer panel and open the SQLSettings form when i click on linklabel on the login form
Richard MacCutchan 22-Jul-20 11:06am    
I think you need to study the answer below from OriginalGriff, and especially the link he provided.

That's because you do this:
MainForm f = new MainForm();Which creates - obviously - a new instance of the MainForm. That isn't the same instance as the user is looking at, so you adding controls and so forth to it doesn't affect what the user sees. You would need to access the MainForm instance that contains the current container, and that's not simple - because the container shouldn't know what contains it.

A better idea is to have the containers generate events which the form can subscribe to and do the work for them.
That's easier than it sounds: Transferring information between two forms, Part 2: Child to Parent[^] will show you how - it's talking about forms, but it's exactly the same for controls (a Form is a Control, just with a lot of bells and whistles added on).
 
Share this answer
 
Quote:
SubContainer is suppose to hold all open forms.
It is a mistake to put a Form inside any other ContainerControl, like a Form: you can use a Panel, or other ContainerControl, to host controls.

For security reasons, and to achieve separation of concerns ... so your app will be safe, and not a mess: use separate Forms for login and SQL configuration. Consider carefully the security required: should the user even see the main UI without being logged in ?

A LinkLabel control is usually used to open a Web page; while you can execute arbitrary code in its clicked event, why not just use a Button and.or Label ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900