How to ‘bind’ a textbox and dataobject property.

ALT

Member
Joined
Feb 23, 2006
Messages
9
Programming Experience
10+
Experience: Beginner Vb.NETer ( But experience programmer)
Version: 2005 .Net

Warning: I am most likely mis-using the terms binding etc, so please take what I am saying with a grain of salt. Also I have simplified the situation greatly to highlight the issue.

I want to bind a textbox on a form to a property of a data object. ( Not a field of a table of a database ). I want the text in the textbox to display what ever is in the bound to property. E.g. the text should change when the property value changes etc. I am sure that the binding must be done at run time but I have no idea how. Below is an code example. What would I need to do to complete it?

I’m thinking something like:

VB.NET:
[COLOR=blue][FONT=Courier New]Me[/FONT][/COLOR][FONT=Courier New].TextBox1.datasource = Me.oData.FooBar[/FONT]

VB.NET:
[FONT=Courier New][FONT=Courier New]<[COLOR=blue]Global[/COLOR].Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _[/FONT]
[COLOR=blue][FONT=Courier New]Partial[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR] Form1[/FONT]
[FONT=Courier New]   [COLOR=blue]Inherits[/COLOR] System.Windows.Forms.Form[/FONT]
 
[FONT=Courier New]   [COLOR=blue]Private[/COLOR] oData [COLOR=blue]As[/COLOR] Data_Class = [COLOR=blue]New[/COLOR] Data_Class[/FONT]
 
[FONT=Courier New]   [COLOR=green]'Form overrides dispose to clean up the component list.[/COLOR][/FONT]
[FONT=Courier New]   <System.Diagnostics.DebuggerNonUserCode()> _[/FONT]
[FONT=Courier New]   [COLOR=blue]Protected[/COLOR] [COLOR=blue]Overrides[/COLOR] [COLOR=blue]Sub[/COLOR] Dispose([COLOR=blue]ByVal[/COLOR] disposing [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR])[/FONT]
[FONT=Courier New]       [COLOR=blue]If[/COLOR] disposing [COLOR=blue]AndAlso[/COLOR] components [COLOR=blue]IsNot[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]           components.Dispose()[/FONT]
[FONT=Courier New]       [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]MyBase[/COLOR].Dispose(disposing)[/FONT]
[FONT=Courier New]   [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
 
[FONT=Courier New]   [COLOR=green]'Required by the Windows Form Designer[/COLOR][/FONT]
[FONT=Courier New]   [COLOR=blue]Private[/COLOR] components [COLOR=blue]As[/COLOR] System.ComponentModel.IContainer[/FONT]
 
[FONT=Courier New]   [COLOR=green]'NOTE: The following procedure is required by the Windows Form Designer[/COLOR][/FONT]
[FONT=Courier New]   [COLOR=green]'It can be modified using the Windows Form Designer.  [/COLOR][/FONT]
[FONT=Courier New]   [COLOR=green]'Do not modify it using the code editor.[/COLOR][/FONT]
[FONT=Courier New]   <System.Diagnostics.DebuggerStepThrough()> _[/FONT]
[FONT=Courier New]   [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] InitializeComponent()[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].TextBox1 = [COLOR=blue]New[/COLOR] System.Windows.Forms.TextBox[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].SuspendLayout()[/FONT]
[FONT=Courier New]       [COLOR=green]'[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=green]'TextBox1[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=green]'[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].TextBox1.Location = [COLOR=blue]New[/COLOR] System.Drawing.Point(13, 13)[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].TextBox1.Name = [COLOR=maroon]"TextBox1"[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].TextBox1.Size = [COLOR=blue]New[/COLOR] System.Drawing.Size(100, 20)[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].TextBox1.TabIndex = 0[/FONT]
[FONT=Courier New]       [COLOR=green]'[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=green]'Form1[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=green]'[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].AutoScaleDimensions = [COLOR=blue]New[/COLOR] System.Drawing.SizeF(6.0!, 13.0!)[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].ClientSize = [COLOR=blue]New[/COLOR] System.Drawing.Size(143, 58)[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].Controls.Add([COLOR=blue]Me[/COLOR].TextBox1)[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].Name = [COLOR=maroon]"Form1"[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].Text = [COLOR=maroon]"Form1"[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].ResumeLayout([COLOR=blue]False[/COLOR])[/FONT]
[FONT=Courier New]       [COLOR=blue]Me[/COLOR].PerformLayout()[/FONT]
 
[FONT=Courier New]   [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT=Courier New]   [COLOR=blue]Friend[/COLOR] [COLOR=blue]WithEvents[/COLOR] TextBox1 [COLOR=blue]As[/COLOR] System.Windows.Forms.TextBox[/FONT]
 
[COLOR=blue][FONT=Courier New]End[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR][/FONT]
[COLOR=green][FONT=Courier New]'======================================================[/FONT][/COLOR]
[COLOR=blue][FONT=Courier New]Public[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR] Data_Class[/FONT]
 
[FONT=Courier New]   [COLOR=blue]Private[/COLOR] mFooBar [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=maroon]"This is a test"[/COLOR][/FONT]
 
[FONT=Courier New]   [COLOR=blue]Public[/COLOR] [COLOR=blue]Property[/COLOR] FooBar() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
[FONT=Courier New]       [COLOR=blue]Get[/COLOR][/FONT]
[FONT=Courier New]           [COLOR=blue]Return[/COLOR] [COLOR=blue]Me[/COLOR].mFooBar[/FONT]
[FONT=Courier New]       [COLOR=blue]End[/COLOR] [COLOR=blue]Get[/COLOR][/FONT]
 
[FONT=Courier New]       [COLOR=blue]Set[/COLOR]([COLOR=blue]ByVal[/COLOR] pValue [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR])[/FONT]
[FONT=Courier New]           [COLOR=blue]Me[/COLOR].mFooBar = pValue[/FONT]
[FONT=Courier New]       [COLOR=blue]End[/COLOR] [COLOR=blue]Set[/COLOR][/FONT]
[FONT=Courier New]   [COLOR=blue]End[/COLOR] [COLOR=blue]Property[/COLOR][/FONT]
 
[COLOR=blue][FONT=Courier New]End[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Class[/COLOR] [COLOR=green]' Data_Class[/COLOR][/FONT]
[/FONT]
 
Back
Top