473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Delegate / Multi-User Control Question

Hi,

I have a form (Form1) that contains a tab control which one tab has a
customer user control (UserControl1). When the user double-clicks on the
grid hosted there a new user control is created. I have a delegate / event
that passes via EventArgs the appropriate info from the grid to the
UserControl2. This works fine, except for now I'm a little lost on the
final step....I need that once the UserControl2 is "filled out" that it is
then added (and then dispose of it) to the tab control being hosted on
Form1.

I hope this isn't that confusing, but I'm not sure if I need to have a
property that is set on Form1, or a static method on Form1 that takes
UserControl2 as a type and addes it. I just need a little guidance / help.
Thanks in advance.

Doug
Jan 19 '06 #1
5 2086
Doug,

Can you be a little more clear? I would think that your grid (or user
control in the grid) exposes the event indicating that the control is
created. The tab control then picks this up (or the host of the tab
control) and then you just add another tab with the control.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:OX****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a form (Form1) that contains a tab control which one tab has a
customer user control (UserControl1). When the user double-clicks on the
grid hosted there a new user control is created. I have a delegate /
event
that passes via EventArgs the appropriate info from the grid to the
UserControl2. This works fine, except for now I'm a little lost on the
final step....I need that once the UserControl2 is "filled out" that it is
then added (and then dispose of it) to the tab control being hosted on
Form1.

I hope this isn't that confusing, but I'm not sure if I need to have a
property that is set on Form1, or a static method on Form1 that takes
UserControl2 as a type and addes it. I just need a little guidance /
help.
Thanks in advance.

Doug

Jan 19 '06 #2
Nicholas,

Let me try and be clearer (sorry) - I may have already figured it out so let
me try -

Basically there are 3 user controls that I'm dealing w/. The first one,
UserControl1 is a grid that has an event that is fired when the user
doubleclicks on a row. The "info" from that event is passed via a Delegate
/ Event to UserControl2. UserControl2 does the data retrieval from the db
based on the "info" provided by UserControl1 (e.g. Primary Key).
UserControl2 then populates the data from the db into itself (it's full of
textboxes and labels, etc). Now, here's where my problem comes in,
UserControl3 is the "hosting" control (i.e. Tab control) that has as one of
its controls on Tab1 UserControl1. What I'm trying to do is add
UserControl2 to UserControl3 Tab2 once it's finished loading the data from
the db.

I believe i've solved it (so let me know if you think this is the right
approach) by using a delegate in UserControl3 and a static method that
instantiates an instance of UserControl2 passing it the "info" to do the db
lookup. In UserControl1, in the event that handles the double-click i
created an instance of the Delegate from UserControl3. With this, it
appears that UserControl3 can now add an instance of UserControl2 to the
appropriate tab w/ the correct data.

Gosh, i hope this makes sense!!! Please let me know...thanks again for the
help in advance.

Doug

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ow**************@TK2MSFTNGP12.phx.gbl...
Doug,

Can you be a little more clear? I would think that your grid (or user
control in the grid) exposes the event indicating that the control is
created. The tab control then picks this up (or the host of the tab
control) and then you just add another tab with the control.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:OX****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a form (Form1) that contains a tab control which one tab has a
customer user control (UserControl1). When the user double-clicks on the
grid hosted there a new user control is created. I have a delegate /
event
that passes via EventArgs the appropriate info from the grid to the
UserControl2. This works fine, except for now I'm a little lost on the
final step....I need that once the UserControl2 is "filled out" that it
is
then added (and then dispose of it) to the tab control being hosted on
Form1.

I hope this isn't that confusing, but I'm not sure if I need to have a
property that is set on Form1, or a static method on Form1 that takes
UserControl2 as a type and addes it. I just need a little guidance /
help.
Thanks in advance.

Doug


Jan 19 '06 #3
Nicolas,

Ok, ran into a problem w/ my approach, the issue is that the tab control is
defined as non-static and I'm referencing it w/i a static method - which
obviously isn't really well liked :( So, i'm a little lost as to my next
steps.

Doug
"Doug Handler" <dk*******@yahoo.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...
Nicholas,

Let me try and be clearer (sorry) - I may have already figured it out so
let me try -

Basically there are 3 user controls that I'm dealing w/. The first one,
UserControl1 is a grid that has an event that is fired when the user
doubleclicks on a row. The "info" from that event is passed via a
Delegate / Event to UserControl2. UserControl2 does the data retrieval
from the db based on the "info" provided by UserControl1 (e.g. Primary
Key). UserControl2 then populates the data from the db into itself (it's
full of textboxes and labels, etc). Now, here's where my problem comes
in, UserControl3 is the "hosting" control (i.e. Tab control) that has as
one of its controls on Tab1 UserControl1. What I'm trying to do is add
UserControl2 to UserControl3 Tab2 once it's finished loading the data
from the db.

