none
winforms: מה עדיף: control חדש, או שינוי המקור של הBinding של הקיים? ואיך עושים את האפשרות השניה. RRS feed

  • שאלה

  • יש לי רשימה של אובייקטים (נאמר לקוחות), לכל אובייקט יש כמה עשרות מאפיינים (שם כתובת וכו'), ויש לי טופס עריכה לאובייקט יחיד.

    כאשר פותחים אובייקט מהרשימה לעריכה, נפתח מופע מהטופס המיעוד לעריכה, כשכל הפקדים הרלוונטים מקבלים לproperty המתאים במחלקה אותה רוצים לערוך.

    כיון שזה טופס עשיר בפקדים ותכונות ועוד (מדובר לצערי בwinform), חשבתי שכאשר יש עריכות מרובות אולי עדיף במקום לפתוח מופעים רבים של טופס העריכה, ליצור כזה אחד בלבד, ולהחזיק אוסף של אובייקטים בעריכה, וכל פעם העריכה האקטיבית תאוכלס בטופס - הbinding של הפקדים יופנה אל האובייקט הזה.

    האם זה אכן הדרך הנכונה לדעתכם?

    אם כן, אני לא יודע ליישם אותה... כשמוסיפים binding לפקד, מאפיין הסורס שלול הוא לקריאה בלבד. צריך להסיר את הbinding וליצור חדש, לא מסורבל מידי?


    תודה רבה מראש! הבעיה הזאת ממש תוקעת אותי.

    יום ראשון 18 מרץ 2012 11:21

תשובות

  • הפתרון הוא להשתמש ב- BindingSource מה- Toolbox תחת הקטגוריה Data

    הנה דוגמא קטנה:

    מתוך הקובץ Form1.Designer.cs

    		private void InitializeComponent()
    		{
    			this.components = new System.ComponentModel.Container();
    			this.txtFirstName = new System.Windows.Forms.TextBox();
    			this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
    			this.label1 = new System.Windows.Forms.Label();
    			this.label2 = new System.Windows.Forms.Label();
    			this.txtLastName = new System.Windows.Forms.TextBox();
    			this.btnSelectAdam = new System.Windows.Forms.Button();
    			this.btnSelectEve = new System.Windows.Forms.Button();
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
    			this.SuspendLayout();
    			// 
    			// txtFirstName
    			// 
    			this.txtFirstName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "FirstName", true));
    			this.txtFirstName.Location = new System.Drawing.Point(113, 50);
    			this.txtFirstName.Name = "txtFirstName";
    			this.txtFirstName.Size = new System.Drawing.Size(111, 22);
    			this.txtFirstName.TabIndex = 0;
    			// 
    			// bindingSource1
    			// 
    			this.bindingSource1.AllowNew = false;
    			this.bindingSource1.DataSource = typeof(FormBindingSample.Person);
    			// 
    			// label1
    			// 
    			this.label1.AutoSize = true;
    			this.label1.Location = new System.Drawing.Point(17, 50);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(80, 17);
    			this.label1.TabIndex = 1;
    			this.label1.Text = "First Name:";
    			// 
    			// label2
    			// 
    			this.label2.AutoSize = true;
    			this.label2.Location = new System.Drawing.Point(14, 90);
    			this.label2.Name = "label2";
    			this.label2.Size = new System.Drawing.Size(80, 17);
    			this.label2.TabIndex = 2;
    			this.label2.Text = "Last Name:";
    			// 
    			// txtLastName
    			// 
    			this.txtLastName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "LastName", true));
    			this.txtLastName.Location = new System.Drawing.Point(113, 90);
    			this.txtLastName.Name = "txtLastName";
    			this.txtLastName.Size = new System.Drawing.Size(111, 22);
    			this.txtLastName.TabIndex = 3;
    			// 
    			// btnSelectAdam
    			// 
    			this.btnSelectAdam.Location = new System.Drawing.Point(37, 197);
    			this.btnSelectAdam.Name = "btnSelectAdam";
    			this.btnSelectAdam.Size = new System.Drawing.Size(91, 23);
    			this.btnSelectAdam.TabIndex = 4;
    			this.btnSelectAdam.Text = "Adam";
    			this.btnSelectAdam.UseVisualStyleBackColor = true;
    			this.btnSelectAdam.Click += new System.EventHandler(this.btnSelectAdam_Click);
    			// 
    			// btnSelectEve
    			// 
    			this.btnSelectEve.Location = new System.Drawing.Point(146, 197);
    			this.btnSelectEve.Name = "btnSelectEve";
    			this.btnSelectEve.Size = new System.Drawing.Size(102, 23);
    			this.btnSelectEve.TabIndex = 5;
    			this.btnSelectEve.Text = "Eve";
    			this.btnSelectEve.UseVisualStyleBackColor = true;
    			this.btnSelectEve.Click += new System.EventHandler(this.btnSelectEve_Click);
    			// 
    			// Form1
    			// 
    			this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    			this.ClientSize = new System.Drawing.Size(612, 525);
    			this.Controls.Add(this.btnSelectEve);
    			this.Controls.Add(this.btnSelectAdam);
    			this.Controls.Add(this.txtLastName);
    			this.Controls.Add(this.label2);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.txtFirstName);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
    			this.ResumeLayout(false);
    			this.PerformLayout();
    
    		}
    
    		#endregion
    
    		private System.Windows.Forms.TextBox txtFirstName;
    		private System.Windows.Forms.BindingSource bindingSource1;
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.Label label2;
    		private System.Windows.Forms.TextBox txtLastName;
    		private System.Windows.Forms.Button btnSelectAdam;
    		private System.Windows.Forms.Button btnSelectEve;
    	}
    

    ובקובץ Form1.cs

    	public partial class Form1 : Form
    	{
    		private List<Person> _people;
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void Form1_Load(object sender, EventArgs e)
    		{
    			_people = new List<Person> {
    				new Person { FirstName = "Adam", LastName = "First"},
    				new Person { FirstName = "Eve", LastName = "Second"},
    			};
    			bindingSource1.DataSource = _people[0];
    		}
    
    		private void btnSelectAdam_Click(object sender, EventArgs e)
    		{
    			bindingSource1.DataSource = _people[0];
    		}
    
    		private void btnSelectEve_Click(object sender, EventArgs e)
    		{
    			bindingSource1.DataSource = _people[1];
    		}
    	}
    

    ה- Class של הדאטה הוא: Person:

    	public class Person
    	{
    		public string FirstName { get; set; }
    		public string LastName { get; set; }
    	}
    



    --- Adar Wesley

    יום רביעי 21 מרץ 2012 17:55

