
Since your user control is (theoretically, at least) a reusable
component, it shouldn't contain a direct reference to MainForm.
The correct way to do this is using events. Your user control should
expose either one or two events depending upon what the button presses
mean. I'll describe the one-event case first.
1. If the two buttons represent two different answers to the same
question, then your user control should expose a property and an event.
The event should be a simple event that just takes System.EventArgs,
and just indicates that "the user responded". You could call the event:
public System.EventHandler UserReponded;
You would then have a property to indicate how the user responded:
public bool UserReponse
{
get { return this._response; }
}
Each button in your user control would then set the _response flag and
raise a UserResponded event.
Your MainForm would then subscribe to the UserResponded event. The
event handler in the MainForm would then read the UserResponse from the
relevant user control and set the text box contents appropriately.
However, there's another possibility, and that's a scenario in which
the two buttons represent completely different actions that the user
wants to take, not really two answers to the same question. A nicer
design in that case is to have your UserControl expose two events:
public System.EventHandler UserCanceled;
public System.EventHandler UserChangedBackgroundColor;
or some such thing. Your MainForm would then subscribe to both events
and do the right thing in each case.
For your simple example: setting the text box contents, I would go for
the first option: an event and a property. However, as I mentioned, it
doesn't fit all problems.
The basic idea here is to hide the structure of the user control. Say,
for example, the two buttons are two different answers to the same
question. If tomorrow you decide on a different design, and want to
show the user a radio button pair and a button to press, then what? If
you expose events like Button1Clicked and Button2Clicked then you have
to fix your MainForm, too. If, on the other hand, you expose events and
properties based on what the user control is _for_, rather than how it
is _built_, then you shouldn't have to change anything on the outside
if you change the way the user control looks.
As well, because it's the MainForm's responsibility to subscribe to the
user control's events, you can drop as many user controls on a form as
you like, or use the user control on as many forms as you like, and the
user control itself doesn't need to understand the context in which
it's being used. It just notifies "the outside world" when something
happens.