471,305 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 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 1981
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ondrej Sevecek | last post: by
3 posts views Thread by Phill | last post: by
7 posts views Thread by Ant | last post: by
6 posts views Thread by David Veeneman | last post: by
11 posts views Thread by matsi.inc | last post: by
26 posts views Thread by raylopez99 | last post: by
10 posts views Thread by vcquestions | last post: by
reply views Thread by rosydwin | last post: by

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.