כל התגובות

  • זה תלוי,האם אתה רוצה לאפשר למשתמש לערוך כמה אובייקטים בו זמנית,או אחד בלבד

    אם אחד בלבד,אין סיבה לפתוח טופס חדש

    יום שלישי 20 מרץ 2012 16:37
  • הפתרון הוא להשתמש ב- BindingSource מה- Toolbox תחת הקטגוריה Data

    הנה דוגמא קטנה:

    מתוך הקובץ Form1.Designer.cs

    		private void InitializeComponent()
    		{
    			this.components = new System.ComponentModel.Container();
    			this.txtFirstName = new System.Windows.Forms.TextBox();
    			this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
    			this.label1 = new System.Windows.Forms.Label();
    			this.label2 = new System.Windows.Forms.Label();
    			this.txtLastName = new System.Windows.Forms.TextBox();
    			this.btnSelectAdam = new System.Windows.Forms.Button();
    			this.btnSelectEve = new System.Windows.Forms.Button();
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
    			this.SuspendLayout();
    			// 
    			// txtFirstName
    			// 
    			this.txtFirstName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "FirstName", true));
    			this.txtFirstName.Location = new System.Drawing.Point(113, 50);
    			this.txtFirstName.Name = "txtFirstName";
    			this.txtFirstName.Size = new System.Drawing.Size(111, 22);
    			this.txtFirstName.TabIndex = 0;
    			// 
    			// bindingSource1
    			// 
    			this.bindingSource1.AllowNew = false;
    			this.bindingSource1.DataSource = typeof(FormBindingSample.Person);
    			// 
    			// label1
    			// 
    			this.label1.AutoSize = true;
    			this.label1.Location = new System.Drawing.Point(17, 50);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(80, 17);
    			this.label1.TabIndex = 1;
    			this.label1.Text = "First Name:";
    			// 
    			// label2
    			// 
    			this.label2.AutoSize = true;
    			this.label2.Location = new System.Drawing.Point(14, 90);
    			this.label2.Name = "label2";
    			this.label2.Size = new System.Drawing.Size(80, 17);
    			this.label2.TabIndex = 2;
    			this.label2.Text = "Last Name:";
    			// 
    			// txtLastName
    			// 
    			this.txtLastName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "LastName", true));
    			this.txtLastName.Location = new System.Drawing.Point(113, 90);
    			this.txtLastName.Name = "txtLastName";
    			this.txtLastName.Size = new System.Drawing.Size(111, 22);
    			this.txtLastName.TabIndex = 3;
    			// 
    			// btnSelectAdam
    			// 
    			this.btnSelectAdam.Location = new System.Drawing.Point(37, 197);
    			this.btnSelectAdam.Name = "btnSelectAdam";
    			this.btnSelectAdam.Size = new System.Drawing.Size(91, 23);
    			this.btnSelectAdam.TabIndex = 4;
    			this.btnSelectAdam.Text = "Adam";
    			this.btnSelectAdam.UseVisualStyleBackColor = true;
    			this.btnSelectAdam.Click += new System.EventHandler(this.btnSelectAdam_Click);
    			// 
    			// btnSelectEve
    			// 
    			this.btnSelectEve.Location = new System.Drawing.Point(146, 197);
    			this.btnSelectEve.Name = "btnSelectEve";
    			this.btnSelectEve.Size = new System.Drawing.Size(102, 23);
    			this.btnSelectEve.TabIndex = 5;
    			this.btnSelectEve.Text = "Eve";
    			this.btnSelectEve.UseVisualStyleBackColor = true;
    			this.btnSelectEve.Click += new System.EventHandler(this.btnSelectEve_Click);
    			// 
    			// Form1
    			// 
    			this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    			this.ClientSize = new System.Drawing.Size(612, 525);
    			this.Controls.Add(this.btnSelectEve);
    			this.Controls.Add(this.btnSelectAdam);
    			this.Controls.Add(this.txtLastName);
    			this.Controls.Add(this.label2);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.txtFirstName);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
    			this.ResumeLayout(false);
    			this.PerformLayout();
    
    		}
    
    		#endregion
    
    		private System.Windows.Forms.TextBox txtFirstName;
    		private System.Windows.Forms.BindingSource bindingSource1;
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.Label label2;
    		private System.Windows.Forms.TextBox txtLastName;
    		private System.Windows.Forms.Button btnSelectAdam;
    		private System.Windows.Forms.Button btnSelectEve;
    	}
    

    ובקובץ Form1.cs

    	public partial class Form1 : Form
    	{
    		private List<Person> _people;
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void Form1_Load(object sender, EventArgs e)
    		{
    			_people = new List<Person> {
    				new Person { FirstName = "Adam", LastName = "First"},
    				new Person { FirstName = "Eve", LastName = "Second"},
    			};
    			bindingSource1.DataSource = _people[0];
    		}
    
    		private void btnSelectAdam_Click(object sender, EventArgs e)
    		{
    			bindingSource1.DataSource = _people[0];
    		}
    
    		private void btnSelectEve_Click(object sender, EventArgs e)
    		{
    			bindingSource1.DataSource = _people[1];
    		}
    	}
    

    ה- Class של הדאטה הוא: Person:

    	public class Person
    	{
    		public string FirstName { get; set; }
    		public string LastName { get; set; }
    	}
    



    --- Adar Wesley

    יום רביעי 21 מרץ 2012 17:55