0

I have set up Entity Framework with my VB.NET project. I have a model class that takes the data from a table from my MS SQL Server DB. My ListBox object is filled from the model class. What I am trying to do is when a user clicks on an item on from the listbox the text box is populated with the data from the table. If the user clicks on a mustang the text boxes are filled with the model, make, and year of a mustang.

I am using INotifyPropertyChanged in a viewmodel class that I thought would allow me to get each part of the dataset.

This code shows only one part of the requirements to better simplify the post

Public Class CarViewModel
Implements INotifyPropertyChanged

Private _SelectedCarModelName As CarModel

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged


Public Property SelectedCarModelName () As CarModel
    Get
        Return _SelectedCarModelName 
    End Get
    Set(ByVal value As CarModel)
        _SelectedCarModelName = value
        OnPropertyChanged("CarName")
    End Set
End Property

Public Overridable Sub OnPropertyChanged(propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class

My Textbox Data bind looks like this. My Textbox resides on the frm.vb.

txtCarName.DataBindings.Add("Text", _carViewModel.SelectedCarModelName.CarName, "CarName", True, DataSourceUpdateMode.OnPropertyChanged)

When I click on an item on the list it will access the property, but it will not set the property change. I know I am doing something wrong but I just don't know what it is.

******UPDATED*******

ViewModel

Public Class CarViewModel
Implements INotifyPropertyChanged

Private _SelectedCarModelName As CarModel

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property SelectedCarModelName As CarModel
    Get
        Return _SelectedCarModelName
    End Get
    Set(ByVal value As CarModel)
        _SelectedCarModelName= value
        OnPropertyChanged("SelectedCarModelName ")
    End Set
End Property

Public Sub OnPropertyChanged(propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class

Windows Form TextBox

**************Edited*******************

private _CarViewModel As CarViewModel
        Dim bs As New BindingSource(_CarViewModel , Nothing)
    txtCarName.DataBindings.Add _
("Text", bs, "SelectedCarModelName", True, DataSourceUpdateMode.OnPropertyChanged, String.Empty)

After I have restarted everything, for some reason my machine likes that, I am not getting an error anymore. When I debug the code is iterating through the Getter 3x, the number of fields. but it is not getting an output for the textbox.

  • See if [Does data binding support nested properties in Windows Forms?](https://stackoverflow.com/a/8894977/719186) solves the problem. – LarsTech Jan 24 '18 at 15:15
  • @LarsTech I have followed your link and used and did some changes. Now I am getting a system.argumentExceptionError "DataMember Property cannot be found on the datasource". You have any thoughts? – Robert DeSautel Jan 24 '18 at 16:19
  • I don't see in your post what you declared for the DataMember. – LarsTech Jan 24 '18 at 16:21
  • @LarsTech from what I Understand "CarName" would be the datamember and my source is _carViewModel.SelectedCarModelName . The "CarName" is a field in the CarModel class – Robert DeSautel Jan 24 '18 at 16:37
  • You're confusing me. Update the post so we know what the code looks like now. – LarsTech Jan 24 '18 at 16:49
  • @LarsTech I really appreciate your help with this. I know it is very very simple for some, but I just don't like working with vb.net let along with winforms....Just not clean to me :) – Robert DeSautel Jan 24 '18 at 17:16
  • Would help if you showed how you created the bs object. Make sure to add `True, DataSourceUpdateMode.OnPropertyChanged)` to your data binding call like you did before. Also, you have to use the actual property name. Change `OnPropertyChanged("PropertyName")` to `OnPropertyChanged("ChangedValue")` – LarsTech Jan 24 '18 at 17:19
  • @LarsTech Thank you for you help I left out how I created that. Sorry about that it is the Bindingsource object from the carviewmodel. I really do appreciate this help. – Robert DeSautel Jan 24 '18 at 19:09
  • Your property names are all over the place. You bind to "ChangedValue", but I don't see that property anywhere in your code. You raise the `OnPropertyName("CarName")` but the property is called "SelectedCarModelName". All those references to the property have to be the same. – LarsTech Jan 24 '18 at 19:12
  • @LarsTech I have changed the propertynames to the appropriate names and now I am getting a column isn't found in the collection. I have checked the model and I am spelling it correctly. – Robert DeSautel Jan 24 '18 at 19:51
  • I don't see your project. I only see what you posted above, and the code above does not have correct property names anywhere. – LarsTech Jan 24 '18 at 19:54
  • @LarsTech I updated the code to make a bit simpler. I am going to the to straight to the model and now my property objects say nothing, and it don't look like the onpropertychanged() is being raised... – Robert DeSautel Jan 24 '18 at 20:18
  • So frustrating. Your binding property is "DisplayName" but your class doesn't have that property. – LarsTech Jan 24 '18 at 20:19
  • @LarsTech Ok I took out the "DisplayName" and now it runs without error however I am not getting the CarName in the textbox. I am sorry for this inconvenience Lars. I really do appreciate your patience and help. I wish I understood this better. – Robert DeSautel Jan 24 '18 at 20:38
  • Now I see "SelectedLightTypeDisplayName", but your class has "SelectedCarModelName". You declare "_CarViewModel", don't initialize it, but bind to "_addLightViewModel". You have to post the code that you are using. This could have been solved hours ago. – LarsTech Jan 24 '18 at 20:39
  • @LarsTech Sorry that was a different code. but this is what I am using, my deepest apologies. Do I need to initialize _CarViewModel? I want to bind to the _CarViewModel, I believe, to get the data. – Robert DeSautel Jan 24 '18 at 20:49

0 Answers0