I've created a UserControl
with a DataGridView
. In my code I add this UserControl
to a TabControl-TabPage
on a Form
. This works well.
Now I want to do a double click on the DataGridView
of the UserControl
, select the row as an object and use this data on another TabPage
which is on the initial Form
.
How can I get access to the UserControl
event an my Form
?
I tried it that way(first answer) How to use UserControl but I can't use it on my Form
. So this is not working.
And how to do a DataGridViewCellEvent
to use?
EDIT: I tried the following way now: UserControl:
public partial class ucData : UserControl
{
public ucData(string Number)
{
InitializeComponent();
public string Data
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public event EventHandler DataAvailable;
/// <summary>
/// Called to signal to subscribers that new data is available
/// </summary>
/// <param name="e"></param>
protected virtual void OnDataAvailable(EventArgs e)
{
EventHandler eh = DataAvailable;
if (eh != null)
{
eh(this, e);
}
}
....
...
private void dataGridView_CellDoubleClick(object sender, EventArgs e)
{
OnDataAvailable(null);
}
And on my Main Form:
...
void Test()
{
UserControls.ucData uc = new ucData(null);
uc.DataAvailable += new EventHandler(child_DataAvailable);
uc.Data = "Hello!";
}
void child_DataAvailable(object sender, EventArgs e)
{
UserControls.ucData child = sender as UserControls.ucData;
if (child != null)
{
MessageBox.Show(child.Data);
}
}
...
But if I do a doubleClick on the UserControl no MessageBox will appear. Why? What is wrong? Can anyone help please.