How to Close User Control?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster

How to Close User Control?

 
0
  #1
22 Days Ago
hi, i am having problem with closing user control.

i have some function in my parent form(with panel2):

  1. public void ActivateUserControlOne()
  2. {
  3. panel2.Controls.Clear();
  4. UserControlOne = new UserControlOne();
  5. panel2.Controls.Add(this.UserControlOne);
  6. }
...and UserControlOne appears.
i want to close(remove) UserControlOne on Cancel()(Cancel is function in UserControlOne) and load another User Control (UserControl2) to panel2:

  1. public void Cancel()
  2. {
  3. this.Parent.Controls.Remove(this);
  4.  
  5. }

any ideas how to do that?!
Last edited by peter_budo; 20 Days Ago at 7:26 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 314
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 37
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
0
  #2
22 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 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #3
22 Days Ago
milosz: What happens when your code runs? I haven't tested it but it looks like it should run OK...
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #4
22 Days Ago
Originally Posted by Diamonddrake View Post
since you always clear the controls on the panel first, seems like it will always be the only control on the panel, so 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.
yes, that's ok. my code works too. but i want to in usercontrolone on Cancel close that control(that is done) and load another usercontrol(UserControl2). funny thing is that the panel2 is already a public property and i can't access it from UserControl2 (or i don't know how

should i access panel2 with following code:
string test = this.Parent.Name; //doesn't work, parent is NULL ?!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #5
22 Days Ago
Originally Posted by sknake View Post
milosz: What happens when your code runs? I haven't tested it but it looks like it should run OK...
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 314
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 37
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
0
  #6
21 Days Ago
you create an event handler from a button on the usercontrol in the main form then just do the removing there, Could be a lot of solutions.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #7
20 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.

Originally Posted by Diamonddrake View Post
you create an event handler from a button on the usercontrol in the main form then just do the removing there, Could be a lot of solutions.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 314
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 37
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
0
  #8
17 Days Ago
on the usercontrol's cs file create an event delegate and event like so

  1. public delegate void onMyEventHandler(object sender, EventArgs e);
  2. public event onMyEventHandler OnMyEvent;

then when the usercontrol needs to tell the form its time to be removed. just call that event


  1. OnMyEvent(this, new EventArgs(null));

on the main form you just create your event handlers as usual.

  1. usercontrol myUserControl = new usercontrol();
  2.  
  3. myUserControl.OnMyEvent += new myUserControl.onMyEventHandler(catchevent);
  4.  
  5. void catchevent(object sender, EventArgs e)
  6. {
  7. Panel2.Controls.remove(((control)sender));
  8. //or Panel2.Controls.Clear();
  9. }

something simple that that should work. sorry its been a while since I have been on because I have been really busy.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: milosz is an unknown quantity at this point 
Solved Threads: 0
milosz milosz is offline Offline
Newbie Poster
 
0
  #9
17 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:
  1. private void butnCancel_Click(object sender, EventArgs e)
  2. {
  3. this.Parent.Controls.Remove(this);
  4. }
make sure that cancel button is public. In main form you should create eventhandler for usercontrol:
  1.  
  2. public void OnCancelUserControlOne(object sender, EventArgs e)
  3. {
  4. //MessageBox.Show("oops!");
  5. panel2.Controls.Clear();
  6. MyUserControlTwo userControlTwo=new MyUserControlTwo();
  7. panel2.Controls.Add(userControlTwo);
  8. }
also add in mainform constructor following code:
  1. userControlOne=new MyUserControlOne();
  2. this.userControlOne.Click += new EventHandler(OnCancelUserControlOne);


That works

So this issue is finally resolved Thank you all once again.
Reply With Quote Quick reply to this message  
Reply

Tags
c#, control, panel, user

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC