Home > Backend Development > C++ > How to Set a ComboBox's SelectedValue on a Second Form by Selecting a DataGridView Row on the First Form?

How to Set a ComboBox's SelectedValue on a Second Form by Selecting a DataGridView Row on the First Form?

Patricia Arquette
Release: 2025-01-31 07:36:11
Original
407 people have browsed it

How to Set a ComboBox's SelectedValue on a Second Form by Selecting a DataGridView Row on the First Form?

Windows forms windows communication method

Scene:

Windows Forms applications often require data interaction between multiple windows, such as filling controls on another window according to the choice of one window.

Question:

How to set up the selectedValue of the second window by selecting the line in the first window to set the line in the DataGridView to fill the Combobox to fill the Combobox with this value? Solution:

In Windows Forms, the windows are similar to other C#categories, and the communication methods between the windows are the same as the inter -class communication method.

Operation the second window in the first window:

Construct function parameter transfer:

Add necessary parameters to the constructor of the second window. When creating a second window instance, the value is passed to the constructor. Public attributes or methods:

Create public attributes or methods in the second window, and set these attributes after creating a second window instance. In this way, the second window can access these values.
  • Public control: Another method is to set the controls that need to be operated to public access permissions in order to access from other windows. But this is usually not recommended because the maintenance and scalability of the code will be reduced.
  • Operation the first window in the second window:
  • Pass the first window instance: Create a public attribute in the second window to store the instance of the first window. When creating a second window instance, pass the instance of the first window to it. Then you can use this instance to operate the first window.
  • Use event:
Create an event in the second window and subscribe to the event in the first window. When the first window is required, the event is triggered.

Inject it into the Action commission: Define a public attribute of the Action entrusted type in the second window. When creating a second window instance, use custom Action to assign the attribute. When you need to operate the first window, call the Action in the second window.

    The first window control:
  • directly disclose the control of the first window, and pass the instance of the first window to the second window. Then, you can operate the control. But this is not recommended, the reason is above.
  • Example:
  • Operation the second window in the first window
  • Example 1 -The constructor of using the second window:
  • In the first window:
Example 2 -Public attributes of using the second window:

In the first window:

The first window operates from the second window

Example 3 -Transfer the instance of the first window to the second window and use a public method:

When creating FORM2:
<code class="language-csharp">public partial class Form2 : Form
{
    int selectedValue;
    public Form2(int value)
    {
        InitializeComponent();
        selectedValue = value;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        // 加载数据
        this.comboBox1.DataSource = new MyDbContext().Categories.ToList(); // 假设Categories是数据库表
        this.comboBox1.DisplayMember = "Name";
        this.comboBox1.ValueMember = "Id";
        this.comboBox1.SelectedValue = selectedValue;
    }
}</code>
Copy after login

These examples show the implementation of different methods, which method to choose depends on the specific application scenario and code structure. It is recommended to give priority to the use of public attributes or methods to avoid direct public control to improve the maintenance and readability of the code.
<code class="language-csharp">int value = 2; // 或从DataGridView获取
Form2 f = new Form2(value);
f.ShowDialog();</code>
Copy after login

The above is the detailed content of How to Set a ComboBox's SelectedValue on a Second Form by Selecting a DataGridView Row on the First Form?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template