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

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.

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

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

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.

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.

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.

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.

this code works
I've tried it for vb

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

Thread Closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.