473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a usercontrol as a form parameter

Hi,

I currently have a control that is on a form [FormA] and I want to pass that
exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a constructor
in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeCompo nent();
_passedInContro l = ctrl; /* used later to set the parent property
back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property
back to the original */
ctrl.Parent = this;
this.Controls.A dd(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(o bject sender, CancelEventArgs e)
{
if (this._passedIn Control != null)
_passedInContro l.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears from
FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to
appear on FormB?

Thanks in advance,
cj

Nov 15 '05 #1
2 2724
Curtis,

You shouldn't be passing the control to another form. Rather, the
control should use a memento pattern to store its state into an object that
can be passed around easily. Basically, the control should expose an object
that exposes the state and then you can pass that to your new form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Curtis Justus" <se*****@nospam .ser.it.is.com> wrote in message
news:e%******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi,

I currently have a control that is on a form [FormA] and I want to pass that exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a constructor in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeCompo nent();
_passedInContro l = ctrl; /* used later to set the parent property
back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property
back to the original */
ctrl.Parent = this;
this.Controls.A dd(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(o bject sender, CancelEventArgs e)
{
if (this._passedIn Control != null)
_passedInContro l.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears from FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to
appear on FormB?

Thanks in advance,
cj

Nov 15 '05 #2
Nicholas,

Thanks for the help. I was going to do that as a last resort. The reason
why is that the object I would be passing around is a document viewer that
holds and instance of an Internet Explorer control. It takes some time to
fire that up and it takes up some resources. It will probably be displaying
a multi-page TIFF image that is used on a previous screen (this entire part
of the app is related to data entry off of an electronic image). I thought
I could avoid grabbing the state from the viewer in FormA and passing the
state to FormB. Since there doesn't seem to be another way to do it, that
will be what I'll have to do. Oh well.

Thanks again...

"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:#1******** ******@tk2msftn gp13.phx.gbl...
Curtis,

You shouldn't be passing the control to another form. Rather, the
control should use a memento pattern to store its state into an object that can be passed around easily. Basically, the control should expose an object that exposes the state and then you can pass that to your new form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Curtis Justus" <se*****@nospam .ser.it.is.com> wrote in message
news:e%******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi,

I currently have a control that is on a form [FormA] and I want to pass

that
exact instance of the control to another form [FormB] (a child form that
appears on a button click). The control has state, etc. that works with
both forms.

In my button click on FormA, I take my control and pass it to a

constructor
in FormB. Here is the code in the constructor:

public FormB(MyControl ctrl)
{
InitializeCompo nent();
_passedInContro l = ctrl; /* used later to set the parent property back to the original */
_oldParent = ctrl.Parent; /* used later to set the parent property back to the original */
ctrl.Parent = this;
this.Controls.A dd(ctrl);
}

I also have in the Form Closing event:

private void FormB_Closing(o bject sender, CancelEventArgs e)
{
if (this._passedIn Control != null)
_passedInContro l.Parent = _oldParent;
}

Here is my problem: my control doesn't appear on FormB. It disappears

from
FormA (which should happen because the parent is changing) and it also
reappears on FormA after I close FormB. What do I need to do to get it to appear on FormB?

Thanks in advance,
cj


Nov 15 '05 #3

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

Similar topics

4
4870
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a parameter. For example, FormOptions formOptions = new FormOptions(); if (formOptions.ShowDialog(this) == DialogResult.OK) {
1
3636
by: Maria | last post by:
Hello! I am new to Crystal reports an I have problems passing parameters form outside to Crystal report an creating a report with data from more than one table This is the problem: I have to make a report( VS.NET, C#, Web Form) with 3 parts and with data from three tables: 1st part: all the field values form table1 coresponding to an Id (Id
9
1759
by: Peter | last post by:
I have an Usercontrol on a WebForm, on this Usercontrol is a button, I want to save data that's residing on the WebForm when user clicks on the button that's on the UserControl. How do I call a method that's in the WebForm from the button_click event or from any other event that originate in the UserControl? Peter -- Thanks
1
13719
by: Josh | last post by:
Hi Guys, I have been having a big problem with trying to pass parameters into a user control when the user control is dynamically loaded into a placholder. I am developing in c#. I have get and set methods on the user control "editButton.ascx" which work fine. How do i pass parameter into the user controls "c1", "c2" ? Here is a bit of my code that is calling the user control from the
1
2219
by: billy | last post by:
Ok, here's the situation... I have a user control that contains two textboxes (one for a from date/time and one for a to date/time) and two image buttons. The user control itself is supposed to be used on a number of different ASPX pages (that's why I made it a ASCX). When clicking the image button, I run simple JavaScript to open another popup ASPX page that contains a calendar control and two dropdownboxes for the time (hours :...
41
4317
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
2
4186
by: Benedict | last post by:
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()
3
3039
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message"> <SelectParameters> <asp:ProfileParameter Name="user" PropertyName="UserName"
9
1569
by: Chris | last post by:
Ok, so I have this sub I wrote, and I create a new instance of a UserControl: ctrlAPs tempctrl = new ctrlAPs(); Now, I would like to be able to use this sub I wrote for more than one UserControl, so I was trying to do something like this: private void somesub(UserControl sourcectrl) {
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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
9530
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
9363
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
9312
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,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
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.