I believe i've solved it (so let me know if you think this is the right
approach) by using a delegate in UserControl3 and a static method that
instantiates an instance of UserControl2 passing it the "info" to do the
db lookup. In UserControl1, in the event that handles the double-click i
created an instance of the Delegate from UserControl3. With this, it
appears that UserControl3 can now add an instance of UserControl2 to the
appropriate tab w/ the correct data.

Gosh, i hope this makes sense!!! Please let me know...thanks again for
the help in advance.

Doug

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Ow**************@TK2MSFTNGP12.phx.gbl...
Doug,

Can you be a little more clear? I would think that your grid (or user
control in the grid) exposes the event indicating that the control is
created. The tab control then picks this up (or the host of the tab
control) and then you just add another tab with the control.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:OX****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a form (Form1) that contains a tab control which one tab has a
customer user control (UserControl1). When the user double-clicks on
the
grid hosted there a new user control is created. I have a delegate /
event
that passes via EventArgs the appropriate info from the grid to the
UserControl2. This works fine, except for now I'm a little lost on the
final step....I need that once the UserControl2 is "filled out" that it
is
then added (and then dispose of it) to the tab control being hosted on
Form1.

I hope this isn't that confusing, but I'm not sure if I need to have a
property that is set on Form1, or a static method on Form1 that takes
UserControl2 as a type and addes it. I just need a little guidance /
help.
Thanks in advance.

Doug



Jan 19 '06 #4
How close is this?

1. Add 3 UserControls (MyTabControl, MyGridControl, MyDataControl)
2. MyGridControl has a single Button
3. MyDataControl has a single Label
4. MyTabControl has a TabControl with two TabPages. TabPage1 has an instance
of MyGridControl. TabPage2 has an instance of MyDataControl with visibility
= false.
5. Define two delegates:

public delegate void DataSelectedEventHandler(string item);
public delegate void DataLoadedEventHandler();

6. In MyGridControl, create an event:

public event DataSelectedEventHandler DataSelected;

When the Button is clicked, fire the event and pass a string as data:

private void OnDataSelected(string item)
{
if ( DataSelected != null ) DataSelected(item);
}

private void button1_Click(...)
{
OnDataSelected(DateTime.Now.Second.ToString());
}

7. In MyDataControl, create an event:

public event DataLoadedEventHandler DataLoaded;

Create a LoadData method, that does its work, then fires the DataLoaded
event:

private void OnDataLoaded()
{
if ( DataLoaded != null ) DataLoaded();
}

public void LoadData(string item)
{
// database access here
label1.Text = item;
DataLoaded();
}

8. In MyTabControl, handle each event:

private void myGridControl1_DataSelected(string item)
{
myDataControl1.LoadData(item);
}

private void myDataControl1_DataLoaded()
{
myDataControl1.Visible = true;
tabControl1.SelectTab(1);
}

I believe it's conceptually doing what you need, except that an instance of
MyDataControl ("UserControl2") is not created and added to TabPage2 when the
button in MyGridControl is selected, the button click simulating selecting
an item from the grid.

If you need to create new TabPages (and new instances of MyDataControl, or
"UserControl2") each time an item from the grid is selected, then something
like this I suppose:

private void myGridControl1_DataSelected(string item)
{
MyDataControl ctrl = new MyDataControl();
ctrl.DataLoaded += new
DataLoadedEventHandler(myDataControl1_DataLoaded);
ctrl.LoadData(item);

AddTabPage(ctrl);
}

private void AddTabPage(Control ctrl)
{
tabControl1.TabPages.Add("some name");
tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(ctrl);
}

private void myDataControl1_DataLoaded()
{
tabControl1.SelectTab(tabControl1.TabPages.Count -1 );
}

Ron

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:u7**************@TK2MSFTNGP14.phx.gbl...
Nicolas,

Ok, ran into a problem w/ my approach, the issue is that the tab control
is defined as non-static and I'm referencing it w/i a static method -
which obviously isn't really well liked :( So, i'm a little lost as to my
next steps.

Doug
"Doug Handler" <dk*******@yahoo.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...
Nicholas,

Let me try and be clearer (sorry) - I may have already figured it out so
let me try -

