Access form element from another class

  • Thread starter Thread starter SoxFan44
  • Start date Start date
S

SoxFan44

Hi,
I have a DataGridView on my Form1 and a public function that can add a row
to it. I want to be able to access that member function from another class.
I'm confused as to how to do this. Can anyone please help? Thanks!
 
You need to use a delegate for this.

class class Form1 // your main form
{
.......
private void YourDataGridAddingFunction(String YourDataToAdd)
{
// do some work
// add YourDataToAdd to your grid however you want to

} // end YourDataGridAddingFunction ()

} // end class Form1


.......

Then in your other class you need to access YourDataGridAddingFunction ():

public class YourNewClass
{
public delegate void AddRowDataGridDelegate(String dataToAdd);
AddRowDataGridDelegate DelegatedAddRowFunction; // this is your function
you use just as you would if you were back in Form1

private void SomeMethod()
{
// if we need to add something to our data grid:
DelegatedAddRowFunction("Add this row or information");
} // end SomeMethod ()

} // end YourNewClass

Hope this helps. I like to refer to this page which Jon gave to me a few
days ago:
http://www.yoda.arachsys.com/csharp/events.html

Rob K
 

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