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