Reputation: 401
So I have the following code:
public partial class Buttons : Form
{
private DataSet AllEventData = new DataSet("AllEventData");
private DataTable makeButtonsTable()
{
DataTable buttonData = AllEventData.Tables.Add("ButtonData");
DataColumn column1, column2, column3;
column1 = new DataColumn();
column1.DataType = Type.GetType("System.Int32");
column1.ColumnName = "ID";
column1.AutoIncrement = true;
column1.AutoIncrementSeed = 1;
column1.AutoIncrementStep = 1;
buttonData.Columns.Add(column1);
column2 = new DataColumn("Button Name", typeof(String));
buttonData.Columns.Add(column2);
column3 = new DataColumn("Button Location", typeof(Rectangle));
buttonData.Columns.Add(column3);
buttonData.PrimaryKey = new DataColumn[] { column1 };
return buttonData;
}
public Buttons()
{
InitializeComponent();
DataTable buttonData = makeButtonsTable();
buttonGridView.DataSource = buttonData;
buttonGridView.Columns["ID"].Visible = false;
}
private void LaunchScreenSelection_Click(object sender, EventArgs e)
{
ss = new ScreenSelection(buttonData);
ss.Show(this);
}
}
My ultimate goal is to have information entered on the ScreenSelection form add data to the datagridview, which I assume means adding it to the datatable but I don't know, clearly.
When I try to access buttonData from LaunchScreenSelection_Click, it of course tells me that buttonData doesn't exist in the current context. Which, yeah, I kinda get that... but given everything I'm constructing here, I have no idea how to make it exist in the current context. Everything I've tried just gives more errors (I'll be happy to list all the attempts here, but... that'll get lengthy).
Any help would be appreciated.
Upvotes: 1
Views: 54
Reputation: 216342
The correct way to handle this scenario is through custom events.
Let's start defining a class that we will use to transfer the information between the ScreenLocation form and the Buttons form. This class should be public and visible to both forms class (same namespace), it could be in its own file or just appended to the ScreenLocation/Buttons form
public class ButtonData
{
public int ID { get; set; }
public string Name { get; set; }
public Rectangle Rect { get; set; }
}
Now we will add the boiler plate code required to define an event raised by the ScreenLocation form
public class ScreenSelection : Form
{
public delegate void onDataReady(ButtonData data);
public event onDataReady DataReady;
....
}
At this point we could change the ScreenLocation class adding the code that raises the event when the data is ready to be transmitted to anyone listening to the event.
For example a ButtonClick handler inside the ScreenLocation could be written in this way
protected void ButtonSave_Click(object sender, EventArgs e)
{
// Anyone has subscribed to the event?
if(DataReady != null)
{
ButtonData btn = new ButtonData();
// Change these GetXXXXValue with the appropriate code
// that extracts the info from the ScreenLocation UI.
btn.ID = GetTheIDValue();
btn.Name = GetTheNameValue();
btn.Rect = GetTheRectValue();
// Send the info to the interested parties.
DataReady(btn);
}
}
The circle is closed when you create the instance of the ScreenLocation in your code from the Buttons form.
private void LaunchScreenSelection_Click(object sender, EventArgs e)
{
ss = new ScreenSelection(buttonData);
// Tell the ScreenLocation ss instance that we are
// interested to know when new data is ready
ss.DataReady += myDataLoader;
ss.Show(this);
}
// When the *ScreenLocation* instance will raise the event,
// we will be called here to handle the event
private void myDataLoader(ButtonData btn)
{
// Now you have your info from the ScreenLocation instance
// and you can add it to the datatable used as datasource for the grid
DataTable dt = AllEventData.Tables["AllEventData"];
dt.Rows.Add(btn.ID, btn.Name, btn.Rect);
}
Upvotes: 1
Reputation: 125
If I understand correctly, the variable buttonData only exists locally within your makeButtonsTable() function. However, you do have your global variable AllEventData that has a reference to the "ButtonData" table since you added that here.
DataTable buttonData = AllEventData.Tables.Add("ButtonData");
Therefore, you need only send that table from AllEventData as your parameter for ScreenSelection.
private void LaunchScreenSelection_Click(object sender, EventArgs e)
{
ss = new ScreenSelection(AllEventData.Tables["ButtonData"]);
ss.Show(this);
}
Upvotes: 1