Home >Backend Development >C++ >How Can I Change a Control on One Windows Form from Another?

How Can I Change a Control on One Windows Form from Another?

Susan Sarandon
Susan SarandonOriginal
2025-01-31 07:41:11276browse

How Can I Change a Control on One Windows Form from Another?

Windows Forms Window Interaction: How to change the control of another window from one window

When using multiple windows in Windows Forms applications, you may need to operate controls on different windows. Here are some methods to implement this task:

The control on the window B from the window A:

Construct function parameters:
  • Define a constructor in the window B, which accepts the appropriate parameter of the value you want to set.

    When creating an instance of the window B from the window A, the value required by constructing a function can be passed by constructing a function.
    • Store the parameter in the member field of the window B for future use.
    • Public attribute/method:
  • Create public attributes or methods in window B to allow you to operate specific controls. After creating an instance of the window B, use these attributes/methods to set or retrieve the value.

    • Public control:
  • The controls to be operated in the window B are set to public control.

    This allows you to use its name to access the control directly from the window A (similar to visiting any other public members).

    • The control on the window A from the window B:
Transfer instance reference:

Create a public method or attribute in the window A, which accepts the window instance as a parameter.
  • The instance of the window A is passed to the window B through this parameter.

    In the window B, use the instance of the pass to operate the control on the window A.

    • Event:
    • Create an event in the window B, which triggers the incident when you need to operate the window A.
  • Subscribe this incident from the window A and put the relevant code in the event processing program.
  • The incident in the window B is caused by the operation required for the execution of the window A.

    • commission:
    • Define a Action type or other commissioned public attributes in the window B.
    After creating an instance of the window B from the window A, the attribute is allocated using a custom operation.
  • In the window B, call the operation to operate the window A.

      Example:
    • Construct function parameters:
    Public attributes:

Public control:

In the designer of the window B, set the "modifier" property of the control to "public", and ensure that "generators" are true.

These are just several ways to communicate between the windows. The best way depends on the specific needs of the application.
<code class="language-csharp">public Form2(int value)
{
    // ...
    this.comboBox1.SelectedValue = value;
}</code>

The above is the detailed content of How Can I Change a Control on One Windows Form from Another?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn