I have my form1(Design) with a button in it, but in my form1(Class) it's getting pretty crowded so I want to make a new class. But how do I make the button work in the new class(the new classes name is form2(Class). Of course the function works for the button in form1(Class) so how do I make it work from form2(Class)?
-
Wait, what? Do you mean you're making an entirely new form, or are you trying to move some of the logic for the existing form into a new class? – Matthew Watson Jun 20 '13 at 07:43
-
@Mathew Watson Yes some of the logic from the default class file into a new class file. – John M. Jun 20 '13 at 07:46
5 Answers
Create your own custom UserControl
with own Controls
and logic. Or create partial class
for your From1
and put events logic there.

- 17,312
- 2
- 36
- 54
It would be worth considering separating your UI code (e.g. button event handling) from the core logic of your application.
There are many way of doing this but the common place to start is with a model class that represents the data and/or logic your window is manipulating.
Try searching on "Windows forms mvc" or "windows forms mvp".
MVP : http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
What you should really do is use the Model/View/Controller
or Model/View/Presenter
pattern.
Essentially what you do is to make the Form class (which is the View
class) pretty dumb. It doesn't know what to do when various events (such as button presses) occur. Instead, it raises events to indicate what has happened.
Then you write a Controller
class which is responsible for creating the View
and attaching to its events. The Controller
knows what to do when a button is pressed, and responds accordingly (perhaps by calling methods or setting properties in the View
class).
The Model
is just an abstraction of the data used by the Controller
to populate the View
class.
Ideally, the View
knows nothing about the Model
. The Controller
is the entity that is responsible for sitting between the Model
and the View
.
This approach lets you split the business logic out from the form, which simplifies things and makes it much easier to change things and also to unit test.
I posted an example about this a while ago here: https://stackoverflow.com/a/15605436/106159

- 1
- 1

- 104,400
- 10
- 158
- 276
You should try to disentangle the UI logic from the "business logic", moving it out of the event and in a new class.
e.g. if now you have:
// in Form1.cs
private void btn_Click(object sender, System.EventArgs e) {
OpenDatabaseConnection();
string customerName=SearchCustomerByCode(someTextBox.Text);
someOtherTextBox.Text=customerName;
CloseDatabaseConnection();
}
you should have something like this instead:
// in Form1.cs
private void btn_Click(object sender, System.EventArgs e)
{
string customerCode=someTextBox.Text;
var cs = new CustomerRepository();
string customerName=cs.SearchCustomerByCode(customerCode);
someOtherTextBox.Text=customerName;
}
// in CustomerRepository.cs
//... logic to get the data from the DB, no specific knowledge of form1
public class CustomerRepository.cs
{
public string SearchCustomerByCode(string customerCode)
{
//...
}
}

- 24,914
- 3
- 72
- 86
You could do something like this, which would work, but not very well.
Form 1
public partial class Form1 : Form
{
Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
f2.ShowDialog();
}
}
Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.button1_Click(null, null);
}
}

- 6,313
- 15
- 32
- 40

- 4,862
- 7
- 29
- 42