Binding a Textbox to a property

  • Thread starter Thread starter mick
  • Start date Start date
M

mick

I have a property "TotalCost" of type int and a TextBox "tbTotalCost" in the
main form "Form1"
and was wondering how to bind one to the other

tbTotalCost.DataBinding.Add("Text",this.TotalWinnings, erm);

First peram. should be Property Name but in the examples Ive seen it usually
has "Text". Guessing its because it is the Text property of the TextBox?

Second peram. should be object DataSource. That the propery?

Third should be DataMember. Erm...

Flailing around a bit as you can see.

mick
 
mick said:
I have a property "TotalCost" of type int and a TextBox "tbTotalCost" in
the main form "Form1"
and was wondering how to bind one to the other

tbTotalCost.DataBinding.Add("Text",this.TotalWinnings, erm);

First peram. should be Property Name but in the examples Ive seen it
usually
has "Text". Guessing its because it is the Text property of the TextBox?

Second peram. should be object DataSource. That the propery?

Third should be DataMember. Erm...

Flailing around a bit as you can see.


Thought tbTotalCost.DataBinding.Add("Text", this,TotalWinnings.ToString());
might work but no. Tried changing the propery ToalCost to a string still
nothing.
Get - "Cannot bind to the property or column 0 on the DataSource.
Parameter name: dataMember."

This might mean something to someone. Doesnt seem to say why it cant bind
so isnt really much help.

mick
 
mick said:
I have a property "TotalCost" of type int and a TextBox "tbTotalCost" in
the main form "Form1"
and was wondering how to bind one to the other

tbTotalCost.DataBinding.Add("Text",this.TotalWinnings, erm);

Is the property you want to bind called "TotalCost"? Or "TotalWinnings"?
First peram. should be Property Name but in the examples Ive seen it
usually
has "Text". Guessing its because it is the Text property of the TextBox?

Second peram. should be object DataSource. That the propery?

No. It's the other object containing the property to which the "Text"
property of the "tbTotalCost" is bound.
Third should be DataMember. Erm...

That would be the name of the property in the other object.

Based on the code you posted, perhaps you want something more like this:

tbTotalCost.DataBinding.Add("Text", this, "TotalWinnings");

Though, typically I'd expect data bindings to be used between two
different objects, not to bind a property of one object to some other
property of the same object.

Pete
 
Peter Duniho said:
Is the property you want to bind called "TotalCost"? Or "TotalWinnings"?

Oops. There are actually two of them TotalCost and TotalWinnings. Cut and
Pasted the wrong one. Sorry.

No. It's the other object containing the property to which the "Text"
property of the "tbTotalCost" is bound.


That would be the name of the property in the other object.

Based on the code you posted, perhaps you want something more like this:

tbTotalCost.DataBinding.Add("Text", this, "TotalWinnings");

This compiles but doesnt update the TextBox. Is there somethig else I should
be doing?

While I was waiting for an answer I actually tried adding a couple of
EventsHandlers
and triggering them in the setters. It works but not sure if that is how
people usually
go about it.

mick
 
mick said:
[...]
Based on the code you posted, perhaps you want something more like this:

tbTotalCost.DataBinding.Add("Text", this, "TotalWinnings");

This compiles but doesnt update the TextBox. Is there somethig else I
should be doing?

It's not really possible to say for sure unless you post a concise but
complete code example, and explain exactly what you are hoping to
accomplish with the data binding.

Do note that for data binding to work, the objects involved have to
comply with one of the various data binding models supported. The data
binding docs go into more detail with respect to that.

Pete
 
mick said:
I have a property "TotalCost" of type int and a TextBox "tbTotalCost" in
the main form "Form1"
and was wondering how to bind one to the other

tbTotalCost.DataBinding.Add("Text",this.TotalWinnings, erm);

First peram. should be Property Name but in the examples Ive seen it
usually
has "Text". Guessing its because it is the Text property of the TextBox?

Second peram. should be object DataSource. That the propery?

Third should be DataMember. Erm...

Flailing around a bit as you can see.

mick

It's a web screen, right?

Overall, what is your application doing?
User one end..... what is the other?
What goes on inbetween?

It would be more usual for you to have a database or some other data store.
Then some data access layer between your screen and the store.
Maybe involving a web service.
That would present some typed object to the screen for binding.
Binding to properties of the screen doesn't make much sense.
Well, outside of richly interactive WPF applications.

I guess you don't understand how to architect an application and you've
dived into coding..
You're probably best looking at books which present complete recipes.
Even the most extensive online examples tend to gloss over or trivialise
things which are important in real world apps.

Hope this is of some help.
 
Back
Top