473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scope UserControl to MainForm

Hello:

What is the correct way to target a method on the MainForm from a
UserControl. For example:

namespace SampleApp
{
public partial class MainForm : Form
{
internal void ShowControl()
{
//code to make a control visible
}

}

public partial class UserControl1 : UserControl
{
private void Button_Click(ob ject sender, System.EventArg s e)
{
//how do I target ShowControl() in the MainForm
}
}
}

Thank you!
Jul 6 '06 #1
2 4187
Benedict,

You have to have a reference to the form in the user control. You can pass
it as a constructor parameter, set it in a proeprty or have special init
method for this. You can also use FindForm method from the UserControl code
that will give you reference to the form that hosts the control.

Just keep in mind that probably this control may not be hosted in a form
that has ShowControl method.

For example FindForm method will return reference to an object of type Form,
which you need to cast to a type that support this show method. If the cast
is possible (you need to check first) then call the method.

If you use parameter or proeprty to pass the form reference you can strongly
type it to accept only special base type or beter yet an interface the the
forms hosting your user control must implement. This interface will declare
the ShowControl method.

There is another solution that I like better than the previous ones. Instead
of making the control knowing too much about the host. Let the control
control fire an even when ShowControl method needs to be called. The forms
that support this feature need to subscribe for this event and act
accodingly. Thus the control can be used in any form, but only the special
once will react on this event.

--
HTH
Stoitcho Goutsev (100)

"Benedict" <Be******@discu ssions.microsof t.comwrote in message
news:5D******** *************** ***********@mic rosoft.com...
Hello:

What is the correct way to target a method on the MainForm from a
UserControl. For example:

namespace SampleApp
{
public partial class MainForm : Form
{
internal void ShowControl()
{
//code to make a control visible
}

}

public partial class UserControl1 : UserControl
{
private void Button_Click(ob ject sender, System.EventArg s e)
{
//how do I target ShowControl() in the MainForm
}
}
}

Thank you!

Jul 6 '06 #2
Stoitcho - this is really helpful, thank you!

Conceptually I understand what needs to be done, I'll look for some samples
on MSDN. Thanks!

Ben

"Stoitcho Goutsev (100)" wrote:
Benedict,

You have to have a reference to the form in the user control. You can pass
it as a constructor parameter, set it in a proeprty or have special init
method for this. You can also use FindForm method from the UserControl code
that will give you reference to the form that hosts the control.

Just keep in mind that probably this control may not be hosted in a form
that has ShowControl method.

For example FindForm method will return reference to an object of type Form,
which you need to cast to a type that support this show method. If the cast
is possible (you need to check first) then call the method.

If you use parameter or proeprty to pass the form reference you can strongly
type it to accept only special base type or beter yet an interface the the
forms hosting your user control must implement. This interface will declare
the ShowControl method.

There is another solution that I like better than the previous ones. Instead
of making the control knowing too much about the host. Let the control
control fire an even when ShowControl method needs to be called. The forms
that support this feature need to subscribe for this event and act
accodingly. Thus the control can be used in any form, but only the special
once will react on this event.

--
HTH
Stoitcho Goutsev (100)

"Benedict" <Be******@discu ssions.microsof t.comwrote in message
news:5D******** *************** ***********@mic rosoft.com...
Hello:

What is the correct way to target a method on the MainForm from a
UserControl. For example:

namespace SampleApp
{
public partial class MainForm : Form
{
internal void ShowControl()
{
//code to make a control visible
}

}

public partial class UserControl1 : UserControl
{
private void Button_Click(ob ject sender, System.EventArg s e)
{
//how do I target ShowControl() in the MainForm
}
}
}

Thank you!


Jul 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
1711
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
4
10066
by: Henry Padilla | last post by:
I am making a UserControl that is entirely taken up by Labels. I have found a way to pass the Click even to the outside (I have the same handler for each label.click and that raises a this.OnClick(e); ) How do I do this with whatever mystery is the ToolTip component? Since the UserControl surface is not exposed, it will never send the ToolTip whatever it needs to fire. I've tried the OnMouseHover() I've tried looking for the ToolTip...
9
2452
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx"); ucline.Initializecontrol(line,alternate); Placeholder1.Controls.Add(ucline); }
5
1195
by: Steve | last post by:
Hi all, In a module I have the following structure.. Public Structure ButtonStatusFlags Public butOK As Boolean Public butNew As Boolean Public butView As Boolean
2
2352
by: lotus | last post by:
HI All.. I'm realtively new to C#. I have MainForm which includes Parent usercontol, and this parent usercontrol also contains child usercontrol. MainForm --> Parent usercontrol --> child usercontrol Parent usercontol has one button to change the child usercontorl's variable.
9
369
by: Jacek Jurkowski | last post by:
.... make COM exposed object to have both constructors COM visible not only the first one?
4
1771
by: andersch | last post by:
Hi I display a xml file with the TreeView control (Windows Forms) on my MainForm. Now, I would like to be able to edit this xml file (XmlElements) when I select a node in this TreeView. For the editing, I have a UserControl with the corresponding controls (textboxes, buttons, ...). But I do not know how! :-(
7
14649
by: Andrus | last post by:
I have UserControls in MDI child forms containing TextBoxes and other controls. When user re-activates form, I need that Control which was last activated is activated again. Currently *first* control is activated always. To reproduce: 1. Run code. 2. Make TextBox2 as current TextBox by selecting its text
6
8359
by: kucheravy | last post by:
Hi, everybody. I have this problem. When I put a <asp:ListBox on a web page and populate the data in the page Page_Load event the ViewState for the control is saved and loaded (after postback) correctly. In other words the Items property of the ListBox is populated from the ViewState after the postback. However if I put this ListBox inside a UserControl, expose the Items list, and populate it with the data in the same manner (in the page...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.