944,052 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 7770
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2009
0

How to Close User Control?

Expand Post »
hi, i am having problem with closing user control.

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

c# Syntax (Toggle Plain Text)
  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:

c# Syntax (Toggle Plain Text)
  1. public void Cancel()
  2. {
  3. this.Parent.Controls.Remove(this);
  4.  
  5. }

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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Nov 1st, 2009
0
Re: How to Close User Control?
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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Nov 1st, 2009
0
Re: How to Close User Control?
milosz: What happens when your code runs? I haven't tested it but it looks like it should run OK...
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 1st, 2009
0
Re: How to Close User Control?
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 ?!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Nov 1st, 2009
0
Re: How to Close User Control?
Click to Expand / Collapse  Quote originally posted by sknake ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Nov 2nd, 2009
0
Re: How to Close User Control?
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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Nov 3rd, 2009
0
Re: How to Close User Control?
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.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Nov 5th, 2009
0
Re: How to Close User Control?
on the usercontrol's cs file create an event delegate and event like so

C# Syntax (Toggle Plain Text)
  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


C# Syntax (Toggle Plain Text)
  1. OnMyEvent(this, new EventArgs(null));

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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Nov 6th, 2009
0
Re: How to Close User Control?
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  1. userControlOne=new MyUserControlOne();
  2. this.userControlOne.Click += new EventHandler(OnCancelUserControlOne);


That works

So this issue is finally resolved Thank you all once again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
milosz is offline Offline
11 posts
since Oct 2009
Feb 19th, 2010
0
Re: How to Close User Control?
this code works
I've tried it for vb
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gaurrzzz is offline Offline
1 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: C#.net windows application
Next Thread in C# Forum Timeline: Update Datagridview in multi-user environment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC