Change the access modifier In Form2 Set the access modifier for the control / field public Anywhere Form1.
Form2 f = new Form2(); f.ShowDialog(); this.textBox1.Text = f.textBox1.Text;
- The fastest to implement and convenient way
- Contrary to all the basics of OOP
- The transmission of only a later form of an earlier
- Form f only shown using ShowDialog (), ie in the first form control returns only the second closing. Avoid this by maintaining a link to the second form in the first form
Use of public property / method. The method is very similar to the first In Form2 class defines a property (or method).
In Form2 class defines a property (or method)
public string Data { get { return textBox1.Text; } }
Anywhere in Form1
Form2 f = new Form2(); f.ShowDialog(); this.textBox1.Text = f.Data;
- Does not contradict all the basics of OOP
- Cons same
Data transfer to the constructor Form2. Change the constructor Form2
public Form2(string data) { InitializeComponent(); this.data = data; } string data;
And create a shape anywhere Form1 as follows:
Form2 f = new Form2(this.textBox1.Text); f.ShowDialog();
To send a link to constructor. Change the constructor Form2.
public Form2(Form1 f1) { InitializeComponent(); string s = f1.textBox1.Text; }
And create a shape anywhere Form1 so that pass it a reference to the first form
Form2 f = new Form2(this); f.ShowDialog();
- Access to all open fields / functions of the first form
- Data transfer is possible in both directions
- Violates the PLO
Use the property 'parent'. When you create a second form sets the owner
Form2 f = new Form2(); f.Owner = this; f.ShowDialog();
In the second, we determine the owner
Form1 main = this.Owner as Form1; if(main != null) { string s = main.textBox1.Text; main.textBox1.Text = "OK"; }
- Access to all open fields / functions of the first form
- Data transfer is possible in both directions
- Do not violate the PLO
Use a separate class. Create a separate class, better static, mainly namespace, ie for example in the Program.cs file
static class Data { public static string Value { get; set; } }
His public properties / methods are available from any form.
Data.Value = "111";
- The most convenient way when data is actively used in several forms.
Transfer method in the constructor. Create delegate
public delegate void MyDelegate(string data);
In Form1, create a method that will handle the received data
void func(string param) { //Process }
Create a second form as follows:
Form2 f = new Form2(new MyDelegate(GetData)); f.ShowDialog();
Change constructor of second form to accept a delegate
MyDelegate d; public Form2(MyDelegate sender) { InitializeComponent(); d= sender; }
And at any moment send data
d(textBox1.Text);
Creating a separate class with a delegate. In main namespace create separate class
public static class Data { public delegate void MyEvent(string data); public static MyEvent EventHandler; }
In the first form, add a handler
void func(string param) { MessageBox.Show(param); }
and initialize the EventHandler
Data.EventHandler = new Data.MyEvent(func);
Create a second form in the usual way and then call it from
Data.EventHandler(textBox1.Text);
- The most flexible way to transfer data
- Difficult to implement and understand
How to make a global list in c# [closed]
-
21-09-2022 - |
سؤال
I have three windows forms in my project, and I have an object class. How can I make a list of those objects that will be available for all three forms to use?
المحلول
نصائح أخرى
You could create a class containing an instance of your object:
public class MyClass
{
public static List<string> MyList {get; set}
}
Then you can access it from your form
MyClass.MyList = new List<string>();
Obviously string will be replaced with the name of your object.
Here u have create new class(Base class) which inherited from Form
class like this:
[System.ComponentModel.DesignerCategory("")]
public class FormBase : System.Windows.Forms.Form
{
public List<string> MyList;
}
After that inherit all of ur form(3 forms) with this class like this:
[System.ComponentModel.DesignerCategory("Form")]
public partial class DeriveClass : FormBase
{
private void Method()
{
base.MyList = new List<string>();
// So something..
}
}
لا تنتمي إلى StackOverflow