| | |
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; Nov 2nd, 2009 at 7:26 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
0
#2 Nov 1st, 2009
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 Nov 1st, 2009
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 Nov 1st, 2009
•
•
•
•
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 Nov 1st, 2009
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 Nov 3rd, 2009
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 Nov 5th, 2009
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 Nov 6th, 2009
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 |
Tag cloud for c#, control, panel, user
.net 2d access analyst array asp asp.net buttons c# chat class combobox console contorl control cs4 custom data database datagridview datetime dba default dell development dialog drawing excel expose expression facebook file files foreach form formatting freelance function gdi+ geometry google home httpwebrequest image index input java javascript laptop led linkedin linux list listbox login math mysql networking nokia object operator oracle panel password photoshop php picturebox platform post print program programming remote remoting reporting resource resourcefile richtextbox saving security sendkeys serialization session sql sql-server string textbox treeview update user validate validation vb.net vista webbrowser wia winforms wizard wpf write







