1

I've created a form that I call a ListChooser. It looks like so: enter image description here
It consists of a System.Windows.Forms.Button and .TextBox.

Clicking on the Button (shown above with the text "Fruits") causes a dialog to popup and some selections are made and the TextBox is populated, like so: enter image description here
My problem is I can't figure out how to set a System.Windows.Forms.Binding to this TextBox. I think the complication is that it's not just a TextBox, it's 2 components in one. (I know how to data bind with just a standalone TextBox). If I go to the TextBox > Properties > DataBindings > Advanced > ..., All entries listed under Property are not selectable:

enter image description here

I've tried this which has no effect:

private System.Windows.Forms.BindingSource myBindingSource;
this.myBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.myBindingSource.DataSource = typeof(Foods) // Fruits is a Property of Foods;
myListChooser.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myBindingSource, "Fruits", true));

How do I do this?

ListChooser.Designer.cs:

partial class ListChooser: UserControl
{
    /// <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.btnPopup = new System.Windows.Forms.Button();
        this.textBox = new System.Windows.Forms.TextBox();
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // btnPopup
        // 
        this.btnPopup.AutoSize = true;
        this.btnPopup.Dock = System.Windows.Forms.DockStyle.Fill;
        this.btnPopup.Location = new System.Drawing.Point(0, 0);
        this.btnPopup.Margin = new System.Windows.Forms.Padding(0);
        this.btnPopup.Name = "btnPopup";
        this.btnPopup.Size = new System.Drawing.Size(75, 21);
        this.btnPopup.TabIndex = 0;
        this.btnPopup.Text = "Type";
        this.btnPopup.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.btnPopup.UseVisualStyleBackColor = true;
        this.btnPopup.Click += new System.EventHandler(this.btnPopup_Click);
        // 
        // txtList
        // 
        this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBox.BackColor = System.Drawing.SystemColors.Window;
        this.textBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.textBox.Location = new System.Drawing.Point(0, 0);
        this.textBox.Name = "txtList";
        this.textBox.ReadOnly = true;
        this.textBox.Size = new System.Drawing.Size(297, 20);
        this.textBox.TabIndex = 1;
        this.textBox.TabStop = false;
        this.textBox.TextChanged += new System.EventHandler(this.txtList_TextChanged);
        //this.textBox.Leave += new System.EventHandler(this.txtList_Leave);
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Margin = new System.Windows.Forms.Padding(0);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.btnPopup);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.textBox);
        this.splitContainer1.Size = new System.Drawing.Size(375, 21);
        this.splitContainer1.SplitterDistance = 75;
        this.splitContainer1.SplitterWidth = 3;
        this.splitContainer1.TabIndex = 2;
        // 
        // ListChooser
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.WhiteSmoke;
        this.Controls.Add(this.splitContainer1);
        this.MaximumSize = new System.Drawing.Size(1200, 21);
        this.Name = "ListChooser";
        this.Size = new System.Drawing.Size(375, 21);
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel1.PerformLayout();
        this.splitContainer1.Panel2.ResumeLayout(false);
        this.splitContainer1.Panel2.PerformLayout();
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button btnPopup;
    private System.Windows.Forms.TextBoxBase textBox;

    private SplitContainer splitContainer1;
}
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • If you're not too far along, you might want to consider using [WPF](https://msdn.microsoft.com/en-us/library/ms754130(v=vs.110).aspx) and the [MVVM](https://msdn.microsoft.com/en-us/library/hh848246.aspx) design pattern for your application. WPF is designed to handle binding like this well. – Tim Hutchison Jun 01 '17 at 15:38
  • If you want to propose an answer with a Form using this pattern, I'm open to it. – Al Lelopath Jun 01 '17 at 16:21
  • You can't bind to a private member. Consider [using the Text property](https://stackoverflow.com/questions/2881409/text-property-in-a-usercontrol-in-c-sharp). – Hans Passant Jun 01 '17 at 16:52
  • @AlLelopath I don't think I could give a good example without know a little bit more about what you are trying to do in your application. Even a screenshot of the application would be helpful – Tim Hutchison Jun 01 '17 at 17:00
  • @HansPassant: Working on your suggestion. I found a problem in that my custom control `ListChooser` inherits from `System.Windows.Forms.UserControl`, which does not have a `DataSource` property, as opposed to a built-in control like `System.Windows.Forms.ComboBox`. Is there another class that I might use for a parent class that has `DataSource`? – Al Lelopath Jun 05 '17 at 15:18
  • I see how to set the DataSource [here](https://stackoverflow.com/questions/1616003/data-binding-for-textbox) – Al Lelopath Jun 05 '17 at 21:45

0 Answers0