Basically there are 3 user controls that I'm dealing w/. The first one,
UserControl1 is a grid that has an event that is fired when the user
doubleclicks on a row. The "info" from that event is passed via a
Delegate / Event to UserControl2. UserControl2 does the data retrieval
from the db based on the "info" provided by UserControl1 (e.g. Primary
Key). UserControl2 then populates the data from the db into itself (it's
full of textboxes and labels, etc). Now, here's where my problem comes
in, UserControl3 is the "hosting" control (i.e. Tab control) that has as
one of its controls on Tab1 UserControl1. What I'm trying to do is add
UserControl2 to UserControl3 Tab2 once it's finished loading the data
from the db.

I believe i've solved it (so let me know if you think this is the right
approach) by using a delegate in UserControl3 and a static method that
instantiates an instance of UserControl2 passing it the "info" to do the
db lookup. In UserControl1, in the event that handles the double-click i
created an instance of the Delegate from UserControl3. With this, it
appears that UserControl3 can now add an instance of UserControl2 to the
appropriate tab w/ the correct data.

Gosh, i hope this makes sense!!! Please let me know...thanks again for
the help in advance.

Doug

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Ow**************@TK2MSFTNGP12.phx.gbl...
Doug,

Can you be a little more clear? I would think that your grid (or
user control in the grid) exposes the event indicating that the control
is created. The tab control then picks this up (or the host of the tab
control) and then you just add another tab with the control.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:OX****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a form (Form1) that contains a tab control which one tab has a
customer user control (UserControl1). When the user double-clicks on
the
grid hosted there a new user control is created. I have a delegate /
event
that passes via EventArgs the appropriate info from the grid to the
UserControl2. This works fine, except for now I'm a little lost on the
final step....I need that once the UserControl2 is "filled out" that it
is
then added (and then dispose of it) to the tab control being hosted on
Form1.

I hope this isn't that confusing, but I'm not sure if I need to have a
property that is set on Form1, or a static method on Form1 that takes
UserControl2 as a type and addes it. I just need a little guidance /
help.
Thanks in advance.

Doug



Jan 19 '06 #5
Ron,

Thanks....i'm looking through your stuff. I believe i may have solved it so
i'm curious if it maps to what you presented. Just in case you care, the
solution that i'm examing right now that seems to be working is that in my
UserControl3's static method that is called via the delegate, I pass in a
reference to the tabcontrol. Here is the pseudo-code that currently
"working."

//UserControl1
public delegate void DetailSelectionHandler(object sender, PersonEventArgs
args);

private void grdDetails_DoubleClick(..)
{
PersonEventArgs args = new PersonEventArgs(Get.....);
UserControl3.OnDetailSelected getDetails = new
UserControls3.OnDetailSelected(UserControl3.GetDet ailInfo);
getDetails(this.m_TabControl, args);
}

//UserControl2
#region Constructor
public UserControl2(Person person)
{
this.m_Person = person;
RetrieveDetailInfo(person);
}
#endregion

//UserControl3
public delegate UserControl2 OnDetailSelected(object sender, PersonEventArgs
args);

public static bool GetDetailInfo(object sender, PersonEventArgs args)
{
bool bSuccess = true;

while(bSuccess)
{
TabControl tabControls = sender as TabControl;
tabControl.Tabs[4].TabPage.Controls.Clear(); // insure nothing is
there

UserControl2 uc2 = new UserControl2(args.Person); // create an
instance of UserControl2 and pass it the "info" that was selected in
UserControl1
tabControl.Tabs[4].TabPage.Controls.Add(uc2); // Add instance to
the specific tab page
tabControl.Tabs[4].Selected = true; // Sets the tab page
active
}
return bSuccess; // added for later logic use

}
"RYoung" <so*****@x.com> wrote in message
news:Oi**************@TK2MSFTNGP14.phx.gbl...
How close is this?

1. Add 3 UserControls (MyTabControl, MyGridControl, MyDataControl)
2. MyGridControl has a single Button
3. MyDataControl has a single Label
4. MyTabControl has a TabControl with two TabPages. TabPage1 has an
instance of MyGridControl. TabPage2 has an instance of MyDataControl with
visibility = false.
5. Define two delegates:

public delegate void DataSelectedEventHandler(string item);
public delegate void DataLoadedEventHandler();

6. In MyGridControl, create an event:

public event DataSelectedEventHandler DataSelected;

When the Button is clicked, fire the event and pass a string as data:

private void OnDataSelected(string item)
{
if ( DataSelected != null ) DataSelected(item);
}

private void button1_Click(...)
{
OnDataSelected(DateTime.Now.Second.ToString());
}

7. In MyDataControl, create an event:

