954,116 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Close User Control?

hi, i am having problem with closing user control.

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

public void ActivateUserControlOne()
{
            panel2.Controls.Clear();
            UserControlOne = new UserControlOne();           
            panel2.Controls.Add(this.UserControlOne);
}

...and UserControlOne appears.
i want to close(remove) UserControlOne on Cancel()(Cancel is function in UserControlOne) and load another User Control (UserControl2) to panel2:

public void Cancel()
{
	    this.Parent.Controls.Remove(this);

}


any ideas how to do that?!

milosz
Newbie Poster
11 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

milosz: What happens when your code runs? I haven't tested it but it looks like it should run OK...

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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 ?!

milosz
Newbie Poster
11 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
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?

milosz
Newbie Poster
11 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

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.
milosz
Newbie Poster
11 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

on the usercontrol's cs file create an event delegate and event like so

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

OnMyEvent(this, new EventArgs(null));


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

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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

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:

private void butnCancel_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);       
        }

make sure that cancel button is public. In main form you should create eventhandler for usercontrol:

public void OnCancelUserControlOne(object sender, EventArgs e)
        {
            //MessageBox.Show("oops!");
            panel2.Controls.Clear();
            MyUserControlTwo userControlTwo=new MyUserControlTwo();
            panel2.Controls.Add(userControlTwo);
        }

also add in mainform constructor following code:

userControlOne=new MyUserControlOne();
this.userControlOne.Click += new EventHandler(OnCancelUserControlOne);

That works :)

So this issue is finally resolved :) Thank you all once again.

milosz
Newbie Poster
11 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

this code works
I've tried it for vb

Gaurrzzz
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I'm glad you got it helpful. If you want to ask question, start your own thread.

Thread Closed.

__avd
Posting Genius (adatapost)
Moderator
8,646 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,240
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: