PC Review


Reply
Thread Tools Rate Thread

Another Binding Question... Even Simpler...

 
 
ZorpiedoMan
Guest
Posts: n/a
 
      9th Jul 2004
Why Doesn't THIS work:
-----------------------------------------------------------
A Windows Form has three controls:
TextBox1
BindButton
ShowTextButton
ChangeTextButton

Code:

Public Class SimpleClass

Private myText as String

Public Sub New(NewText as String)
myText = NewText
End Sub

Public Property Text() As String
Get
Return myText
End Get
Set(Value as String)
myText = Value
End Set
End Property

End Class

Private mySimpleClass as new SimpleClass("Hello")

Private Sub BindButtonClick(...)...
TextBox1.DataBindings.Add("Text", mySimpleClass, "Text")
End Sub

Private Sub ShowTextButtonClick(...)...
MsgBox(mySimpleClass.Text)
End Sub

Private Sub changeTextButtonClick(...)...
mySimpleClass.Textx = "Goodbye"
End Sub
-----------------------------------------------------------

Ok... Here's what happens...

1. Run the program. No problem
2. Click The ShowText Button. "Hello" is shown. Fine.
3. Click the BindButton. TextBox1 shows "Hello". Still Fine.
4. Type "World" into TextBox1.
5. Click the ShowText Button. "World" is shown. So Far So Good...
6. Click the ChangeText Button. PROBLEM: TextBox1 still shows
"World". NOT Good.
7. Click the ShowText Button. "GoodBye" is shown. As Expected.

Why this one-way behavior? How do I get my SimpleClass to notify the
Textbox it needs to change?


--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      9th Jul 2004
It doesent work because you need to inform the binding manager on the form
that something has changed.

Declare a public Event TextChanged As Event Handler

In your Set Property, Raise the event if the text is different from before
by calling a sub which raises this event TextChanged.

Then it will work.



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"ZorpiedoMan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Why Doesn't THIS work:
> -----------------------------------------------------------
> A Windows Form has three controls:
> TextBox1
> BindButton
> ShowTextButton
> ChangeTextButton
>
> Code:
>
> Public Class SimpleClass
>
> Private myText as String
>
> Public Sub New(NewText as String)
> myText = NewText
> End Sub
>
> Public Property Text() As String
> Get
> Return myText
> End Get
> Set(Value as String)
> myText = Value
> End Set
> End Property
>
> End Class
>
> Private mySimpleClass as new SimpleClass("Hello")
>
> Private Sub BindButtonClick(...)...
> TextBox1.DataBindings.Add("Text", mySimpleClass, "Text")
> End Sub
>
> Private Sub ShowTextButtonClick(...)...
> MsgBox(mySimpleClass.Text)
> End Sub
>
> Private Sub changeTextButtonClick(...)...
> mySimpleClass.Textx = "Goodbye"
> End Sub
> -----------------------------------------------------------
>
> Ok... Here's what happens...
>
> 1. Run the program. No problem
> 2. Click The ShowText Button. "Hello" is shown. Fine.
> 3. Click the BindButton. TextBox1 shows "Hello". Still Fine.
> 4. Type "World" into TextBox1.
> 5. Click the ShowText Button. "World" is shown. So Far So Good...
> 6. Click the ChangeText Button. PROBLEM: TextBox1 still shows
> "World". NOT Good.
> 7. Click the ShowText Button. "GoodBye" is shown. As Expected.
>
> Why this one-way behavior? How do I get my SimpleClass to notify the
> Textbox it needs to change?
>
>
> --Zorpy
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      9th Jul 2004
Full Example using check boxes

Here we have an Class Person. Within it we declare a public event called
CheckedChanged, this is made up of the property which the binding manager
listens for an event for once binding has been set. When the Property
changes from checked to unchecked we call a helper function which raises
this event and the binding manager syncs the control ( rad1 ) to this
property.

Button1.click changes the property of the aPerson object to checked, this is
then reflected through the bindings in the rad1 state.

On_Load is where we set the bindings

HTH

Private WithEvents aPerson As New Person

Public Class Person

Public Event CheckedChanged As EventHandler

Private m_checked As Boolean

Public Sub New()
m_checked = False
End Sub

Public Property Checked() As Boolean
Get
Return m_checked
End Get
Set(ByVal value As Boolean)
m_checked = value
PropertyChanged(EventArgs.Empty)
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
rad1.DataBindings.Add("Checked", aPerson, "Checked")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
aPerson.checked = True
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .




 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      9th Jul 2004
Hi Zorpy,

I cannot answer your question, however when you do this it works.

mySimpleClass.Text = "Goodbye"
Me.BindingContext(mySimpleClass).ResumeBinding()

I hope it helps anyhow?

Cor


 
Reply With Quote
 
ZorpiedoMan
Guest
Posts: n/a
 
      9th Jul 2004
What/Where is PropertyChanged?


--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
ZorpiedoMan
Guest
Posts: n/a
 
      9th Jul 2004
PropertyChanged is supposed to be part of IpropertyChange which is
supposed to be in system.ComponentModel, yet I don't seem to have it???

Where the heck is it... even documentation is coming up blank on
"PropertyChanged" ...



--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
ZorpiedoMan
Guest
Posts: n/a
 
      9th Jul 2004
I can't get this to work either...

Did I follow your suggestion correctly:

Public Class SimpleClass


Private myText As String
Public Event TextChanged As EventHandler

Public Sub New(ByVal NewText As String)
myText = NewText
End Sub

Public Property Text() As String
Get
Return myText
End Get
Set(ByVal Value As String)
If myText <> Value Then
Tellme()
End If
myText = Value
End Set
End Property

Private Sub Tellme()
RaiseEvent TextChanged(Me, New EventArgs)
End Sub

End Class

--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      9th Jul 2004
> I can't get this to work either...
>

Did you try what I sand more than an half hour ago?

Cor


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      9th Jul 2004
Zorpy,
In addition to the other comments, You need to define an Event as
EventHandler that has the same name as the Property with Changed on the end.
For example your Text property would need a TextChanged event, a Count
Property would need a CountChanged event.

This is explained in the following article

http://msdn.microsoft.com/library/de...et02252003.asp

Here is an example binding to multiple properties, with notifications:

Public Class Person

Public Event Text1Changed As EventHandler
Public Event Text2Changed As EventHandler

Private m_text1 As String
Private m_text2 As String

Public Sub New()
m_text1 = String.Empty
m_text2 = String.Empty
End Sub

Public Property Text1() As String
Get
Return m_text1
End Get
Set(ByVal value As String)
m_text1 = value
OnText1Changed(EventArgs.Empty)
End Set
End Property

Public Property Text2() As String
Get
Return m_text2
End Get
Set(ByVal value As String)
m_text2 = value
OnText2Changed(EventArgs.Empty)
End Set
End Property

Protected Overridable Sub OnText1Changed(ByVal e As EventArgs)
RaiseEvent Text1Changed(Me, e)
End Sub

Protected Overridable Sub OnText2Changed(ByVal e As EventArgs)
RaiseEvent Text2Changed(Me, e)
End Sub

End Class


Public Class PersonForm
Inherits System.Windows.Forms.Form

' ... designer generated code

Private aPerson As New Person

Private Sub Person_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.TextBox1.DataBindings.Add("Text", aPerson, "Text1")
Me.TextBox2.DataBindings.Add("Text", aPerson, "Text2")
End Sub

End Class


Hope this helps
Jay


"ZorpiedoMan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Why Doesn't THIS work:
> -----------------------------------------------------------
> A Windows Form has three controls:
> TextBox1
> BindButton
> ShowTextButton
> ChangeTextButton
>
> Code:
>
> Public Class SimpleClass
>
> Private myText as String
>
> Public Sub New(NewText as String)
> myText = NewText
> End Sub
>
> Public Property Text() As String
> Get
> Return myText
> End Get
> Set(Value as String)
> myText = Value
> End Set
> End Property
>
> End Class
>
> Private mySimpleClass as new SimpleClass("Hello")
>
> Private Sub BindButtonClick(...)...
> TextBox1.DataBindings.Add("Text", mySimpleClass, "Text")
> End Sub
>
> Private Sub ShowTextButtonClick(...)...
> MsgBox(mySimpleClass.Text)
> End Sub
>
> Private Sub changeTextButtonClick(...)...
> mySimpleClass.Textx = "Goodbye"
> End Sub
> -----------------------------------------------------------
>
> Ok... Here's what happens...
>
> 1. Run the program. No problem
> 2. Click The ShowText Button. "Hello" is shown. Fine.
> 3. Click the BindButton. TextBox1 shows "Hello". Still Fine.
> 4. Type "World" into TextBox1.
> 5. Click the ShowText Button. "World" is shown. So Far So Good...
> 6. Click the ChangeText Button. PROBLEM: TextBox1 still shows
> "World". NOT Good.
> 7. Click the ShowText Button. "GoodBye" is shown. As Expected.
>
> Why this one-way behavior? How do I get my SimpleClass to notify the
> Textbox it needs to change?
>
>
> --Zorpy
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
ZorpiedoMan
Guest
Posts: n/a
 
      9th Jul 2004
Cor -

No I didn't, because I don't think it really applies. The sample I'm
giving is just that - a stripped down version to demonstrate the
problem. The property of the Class will be getting changed
programitacally, not from the form, so Me.BindingContext is not exposed
to the class. Pretend for a moment that the Text property of my class
keeps changing every second to show the current time. The change is
taking place in the class, and the form needs to be notified of the
change to update the textbox. The class has no idea if it is on a form
or not.

I think the PropertyChanged suggestion is probably the right one, I just
can't find it anywhere.

--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
binding to gridview question Yankee Imperialist Dog Microsoft ASP .NET 0 25th Feb 2009 03:52 PM
Pivot Table question or a simpler suggestion Michael C Microsoft Excel Misc 2 10th May 2008 01:50 PM
Binding question =?Utf-8?B?ZHVzdHk=?= Microsoft Windows 2000 Networking 2 6th Mar 2006 10:32 PM
QUESTION- Simpler Way to Add a Data Series to Other Graphs? phil6666 Microsoft Excel Misc 0 10th Feb 2005 05:57 PM
Binding Question =?Utf-8?B?RGVubmlz?= Microsoft VB .NET 1 6th Dec 2004 12:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:59 AM.