public event DataLoadedEventHandler DataLoaded;

Create a LoadData method, that does its work, then fires the DataLoaded
event:

private void OnDataLoaded()
{
if ( DataLoaded != null ) DataLoaded();
}

public void LoadData(string item)
{
// database access here
label1.Text = item;
DataLoaded();
}

8. In MyTabControl, handle each event:

private void myGridControl1_DataSelected(string item)
{
myDataControl1.LoadData(item);
}

private void myDataControl1_DataLoaded()
{
myDataControl1.Visible = true;
tabControl1.SelectTab(1);
}

I believe it's conceptually doing what you need, except that an instance
of MyDataControl ("UserControl2") is not created and added to TabPage2
when the button in MyGridControl is selected, the button click simulating
selecting an item from the grid.

If you need to create new TabPages (and new instances of MyDataControl, or
"UserControl2") each time an item from the grid is selected, then
something like this I suppose:

private void myGridControl1_DataSelected(string item)
{
MyDataControl ctrl = new MyDataControl();
ctrl.DataLoaded += new
DataLoadedEventHandler(myDataControl1_DataLoaded);
ctrl.LoadData(item);

AddTabPage(ctrl);
}

private void AddTabPage(Control ctrl)
{
tabControl1.TabPages.Add("some name");
tabControl1.TabPages[tabControl1.TabPages.Count -
1].Controls.Add(ctrl);
}

private void myDataControl1_DataLoaded()
{
tabControl1.SelectTab(tabControl1.TabPages.Count -1 );
}

Ron

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:u7**************@TK2MSFTNGP14.phx.gbl...
Nicolas,

Ok, ran into a problem w/ my approach, the issue is that the tab control
is defined as non-static and I'm referencing it w/i a static method -
which obviously isn't really well liked :( So, i'm a little lost as to
my next steps.

Doug
"Doug Handler" <dk*******@yahoo.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...
Nicholas,

Let me try and be clearer (sorry) - I may have already figured it out so
let me try -

Basically there are 3 user controls that I'm dealing w/. The first one,
UserControl1 is a grid that has an event that is fired when the user
doubleclicks on a row. The "info" from that event is passed via a
Delegate / Event to UserControl2. UserControl2 does the data retrieval
from the db based on the "info" provided by UserControl1 (e.g. Primary
Key). UserControl2 then populates the data from the db into itself (it's
full of textboxes and labels, etc). Now, here's where my problem comes
in, UserControl3 is the "hosting" control (i.e. Tab control) that has as
one of its controls on Tab1 UserControl1. What I'm trying to do is add
UserControl2 to UserControl3 Tab2 once it's finished loading the data
from the db.

I believe i've solved it (so let me know if you think this is the right
approach) by using a delegate in UserControl3 and a static method that
instantiates an instance of UserControl2 passing it the "info" to do the
db lookup. In UserControl1, in the event that handles the double-click
i created an instance of the Delegate from UserControl3. With this, it
appears that UserControl3 can now add an instance of UserControl2 to the
appropriate tab w/ the correct data.

Gosh, i hope this makes sense!!! Please let me know...thanks again for
the help in advance.

Doug

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Ow**************@TK2MSFTNGP12.phx.gbl...
Doug,

Can you be a little more clear? I would think that your grid (or
user control in the grid) exposes the event indicating that the control
is created. The tab control then picks this up (or the host of the tab
control) and then you just add another tab with the control.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:OX****************@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> I have a form (Form1) that contains a tab control which one tab has a
> customer user control (UserControl1). When the user double-clicks on
> the
> grid hosted there a new user control is created. I have a delegate /
> event
> that passes via EventArgs the appropriate info from the grid to the
> UserControl2. This works fine, except for now I'm a little lost on
> the
> final step....I need that once the UserControl2 is "filled out" that
> it is
> then added (and then dispose of it) to the tab control being hosted on
> Form1.
>
> I hope this isn't that confusing, but I'm not sure if I need to have a
> property that is set on Form1, or a static method on Form1 that takes
> UserControl2 as a type and addes it. I just need a little guidance /
> help.
> Thanks in advance.
>
> Doug
>
>



Jan 19 '06 #6

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

Similar topics

6
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I...
3
by: Phill | last post by:
When should you use an interface over a delegate? Like if you have a class that needs to sort various types of objects. Should it use a delegate to do the sorting or require that the objects...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
4
by: Paul Tomlinson | last post by:
OK, I have a multi-threaded application. I have a master thread which calls a delegate which I want the child thread to execute but that is not happening, instead the master thread is running the...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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...

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.