public class Contact { public string Nom { get; set; } public string Prenom { get; set; } public DateTime Naissance { get; set; } public string NomComplet { get { return Prenom + " " + Nom; } } public override string ToString() { return string.Format("Je m'appelle {0} (1}", Prenom, Nom); } }
public partial class frmMickBristol : Form { public frmMickBristol() { InitializeComponent(); } private void frmMickBristol_Load(object sender, EventArgs e) { Contact jeanSor = new Contact { Prenom = "Jean", Nom = "Sor", Naissance = DateTime.Parse("01/01/2000") }; contactBindingSource.DataSource = jeanSor; } }
partial class frmMickBristol { /// <summary> /// Variable nécessaire au concepteur. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Nettoyage des ressources utilisées. /// </summary> /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Code généré par le Concepteur Windows Form /// <summary> /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas /// le contenu de cette méthode avec l'éditeur de code. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.contactBindingSource = new System.Windows.Forms.BindingSource(this.components); this.label4 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.contactBindingSource)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(48, 32); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 13); this.label1.TabIndex = 0; this.label1.Text = "Nom"; // // textBox1 // this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.contactBindingSource, "Nom", true)); this.textBox1.Location = new System.Drawing.Point(150, 29); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 1; // // textBox2 // this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.contactBindingSource, "Prenom", true)); this.textBox2.Location = new System.Drawing.Point(150, 72); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 3; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(48, 75); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(43, 13); this.label2.TabIndex = 2; this.label2.Text = "Prénom"; // // textBox3 // this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.contactBindingSource, "Naissance", true)); this.textBox3.Location = new System.Drawing.Point(150, 118); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(100, 20); this.textBox3.TabIndex = 5; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(48, 121); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(96, 13); this.label3.TabIndex = 4; this.label3.Text = "Date de naissance"; // // contactBindingSource // this.contactBindingSource.DataSource = typeof(test.Contact); // // label4 // this.label4.AutoSize = true; this.label4.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.contactBindingSource, "NomComplet", true)); this.label4.Location = new System.Drawing.Point(92, 170); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(35, 13); this.label4.TabIndex = 6; this.label4.Text = "label4"; // // frmMickBristol // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(299, 239); this.Controls.Add(this.label4); this.Controls.Add(this.textBox3); this.Controls.Add(this.label3); this.Controls.Add(this.textBox2); this.Controls.Add(this.label2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Name = "frmMickBristol"; this.Text = "frmMickBristol"; this.Load += new System.EventHandler(this.frmMickBristol_Load); ((System.ComponentModel.ISupportInitialize)(this.contactBindingSource)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.BindingSource contactBindingSource; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; }
changement que j'effectue dans les textbox est maintenant assigné automatiquement a mes proprietés.
A quel "moment" les valeurs sont assignées ?oui
La valeur du label a l'air de changer lors que je change de control dans la form?
y a t-il moyen d'effectuer les changements seulement au click d'un bouton par exemple ?de base non, mais tu peux avoir deux variables.
Contact c1 = new Contact(); .... Contact c2 = c1;
public class Contact : ICloneable { public string Nom { get; set; } public string Prenom { get; set; } public DateTime Naissance { get; set; } public string NomComplet { get { return Prenom + " " + Nom; } } public override string ToString() { return string.Format("Je m'appelle {0} {1}", Prenom, Nom); } #region ICloneable Membres public Contact Clone() { return new Contact { Nom = this.Nom, Prenom = this.Prenom, Naissance = this.Naissance };//là j'ai un doute concernant naissance puisque le type est turquoise.... } #endregion }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionje crée une liste contenant des textbox List<TextBox> TbList = new List< TbList.Add(tb_R1); TbList.Add(tb_R3); TbList.Add(tb_R9); je crée une liste contenant des int, List<int> R_values = new List<int>(); R_values.Add(obj.R1); R_values.Add(obj.R3); R_values.Add(obj.R9); maintenant je veux associer mes texbox a mes int en fesant une boucle for for (i = 0; i< 3 ; i++) { R_values[i] = Convert.ToInt32(TbList[i].Text); }
namespace test
{
public class Class1
{
public string MyProperty { get; set; }
public string MyProperty2 { get; set; }
public string MyProperty3 { get; set; }
}
public partial class Form1 : Form
{
Class1 mc = new Class1();
public Form1()
{
InitializeComponent();
Transfertdata();
}
private void Transfertdata()
{
List<string> ListSource = new List<string> { "111", "222", "333" };
List<string> ListDestionation = new List<string> { mc.MyProperty, mc.MyProperty2, mc.MyProperty3 };
for (int i = 0; i < 3; i++)
{
ListDestionation[i] = ListSource[i];
}
}
}
}
List<string> ListDestionation = new List<string> { mc.MyProperty, mc.MyProperty2, mc.MyProperty3 };
Contact toto = new Contact { Nom = "Sors", Prenom = "Jean", Naissance = uneDate };
Contact toto = new Contact () toto.Nom = "Sors"; toto.Prenom = "Jean"; toto.Naissance = uneDate;
Contact clone = monPrincipal.Clone();crée un deuxième contact avec les mêmes données mais avec un instance différente.