Bonjour,
Je débute en programmation et j'ai créé un projet de test. Il est constitué d'un form(avec un bindingnavigator) dans lequel j'ai déposé un usercontrol ("MyControl") possédant deux textbox liés à une base de donnée.
Le tout fonctionne convenablement mais je souhaiterais savoir s'il est possible d'écrire le code de manière plus "condensée" dans le usercontrol ("Mycontrol"). Ce que je voudrais savoir c'est s'il n'est pas possible de rassembler le public string employeeName et le public string Country. Ce qui pourrait être intéressant je crois si je place une douzaine de textbox.
Si cela est possible, cela aura forcément des répercutions sur le code de mon form. Qu'elles seraient les modification a apporter?
Voici le code du Formet le code de MyControl
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 private void button1_Click(object sender, EventArgs e) { //Populate rows to the dataset. sqlDataAdapter1.Fill(dataSet11.Tables["Customers"]); this.customersBindingSource.DataSource = dataSet11.Tables["Customers"]; this.textBox1.DataBindings.Add(new Binding ("Text", this.customersBindingSource,"CompanyName")); //Set employeeLName as the data-bound property, and set dataSet11 as the data source. this.bindingNavigator1.BindingSource = this.customersBindingSource; this.Controls.Add(this.bindingNavigator1); //Set employeeLName as the data-bound property, and set dataSet11 as the data source. this.myControl1.DataBindings.Add(new Binding("employeeName", this.customersBindingSource, "CompanyName")); this.myControl1.DataBindings.Add(new Binding("Country", this.customersBindingSource, "Country")); button1.Visible = false; }
Merci à ceux qui voudront bien y regarder
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public MyControl() { InitializeComponent(); } public event System.ComponentModel.PropertyChangedEventHandler employeeNameChanged; public string empLName = ""; public string test2 = ""; //Makes this property bindable. [System.ComponentModel.Bindable(true)] public string EmployeeName { get { return empLName; } set { empLName = value; this.textBox1.Text = empLName.ToString(); } } public string Country { get { return test2; } set { test2 = value; this.textBox2.Text = test2.ToString(); } } public void OnemployeeNameChanged(System.ComponentModel.PropertyChangedEventArgs e) { //Raise the employeeIDChanged event. if (employeeNameChanged != null) employeeNameChanged(this, e); } }
Partager