Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding a textbox to a custom class

Status
Not open for further replies.

dahlstrand

Programmer
May 27, 2005
5
SE
I'm building a custom class with properties.
I declair this class as a object in the form

I have managed to bind a texbox to a class property and gets the value into the textbox. So far all good.

The problem is when the value changes in the class it does not update the text box...

I'm I missing something...??
 
Well,

I think I understand what you mean and here is my solution, it's a simple one, it's up to you to make it a great one ;-)

let's say you have a custom class called TestClass:

Code:
Public Class testClass

    Private _txtBox As System.Windows.Forms.TextBox

    Public WriteOnly Property TheTextBox() As System.Windows.Forms.TextBox
        Set(ByVal Value As System.Windows.Forms.TextBox)
            _txtBox = Value
        End Set
    End Property

    Public Sub YourCustomProcess()

        'whateveryouwant

        _txtBox.Text = "you changed the textBox's text from your custom class!!"

    End Sub

End Class


Now let's take a look at your main form, you need a textBox and a button on it in order to test you custom class. We'll call the textBox textbox1 and your button, button1.

Don't forget to declare this at the top of your main form code :

Code:
Private testClassObj As testClass


Load event of your main form:

Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    testClassObj = New testClass
    testClassObj.TheTextBox = textbox1 

End Sub



Click event of button1 :

Code:
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        testClassObj.YourCustomProcess()

    End Sub

I hope this helped you somehow.
 
Does your custom class update the property to which your textbox is bound? Let say you bound your textbox something like this:
Code:
 Me.TextBox1.DataBindinds.Add("Text", MyCustomObject, "Text")
You would then need to ensure that your custom object reset its Text property as updates were made, something like this:
Code:
Private Sub DoSomeCalculations
    DoCalculations
    Me.Text = CalculatedResult
End Sub
Good Luck!

Have a great day!

j2consulting@yahoo.com
 
Thanks for your answer!

I have set up the code simular to the reply from MastaKilla exept that I bound the object as suggested by SBendBuckeye.

The quetion is that if the textbox is bound to the object Why do I have to manualy update the textbox as you both suggest?

If I bind the textbox to a dataset the text updates itselves I do not have to update the text manualy.

Updating the text manualy both makes the binding unnesseseray and puting the updating in the class makes the class less scalable if I want to use it on another form.

I could of course send the object (textbox) wich should be updated to the class but it does feel like using it in a way it was not ment. (something I will probobly have to get used to :) It must be a more correct way to do this as the dataset uses.

 
I think you misunderstood what I was trying to say. My question was, have you made sure that the Object property that you bound the TextBox to is being updated properly?

Here is a different example to maybe clear up the confusion:
Code:
Me.TextBox1.DataBindings.Add("Text", MyCustomObject, "MyCustomProperty")
Code:
Private Sub DoSomeCalculations
    DoCalculations
    Me.MyCustomProperty = CalculatedResult
End Sub
If MyCustomProperty is not being updated consistently within your custom class, then your TextBox will not appear to update correctly. Good Luck!

Have a great day!

j2consulting@yahoo.com
 
Sorry, that is correct

The property reflects a private variable in the class. This is being updated correctly.

I have also tried as in your last code in the "DoCalculations" procedure specificly adressed the property to updated the value of this property.

None of this updates my bound text box... :(

What I'm trying to do is to create a extended dataset that keeps track of the selected row and its values and makes moving and working with the row and it's values more easy. Together with som special functions for the object that the row represents.

Several windows can be working with the same object type and the underlying datatable that holds the object collection. The collection and subsets of the collection should be shered between different windows to eliminate having to open the same data in different MDI shild in different datasets and "dataviews".

Hope that the description clarefies the issue.
 
You may as well do some research on MSDN. I'm sure there are some interfaces you will need to implement to get the changes which are updated reflected in your databinding.
 
I have searched and searched in the MSDN for different information on binding and objects and classes. and veriation of these and simular/related words.

I have found that it might need to implement IList or simular collections but nothing concreet that will help me solve the issue.

If anyone has a god link I would love to have it so I can read and learn more or if you have an idea regarding if what I'm trying to do is programaticly/logicaly a god idea.
 
Can you post your binding statement?

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top