There is a much easier way than that jo0, and I thought the point here was to use an external class.
First, highlight the 10 buttons you have on your form, open the events pane, and double-click the 'Click' action. This will link every highlighted control's click event to one central click handler (button_click in the code below).
Now highlight the textbox in the designer, and in the properties pane under Design you'll find the Modifiers property. This controls the level of access to the selected control. Currently it is only accessible by the form on which it is placed (private), but seeing as we want to use it from an external class we will need to set this to public.
Now add a new class to your form (Class1 in the code below).
Form1.cs
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Class1 class1;
public Form1()
{
InitializeComponent();
this.class1 = new Class1(this);
}
private void button_Click(object sender, EventArgs e)
{
Button btn = (sender as Button);
class1.SetText(btn.Text);
}
}
}
Class1.cs
Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
public class Class1
{
private Form1 form1;
public Class1(Form1 form1)
{
this.form1 = form1;
}
public void SetText(string text)
{
if (form1.textBox1.Text == "0.")
{
form1.textBox1.Text = text;
}
else
{
form1.textBox1.Text += text;
}
}
}
}
Note that a Form1 instance is passed in to class1, who then stores it away for later to access the textbox. (Note that if you hadn't changed the textbox's modifier, the textbox would have not appeared in the intellisense list, and you would have also recieved a compile-time error if you had still tryed to use it).
You have to understand that because Form1 is a reference type (an object) by passing it in as a parameter a new copy of it is not made, but a
reference. Meaning Class1's form1 is a pointer to the actual form. So even once you've set Class1's form1, If you make any changes to Form1 it'll all happen in Class1's form1 too (because Class1 doesn't really have a form1, just a pointer to one). In memory, the reference will be stored on the stack, whilst the actual data (the form1) will be on the heap/free store.
The exception to this is value types (like int, short, long, etc), which when passed in will be copied, meaning if you did pass in an integer from Form1 to Class1, changing it in Form1 would not have any effect on the one passed in to Class1. Value types are stored completely on the stack.
If you do for some reason need to keep the same instance of a value type, you can add the ref keyword: 'public void Hello(ref int num)'. This will mean any changes made in the Hello function to num, will be reflected in Form1's num (assuming thats where you called the function). Note that attaching ref to a reference type has no effect.