Home >Backend Development >C++ >How to Modify Controls in One Windows Form Based on Another Form's Selection?
Data interaction between windows: Modify the control value according to the selection of another window
How to change the control value in a window based on the choice of another window? For example, when the combination box in Form 1 needs to fill the data according to the lines selected in the data grid in Form 2, this problem will occur.
Solution:
In C#, the data communication between the windows is similar to the interaction between the class. Here are several methods to achieve this goal:
Set parameters in the constructor of Form 2: Create an instance of Form 2 and pass the required value as the parameter.
Create public attributes or methods in Form 2: configure the required attributes or methods in Form 2, and visit them from Form 1.Pass the instance of Form 1 to Form 2: Create a attribute or method that can access from Form 2 in Form 1. Event from Form 1 Subscribe to Form 2: Create an event in Form 2 and subscribe to it from Form 1.
Use the constructor method:
In Form 1:
<code class="language-csharp">public partial class Form2 : Form { int selectedValue; public Form2(int value) { InitializeComponent(); selectedValue = value; } //... }</code>Use ACTION attribute:
<code class="language-csharp">int value = 2; var f = new Form2(value);</code>In Form 1:
By using these technologies, you can effectively realize interaction between different windows in Windows Forms applications.
The above is the detailed content of How to Modify Controls in One Windows Form Based on Another Form's Selection?. For more information, please follow other related articles on the PHP Chinese website!