Connecting Tech Pros Worldwide Forums | Help | Site Map

Delegate / Multi-User Control Question

Doug Handler
Guest
 
Posts: n/a
#1: Jan 19 '06
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



Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Jan 19 '06

re: Delegate / Multi-User Control Question


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]
- mvp@spam.guard.caspershouse.com

"Doug Handler" <dkhandler@yahoo.com> wrote in message
news:OX%23FTPPHGHA.1124@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>
>[/color]


Doug Handler
Guest
 
Posts: n/a
#3: Jan 19 '06

re: Delegate / Multi-User Control Question


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]" <mvp@spam.guard.caspershouse.com> wrote in
message news:Owfk60PHGHA.1424@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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]
> - mvp@spam.guard.caspershouse.com
>
> "Doug Handler" <dkhandler@yahoo.com> wrote in message
> news:OX%23FTPPHGHA.1124@TK2MSFTNGP10.phx.gbl...[color=green]
>> 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
>>
>>[/color]
>
>[/color]


Doug Handler
Guest
 
Posts: n/a
#4: Jan 19 '06

re: Delegate / Multi-User Control Question


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" <dkhandler@yahoo.com> wrote in message
news:uFcvS$PHGHA.3056@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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]" <mvp@spam.guard.caspershouse.com> wrote
> in message news:Owfk60PHGHA.1424@TK2MSFTNGP12.phx.gbl...[color=green]
>> 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]
>> - mvp@spam.guard.caspershouse.com
>>
>> "Doug Handler" <dkhandler@yahoo.com> wrote in message
>> news:OX%23FTPPHGHA.1124@TK2MSFTNGP10.phx.gbl...[color=darkred]
>>> 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
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


RYoung
Guest
 
Posts: n/a
#5: Jan 19 '06

re: Delegate / Multi-User Control Question


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" <dkhandler@yahoo.com> wrote in message
news:u7RGsLQHGHA.2440@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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" <dkhandler@yahoo.com> wrote in message
> news:uFcvS$PHGHA.3056@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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]" <mvp@spam.guard.caspershouse.com> wrote
>> in message news:Owfk60PHGHA.1424@TK2MSFTNGP12.phx.gbl...[color=darkred]
>>> 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]
>>> - mvp@spam.guard.caspershouse.com
>>>
>>> "Doug Handler" <dkhandler@yahoo.com> wrote in message
>>> news:OX%23FTPPHGHA.1124@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
>>>>
>>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Doug Handler
Guest
 
Posts: n/a
#6: Jan 19 '06

re: Delegate / Multi-User Control Question


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" <someone@x.com> wrote in message
news:Oi9U3tQHGHA.2036@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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" <dkhandler@yahoo.com> wrote in message
> news:u7RGsLQHGHA.2440@TK2MSFTNGP14.phx.gbl...[color=green]
>> 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" <dkhandler@yahoo.com> wrote in message
>> news:uFcvS$PHGHA.3056@TK2MSFTNGP09.phx.gbl...[color=darkred]
>>> 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]" <mvp@spam.guard.caspershouse.com> wrote
>>> in message news:Owfk60PHGHA.1424@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]
>>>> - mvp@spam.guard.caspershouse.com
>>>>
>>>> "Doug Handler" <dkhandler@yahoo.com> wrote in message
>>>> news:OX%23FTPPHGHA.1124@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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Closed Thread