9 Replies - 6007 Views - Last Post: 17 January 2013 - 12:42 PM Rate Topic: -----

#1 njhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-January 13

UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 10:42 AM

I have a usercontrol with a single combobox. I would like to use this same usercontrol on
two different forms. On each form, I seem to add the usercontrol properly, as it is visible on those forms:
myusercontrol control = new myusercontrol();
control.show();
myform1.Controls.Add(control);



On the usercontrol, I will get the index in the combobox through the SelectedIndexChanged() event.
Then, based on which of the two forms I am on, I would call the appropriate method. How, from the usercontrol,
can I tell which of the two forms that I am on, so that I know whether to call myform1.thefunction() or myform2.thefunction()?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: UserControl on Form, How To Know Which Form It Is Embedded On

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,526
  • Joined: 12-December 12

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 10:56 AM

I haven't tried it with two forms yet, but try

MessageBox.Show(myusercontrol.Parent.Name);

Make sure the Name is set for each form.

Otherwise, we might need to navigate through this or sender.

Edited: Actually, there is a specific method FindForm() to do just this:

myusercontrol.FindForm()

This is better anyway, as the Parent to a control needn't be a form (could be a panel, etc.).

This post has been edited by andrewsw: 17 January 2013 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:01 AM

That's not the point of a UserControl. The idea of a user control is something encapsulates its own custom state and behavior within a package that can be reused elsewhere. Consider the TextEditBox or the Button. It doesn't care what form you put it into. It just works in its own little world without regard for the form that contains it. And so likewise, that is how you should be designing your user control.

Just like with the built in controls, the way you can get your user control to interact with the containing forms is it register for events. So in the case of your user control, it should expose a Selectionchanged event to which myform1 or myform2 would register for notifications on. Then the event handlers of those respective forms would call the appropriate methods in response to the events.
Was This Post Helpful? 2
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,526
  • Joined: 12-December 12

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:07 AM

Skydiver is quite right of course ;). What's the point of creating a custom-control if it's behaviour is dependent on its parent-form.
Was This Post Helpful? 0
  • +
  • -

#5 njhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-January 13

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:12 AM

Thanks for the quick reply. Using the Parent.Name works and is what I should be doing.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:15 AM

And how well will that scale? If you use that user control in myform3, now you'll have to touch the code within your user control to know about myform3 as well. That's just not good object oriented design.
Was This Post Helpful? 0
  • +
  • -

#7 njhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-January 13

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:18 AM

Thanks for the explanation Skydiver. I chose the usercontrol because I want to deploy this on many forms. I said two but eventually it will be dozens, and I want each form to have the same look and feel for the portion that the usercontrol represents.

What would be more appropriate to use for this example?

Thanks
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,526
  • Joined: 12-December 12

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:20 AM

View Postnjhokie, on 17 January 2013 - 11:12 AM, said:

Thanks for the quick reply. Using the Parent.Name works and is what I should be doing.


Did you read Skydiver's posts?

I also edited my previous post to advise that Parent.Name is not reliable - Parent may not be the form and the Name may not be set to a known value. FindForm() is the way to do this, but you should re-think how you are approaching this subject.
Was This Post Helpful? 0
  • +
  • -

#9 njhokie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-January 13

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 11:29 AM

Yes I read his posts, just after I had already replied. And thanks for updating the .Parent information andrewsw. I suppose you are both right and I should rethink how to deploy this current usercontrol so that I avoid complications later during development.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: UserControl on Form, How To Know Which Form It Is Embedded On

Posted 17 January 2013 - 12:42 PM

View Postnjhokie, on 17 January 2013 - 12:18 PM, said:

Thanks for the explanation Skydiver. I chose the usercontrol because I want to deploy this on many forms. I said two but eventually it will be dozens, and I want each form to have the same look and feel for the portion that the usercontrol represents.

What would be more appropriate to use for this example?

Thanks


The user control should probably be raising an event, and the parent should be subscribing to that event. That way the form can react as needed.

So the UserControl raises an event that effectively says "Done with these results"... or just "done"... or "Logged in, with these user credentials". (I don't know the purpose of the user control). Then the parent (the form) reacts as needed.

Think of it this way - you drop 15 button on a form and don't have to do a thing to the button code. You have its parent form react to the .Click event for each button. You're going to do the same thing with the UserControl.

Stop working on your project for 10 minutes and go do this tutorial which is heavy on building UserControls and designing your program before banging on the keyboard.
Bulding an application - Part 1
Then re-approach your project with a better plan.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1