Delegate / Multi-User Control Question

  • Thread starter Thread starter Doug Handler
  • Start date Start date
D

Doug Handler

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
 
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,

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 said:
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]
- (e-mail address removed)

Doug Handler said:
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
 
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 said:
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 said:
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]
- (e-mail address removed)

Doug Handler said:
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
 
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 said:
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 said:
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 said:
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]
- (e-mail address removed)

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
 
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.GetDetailInfo);
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 said:
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 said:
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 said:
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

in message 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]
- (e-mail address removed)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top