Referencing controls on a different form

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

In Windows (not Web) forms, is there a better way to refer to controls on
another form than the following:

ComboBox cmbMyCombo = (ComboBox)this.Owner.Controls[14];
cmbMyCombo.Items.Add(txtMyTextBox.Text);

In the above example, a "main" form opens a dialog form with a textbox on
it. The user enters some text in the textbox and then clicks a button which
adds the contents of the textbox to the combo box on the main form.

This works, but it is annoying not to be able to reference the combo box by
its name, or am I missing something here?

Any assistance gratefully received.

Best regards,

Mark Rae
 
Hi Mark,
It is not like you can't do that, but I think it is not good design and
practice to reference controls that don't belong to the form. The bether
approach would be, I believe, to fire an event and let the form that owns
the controls to update it or after closing the dialog the form opened the
dialog can read the dialog properties and update its own controls.
 
It is not like you can't do that,

Hmm - wanna tell me how to do it, then...?
but I think it is not good design and practice to reference controls that
don't belong to the form.

Why?
The bether approach would be, I believe, to fire an event and let the form that owns
the controls to update it or after closing the dialog the form opened the
dialog can read the dialog properties and update its own controls.

Why would this be better? Are you really saying that, if a Windows
application has more than one form, it's not a "good approach" for those
forms to communicate directly one with another?
 
Hi,
Why would this be better?

Because the two forms are separate objects. If you do so you tightly couple
the objects. Something that OOD is trying to avoid. However, if you are ok
with that go ahead and expose the controls as a public members or provide
public properties for them. If you want to expose the references to the
control publicly select the control in the designer and in the property
window change Modifiers property to *public*.
If you are looking for method that returns a control by its name you won't
find any. Controls don't have to have "Name" property set.
Are you really saying that, if a Windows
application has more than one form, it's not a "good approach" for those
forms to communicate directly one with another?

No, they can communicate. But that communication has to be made in a way
that the forms don't show any knowledge about each other's internal
structure.

However, it's your call as a disigner.
 
No, they can communicate. But that communication has to be made in a way
that the forms don't show any knowledge about each other's internal
structure.

Again, why?
 
"This works, but it is annoying not to be able to reference the combo box by
its name, or am I missing something here?"

What you really miss Mark is called OOD or more specifically Encapsulation.
Read more about those and the ask why.
 
Back
Top