| | |
How to Close User Control?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
hi, i am having problem with closing user control.
i have some function in my parent form(with panel2):
...and UserControlOne appears.
i want to close(remove) UserControlOne on Cancel()(Cancel is function in UserControlOne) and load another User Control (UserControl2) to panel2:
any ideas how to do that?!
i have some function in my parent form(with panel2):
c# Syntax (Toggle Plain Text)
public void ActivateUserControlOne() { panel2.Controls.Clear(); UserControlOne = new UserControlOne(); panel2.Controls.Add(this.UserControlOne); }
i want to close(remove) UserControlOne on Cancel()(Cancel is function in UserControlOne) and load another User Control (UserControl2) to panel2:
c# Syntax (Toggle Plain Text)
public void Cancel() { this.Parent.Controls.Remove(this); }
any ideas how to do that?!
Last edited by peter_budo; 26 Days Ago at 7:26 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
0
#2 27 Days Ago
since you always clear the controls on the panel first, seems like it will always be the only control on the panel, so
but you could also use
seems like there could be several ways to go about it.
you could even expose the panel2 as a public property of the form and modify its contents directly through the property.
panel2.controls. clear(); should work.but you could also use
panel2.Controls[0].Remove(); seems like there could be several ways to go about it.
you could even expose the panel2 as a public property of the form and modify its contents directly through the property.
0
#3 27 Days Ago
milosz: What happens when your code runs? I haven't tested it but it looks like it should run OK...
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#4 27 Days Ago
•
•
•
•
since you always clear the controls on the panel first, seems like it will always be the only control on the panel, sopanel2.controls. clear();should work.
but you could also usepanel2.Controls[0].Remove();
seems like there could be several ways to go about it.
you could even expose the panel2 as a public property of the form and modify its contents directly through the property.
should i access panel2 with following code:
string test = this.Parent.Name; //doesn't work, parent is NULL ?!
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#5 27 Days Ago
my code is ok. UserControlOne disappears from panel2. But I want somehow to enable for UserControlTwo to appear when I close UserControlOne. I can do that by using events, but is there any other way?
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#7 25 Days Ago
so what are they?! please post example for some solutions, because i tried creating event handler, but i think it's too complicated to do that.
0
#8 23 Days Ago
on the usercontrol's cs file create an event delegate and event like so
then when the usercontrol needs to tell the form its time to be removed. just call that event
on the main form you just create your event handlers as usual.
something simple that that should work. sorry its been a while since I have been on because I have been really busy.
C# Syntax (Toggle Plain Text)
public delegate void onMyEventHandler(object sender, EventArgs e); public event onMyEventHandler OnMyEvent;
then when the usercontrol needs to tell the form its time to be removed. just call that event
C# Syntax (Toggle Plain Text)
OnMyEvent(this, new EventArgs(null));
on the main form you just create your event handlers as usual.
C# Syntax (Toggle Plain Text)
usercontrol myUserControl = new usercontrol(); myUserControl.OnMyEvent += new myUserControl.onMyEventHandler(catchevent); void catchevent(object sender, EventArgs e) { Panel2.Controls.remove(((control)sender)); //or Panel2.Controls.Clear(); }
something simple that that should work. sorry its been a while since I have been on because I have been really busy.
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#9 22 Days Ago
thank you for support. your posts was very helpful.
i find my way to display another usercontrol after closing one:
in usercontrols you should add:
make sure that cancel button is public. In main form you should create eventhandler for usercontrol:
also add in mainform constructor following code:
That works
So this issue is finally resolved
Thank you all once again.
i find my way to display another usercontrol after closing one:
in usercontrols you should add:
C# Syntax (Toggle Plain Text)
private void butnCancel_Click(object sender, EventArgs e) { this.Parent.Controls.Remove(this); }
C# Syntax (Toggle Plain Text)
public void OnCancelUserControlOne(object sender, EventArgs e) { //MessageBox.Show("oops!"); panel2.Controls.Clear(); MyUserControlTwo userControlTwo=new MyUserControlTwo(); panel2.Controls.Add(userControlTwo); }
C# Syntax (Toggle Plain Text)
userControlOne=new MyUserControlOne(); this.userControlOne.Click += new EventHandler(OnCancelUserControlOne);
That works

So this issue is finally resolved
Thank you all once again. ![]() |
Other Threads in the C# Forum
- Previous Thread: Compare disparate data using C#
- Next Thread: Binding data to the custom DataGridView
| Thread Tools | Search this Thread |
+ .net 3.5 access activedirectory advice app array asp.net buttons c# c++ camera char check code combobox connect control custom database datetime dbconnection development directrobot dll dubai ecommerce editor enabled eventhandlers facebook file files form forms gadgets gdi+ generated hardware http httpwebrequest image index inverter javascript keypress label lcd linked linkedin linux list login mdd mobilephone msword mysql opacity oracle panel password photoshop php post program programming read remote remoting research resource richtextbox single smoobjects softwaredevelopment sql sql-server sqlserver stream string stringformatting table text textbox totaldays update user useraccounts users vb vb.net visual visualbasic webbrowser whileloop winforms wordautomation working wpf







