Another day on this and I still can't get things working. I've taken all
your advice (as best I've understood it) and created the most simple
example I can think of:
User Control with two TextBox controls.
Here is the code for the UserControl:
<code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace PMD.Library.WinFormControls
{
public partial class ReallySimpleAddress : UserControl
{
public ReallySimpleAddress()
{
InitializeComponent();
}
[Editor("System.ComponentModel.Design.MultilineStringEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
SettingsBindable(true)]
[Bindable(true)]
public string Address1
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
[Editor("System.ComponentModel.Design.MultilineStringEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
SettingsBindable(true)]
[Bindable(true)]
public string Address2
{
get { return textBox2.Text; }
set { textBox2.Text = value; }
}
}
}
// Designer.cs code (in case it helps)
namespace PMD.Library.WinFormControls
{
partial class ReallySimpleAddress
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(3, 3);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(3, 29);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
//
// ReallySimpleAddress
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "ReallySimpleAddress";
this.Size = new System.Drawing.Size(109, 54);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
}
</code>
I then have a simple Windows application with a single form. I've created
once instance of my "Address" object (dumb object, only fields and
properties, doesn't inherit from anything (except object of course)
I've added the UserControl to my form, then from the property editor
created a BindingSource from my Address object.
I've bound the Address1 property of the UserControl to the Address1
property of the business object and done the same for Address2.
When I run my application and type in the Address1 field of the user
control, then leave the control (lose focus) the new value sticks. Good.
When I type in the Address2 field and leave focus it reverts back to the
value it was initialized to!
Ahh... I don't get it.
I've looked at the properties of a TextBox in reflector. The text
property has no special attributes, so I don't know how it shows up in the
binding area of the properties editor.
I added the [Binding(true)] attribute which seems to be the attribute that
makes the property appear in the bindable properties list.
Here is some more relevant code:
<code>
public Form1()
{
_address.Address1 = "350 N. Maplewood St.";
_address.Address2 = "Suite 101";
_address.City = "Orange";
_address.State = "CA";
_address.Zipcode = "92866";
InitializeComponent();
// Address testing
addressBindingSource.DataSource = _address;
}
namespace PMD.BusinessEntities.NetsuiteEntities
{
public class Address
{
private string _address1;
private string _address2;
private string _city;
private string _state;
private string _zipcode;
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get { return _address2; }
set { _address2 = value; }
}
public string City
{
get { return _city; }
set { _city = value; }
}
public string State
{
get { return _state; }
set { _state = value; }
}
public string Zipcode
{
get { return _zipcode; }
set { _zipcode = value; }
}
}
}
//
// reallySimpleAddress1
//
this.reallySimpleAddress1.Address1 = "";
this.reallySimpleAddress1.Address2 = "";
this.reallySimpleAddress1.DataBindings.Add(new
System.Windows.Forms.Binding("Address1", this.addressBindingSource,
"Address1", true));
this.reallySimpleAddress1.DataBindings.Add(new
System.Windows.Forms.Binding("Address2", this.addressBindingSource,
"Address2", true));
this.reallySimpleAddress1.Location = new System.Drawing.Point(361, 137);
this.reallySimpleAddress1.Name = "reallySimpleAddress1";
this.reallySimpleAddress1.Size = new System.Drawing.Size(109, 54);
this.reallySimpleAddress1.TabIndex = 5;
</code>
I REALLY hope someone sees the problem or has some idea what in the world
I'm doing wrong. This is much more complex and difficult that I had
expected.
If a simple and complete (as Mr. Skeet usually requests) would be in order
let me know and I will zip and post online.
Thanks for any help,
Steve
Marc Gravell said:
For info, EditorBrowsable mainly controls intellisense, not the
designer. As Nicholas says - looking at the properties for similar
controls is a good route - either by reflection, ILDASM, or reflector
[the easiest route].
Marc