none
Setting property value? RRS feed

  • Question

  • I have a public property like this. Is it possible to set the value directly? for e.g on button click  SelectedCost= 1000                                                                                                                    

            Public Property SelectedCost() As Double
    
            Get
                Try
    
                    If Me.vt_rbn_estimated.Checked Then
                        Return vt_tbx_ecost.Text
    
                    ElseIf Me.vt_rbn_FixPr1.Checked Then
                        Return Decimal.Round(vt_tbx_tcost1.Text / vt_cbx_fxc1.SelectedValue, 2)
    
                    ElseIf Me.vt_rbn_FixPr2.Checked Then
                        Return Decimal.Round(vt_tbx_tcost2.Text / vt_cbx_fxc2.SelectedValue, 2)
    
                    ElseIf Me.vt_rbn_FixPr3.Checked Then
                        Return Decimal.Round(vt_tbx_tcost3.Text / vt_cbx_fxc3.SelectedValue, 2)
    
                    End If
    
                Catch
    
    
                End Try
    
            End Get
    
            Set(ByVal value As Double)
    
                '_SelectedCost = value
    
            End Set
    
        End Property

    Thanks

     
    Saturday, August 17, 2019 5:56 PM

Answers

  • Hi,
    try this code:

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SelectedCost = SetSelectedCost
      End Sub
    
      Public Function SetSelectedCost() As Decimal
        Dim cost As Decimal = 0
        Dim div As Decimal = 0
        If Me.vt_rbn_estimated.Checked AndAlso Decimal.TryParse(vt_tbx_ecost.Text, cost) Then Return cost
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_tbx_tcost1.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_tbx_tcost2.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_tbx_tcost3.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        Return cost
      End Function
    
      Public Property SelectedCost() As Decimal


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Shan1986 Friday, August 23, 2019 9:16 AM
    Saturday, August 17, 2019 8:10 PM
  • Hi,
    without knowing your code I cannot reproduce your problem.

    Try the following demo. The demo work well.

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class Form1
    
      Private WithEvents vt_rbn_estimated As New RadioButton With {.Text = "Estimated", .Top = 10, .Left = 10, .Checked = True}
      Private WithEvents vt_rbn_FixPr1 As New RadioButton With {.Text = "Fixed Price 1", .Top = 40, .Left = 10}
      Private WithEvents vt_rbn_FixPr2 As New RadioButton With {.Text = "Fixed Price 2", .Top = 70, .Left = 10}
      Private WithEvents vt_rbn_FixPr3 As New RadioButton With {.Text = "Fixed Price 3", .Top = 100, .Left = 10}
      Private WithEvents vt_tbx_ecost As New TextBox With {.Top = 10, .Left = 120}
      Private WithEvents vt_tbx_tcost1 As New TextBox With {.Top = 40, .Left = 120}
      Private WithEvents vt_tbx_tcost2 As New TextBox With {.Top = 70, .Left = 120}
      Private WithEvents vt_tbx_tcost3 As New TextBox With {.Top = 100, .Left = 120}
      Private WithEvents vt_cbx_fxc1 As New ComboBox With {.Top = 130, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc2 As New ComboBox With {.Top = 160, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc3 As New ComboBox With {.Top = 190, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents bs As New BindingSource
      Private dgv As New DataGridView With {.Top = 220, .Left = 10, .Width = 600, .DataSource = bs}
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SuspendLayout()
        Me.Controls.AddRange(New Control() {vt_cbx_fxc3, vt_cbx_fxc2, vt_cbx_fxc1, dgv})
        Me.Controls.AddRange(New Control() {vt_tbx_tcost3, vt_tbx_tcost2, vt_tbx_tcost1, vt_tbx_ecost})
        Me.Controls.AddRange(New Control() {vt_rbn_FixPr3, vt_rbn_FixPr2, vt_rbn_FixPr1, vt_rbn_estimated})
        bs.DataSource = GetData()
        vt_tbx_ecost.DataBindings.Add("Text", bs, "Estimated", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost1.DataBindings.Add("Text", bs, "FixPr1", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost2.DataBindings.Add("Text", bs, "FixPr2", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost3.DataBindings.Add("Text", bs, "FixPr3", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_cbx_fxc1.DataSource = GetFxc()
        vt_cbx_fxc2.DataSource = GetFxc()
        vt_cbx_fxc3.DataSource = GetFxc()
        AddHandler bs.CurrentChanged, AddressOf bs_CurrentChanged
        Me.ResumeLayout()
      End Sub
    
      Private Function GetData() As List(Of Data)
        Dim l As New List(Of Data)
        l.Add(New Data() With {.Estimated = 100, .FixPr1 = 75, .FixPr2 = 100, .FixPr3 = 150})
        l.Add(New Data() With {.Estimated = 200, .FixPr1 = 300, .FixPr2 = 400, .FixPr3 = 250})
        l.Add(New Data() With {.Estimated = 300, .FixPr1 = 500, .FixPr2 = 600, .FixPr3 = 700})
        Return l
      End Function
      Private Function GetFxc() As List(Of Fxc)
        Dim l As New List(Of Fxc)
        For i = 1 To 5
          l.Add(New Fxc With {.Div = i, .Description = $"Value {i}"})
        Next
        Return l
      End Function
    
      Private Sub Cost_Click(sender As Object, e As EventArgs) Handles vt_rbn_estimated.CheckedChanged,
              vt_rbn_FixPr1.CheckedChanged, vt_rbn_FixPr2.CheckedChanged, vt_rbn_FixPr3.CheckedChanged,
              vt_cbx_fxc1.SelectedIndexChanged, vt_cbx_fxc2.SelectedIndexChanged, vt_cbx_fxc3.SelectedIndexChanged
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Private Sub Cost_Lost(sender As Object, e As KeyEventArgs) Handles vt_tbx_ecost.KeyUp, vt_tbx_tcost1.KeyUp, vt_tbx_tcost2.KeyDown, vt_tbx_tcost3.KeyUp
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Private Function SetSelectedCost() As Decimal
        Dim d = TryCast(bs.Current, Data)
        If d Is Nothing Then Return 0
        Dim cost As Decimal = 0
        Dim div As Decimal = 1
        If Me.vt_rbn_estimated.Checked Then Return d.Estimated
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr1 / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr2 / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr3 / div, 2)
        Return cost
      End Function
    
      Private Sub bs_CurrentChanged(sender As Object, e As EventArgs)
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Public Class Data
        Implements INotifyPropertyChanged
        Public Property Estimated As Decimal
        Public Property FixPr1 As Decimal
        Public Property FixPr2 As Decimal
        Public Property FixPr3 As Decimal
        Private _selCost As Decimal
        Public Property SelCost As Decimal
          Get
            Return Me._selCost
          End Get
          Set(value As Decimal)
            Me._selCost = value
            OnPropChanged()
          End Set
        End Property
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Friend Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub
      End Class
    
      Public Class Fxc
        Public Property Div As Decimal
        Public Property Description As String
      End Class
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Shan1986 Tuesday, August 27, 2019 6:32 AM
    Monday, August 26, 2019 3:39 PM

All replies

  • Hi

    Suggest you build a test project using default controls and repost. There are too many control names to figure out and sort out in the example you posted. (you didn't even post an image of the UI to assist us)

    I do see that you are treating TextBox text as numeric values - not a good idea.

    I do see that you have an empty Try....Catch - not a good idea.


    Regards Les, Livingston, Scotland

    Saturday, August 17, 2019 6:20 PM
  • Hi,
    set OPTION STRICT ON and try this code:

      Public ReadOnly Property SelectedCost() As Decimal
        Get
          Dim cost As Decimal = 0
          Dim div As Decimal = 0
          If Me.vt_rbn_estimated.Checked AndAlso Decimal.TryParse(vt_tbx_ecost.Text, cost) Then Return cost
          If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_tbx_tcost1.Text, cost) _
            AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
          If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_tbx_tcost2.Text, cost) _
            AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
          If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_tbx_tcost3.Text, cost) _
            AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
          Return cost
        End Get
      End Property


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, August 17, 2019 6:39 PM
  • Hallo Peter , thanks for your answer but i wanted to know is it possible to set the value simply by a button click e,g 

      on button click

       SelectedCost= 1000 


    • Edited by Shan1986 Saturday, August 17, 2019 6:53 PM
    Saturday, August 17, 2019 6:53 PM
  • Hi,
    try this code:

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SelectedCost = SetSelectedCost
      End Sub
    
      Public Function SetSelectedCost() As Decimal
        Dim cost As Decimal = 0
        Dim div As Decimal = 0
        If Me.vt_rbn_estimated.Checked AndAlso Decimal.TryParse(vt_tbx_ecost.Text, cost) Then Return cost
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_tbx_tcost1.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_tbx_tcost2.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_tbx_tcost3.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        Return cost
      End Function
    
      Public Property SelectedCost() As Decimal


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Shan1986 Friday, August 23, 2019 9:16 AM
    Saturday, August 17, 2019 8:10 PM
  • Hi,
    try this code:

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SelectedCost = SetSelectedCost
      End Sub
    
      Public Function SetSelectedCost() As Decimal
        Dim cost As Decimal = 0
        Dim div As Decimal = 0
        If Me.vt_rbn_estimated.Checked AndAlso Decimal.TryParse(vt_tbx_ecost.Text, cost) Then Return cost
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_tbx_tcost1.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_tbx_tcost2.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_tbx_tcost3.Text, cost) _
          AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2)
        Return cost
      End Function
    
      Public Property SelectedCost() As Decimal


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Hallo Peter, 

    I am having a different problem.  I am binding this selected cost to one of the datatable column "Selected Cost"

    the goal is to select one cost for the product among 4 costs . here everything works but the problem is when ever the new radiobutton is selected and if the textbox is empty, i can not type the first letter or the first digit disappear only works from the 2 nd digit, that means i have to type the first digit twice. Here is my code .

    On textbox text change and radio button change

    SelectedCost = SetSelectedCost

    Public Function SetSelectedCost() As Decimal Dim cost As Decimal = 0 Dim div As Decimal = 0 If Me.vt_rbn_estimated.Checked AndAlso Decimal.TryParse(vt_tbx_ecost.Text, cost) Then Return cost If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_tbx_tcost1.Text, cost) _ AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2) If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_tbx_tcost2.Text, cost) _ AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2) If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_tbx_tcost3.Text, cost) _ AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, cost) Then Return Decimal.Round(cost / div, 2) Return cost

    NotifyPropertyChanged("SelectedCost")

    End Function

    Databinding

      
    Form1.DataBindings.Add("SelecedCost", EQBS, "DtSelCost", DatasourceUpdateMethod.onPropertyChanged)

    Could you please tell me why the first digit disappears?

    Thanks

    Monday, August 26, 2019 6:49 AM
  • Hi,
    without knowing your code I cannot reproduce your problem.

    Try the following demo. The demo work well.

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class Form1
    
      Private WithEvents vt_rbn_estimated As New RadioButton With {.Text = "Estimated", .Top = 10, .Left = 10, .Checked = True}
      Private WithEvents vt_rbn_FixPr1 As New RadioButton With {.Text = "Fixed Price 1", .Top = 40, .Left = 10}
      Private WithEvents vt_rbn_FixPr2 As New RadioButton With {.Text = "Fixed Price 2", .Top = 70, .Left = 10}
      Private WithEvents vt_rbn_FixPr3 As New RadioButton With {.Text = "Fixed Price 3", .Top = 100, .Left = 10}
      Private WithEvents vt_tbx_ecost As New TextBox With {.Top = 10, .Left = 120}
      Private WithEvents vt_tbx_tcost1 As New TextBox With {.Top = 40, .Left = 120}
      Private WithEvents vt_tbx_tcost2 As New TextBox With {.Top = 70, .Left = 120}
      Private WithEvents vt_tbx_tcost3 As New TextBox With {.Top = 100, .Left = 120}
      Private WithEvents vt_cbx_fxc1 As New ComboBox With {.Top = 130, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc2 As New ComboBox With {.Top = 160, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc3 As New ComboBox With {.Top = 190, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents bs As New BindingSource
      Private dgv As New DataGridView With {.Top = 220, .Left = 10, .Width = 600, .DataSource = bs}
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SuspendLayout()
        Me.Controls.AddRange(New Control() {vt_cbx_fxc3, vt_cbx_fxc2, vt_cbx_fxc1, dgv})
        Me.Controls.AddRange(New Control() {vt_tbx_tcost3, vt_tbx_tcost2, vt_tbx_tcost1, vt_tbx_ecost})
        Me.Controls.AddRange(New Control() {vt_rbn_FixPr3, vt_rbn_FixPr2, vt_rbn_FixPr1, vt_rbn_estimated})
        bs.DataSource = GetData()
        vt_tbx_ecost.DataBindings.Add("Text", bs, "Estimated", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost1.DataBindings.Add("Text", bs, "FixPr1", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost2.DataBindings.Add("Text", bs, "FixPr2", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost3.DataBindings.Add("Text", bs, "FixPr3", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_cbx_fxc1.DataSource = GetFxc()
        vt_cbx_fxc2.DataSource = GetFxc()
        vt_cbx_fxc3.DataSource = GetFxc()
        AddHandler bs.CurrentChanged, AddressOf bs_CurrentChanged
        Me.ResumeLayout()
      End Sub
    
      Private Function GetData() As List(Of Data)
        Dim l As New List(Of Data)
        l.Add(New Data() With {.Estimated = 100, .FixPr1 = 75, .FixPr2 = 100, .FixPr3 = 150})
        l.Add(New Data() With {.Estimated = 200, .FixPr1 = 300, .FixPr2 = 400, .FixPr3 = 250})
        l.Add(New Data() With {.Estimated = 300, .FixPr1 = 500, .FixPr2 = 600, .FixPr3 = 700})
        Return l
      End Function
      Private Function GetFxc() As List(Of Fxc)
        Dim l As New List(Of Fxc)
        For i = 1 To 5
          l.Add(New Fxc With {.Div = i, .Description = $"Value {i}"})
        Next
        Return l
      End Function
    
      Private Sub Cost_Click(sender As Object, e As EventArgs) Handles vt_rbn_estimated.CheckedChanged,
              vt_rbn_FixPr1.CheckedChanged, vt_rbn_FixPr2.CheckedChanged, vt_rbn_FixPr3.CheckedChanged,
              vt_cbx_fxc1.SelectedIndexChanged, vt_cbx_fxc2.SelectedIndexChanged, vt_cbx_fxc3.SelectedIndexChanged
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Private Sub Cost_Lost(sender As Object, e As KeyEventArgs) Handles vt_tbx_ecost.KeyUp, vt_tbx_tcost1.KeyUp, vt_tbx_tcost2.KeyDown, vt_tbx_tcost3.KeyUp
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Private Function SetSelectedCost() As Decimal
        Dim d = TryCast(bs.Current, Data)
        If d Is Nothing Then Return 0
        Dim cost As Decimal = 0
        Dim div As Decimal = 1
        If Me.vt_rbn_estimated.Checked Then Return d.Estimated
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr1 / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr2 / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, div) Then Return Decimal.Round(d.FixPr3 / div, 2)
        Return cost
      End Function
    
      Private Sub bs_CurrentChanged(sender As Object, e As EventArgs)
        Dim d = TryCast(bs.Current, Data)
        If d IsNot Nothing Then d.SelCost = SetSelectedCost()
      End Sub
    
      Public Class Data
        Implements INotifyPropertyChanged
        Public Property Estimated As Decimal
        Public Property FixPr1 As Decimal
        Public Property FixPr2 As Decimal
        Public Property FixPr3 As Decimal
        Private _selCost As Decimal
        Public Property SelCost As Decimal
          Get
            Return Me._selCost
          End Get
          Set(value As Decimal)
            Me._selCost = value
            OnPropChanged()
          End Set
        End Property
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Friend Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub
      End Class
    
      Public Class Fxc
        Public Property Div As Decimal
        Public Property Description As String
      End Class
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by Shan1986 Tuesday, August 27, 2019 6:32 AM
    Monday, August 26, 2019 3:39 PM
  • Thanks Peter, i will try & understand your code after going home. 
    Tuesday, August 27, 2019 6:32 AM
  • Hi,
    copy the code in an empty Form in Windows project without any additional design.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, August 27, 2019 7:00 AM
  • Hallo Peter,

    I am having trouble adopting your code with datatable, I have made following function to test but i could not get it working with datatable. Could you please help me?. Thanks

        Private Function GetData() As DataTable
    
            Dim table As New DataTable
    
            table.Columns.Add("Estimated", GetType(Integer))
            table.Columns.Add("FixPr1", GetType(Integer))
            table.Columns.Add("FixPr2", GetType(Integer))
            table.Columns.Add("FixPr3", GetType(Integer))
            table.Columns.Add("SelCost1", GetType(Integer))
    
    
            table.Rows.Add(25, 50, 70, 90, 0)
            table.Rows.Add(30, 55, 66, 95, 0)
            table.Rows.Add(15, 40, 67, 33, 0)
    
            Return table
    
        End Function

    Wednesday, August 28, 2019 6:29 PM
  • Hi,
    copy the code in an empty Form in Windows project without any additional design.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Hallo Peter, 

    Any idea how to get it work with datatable?

    thanks.


    Saturday, August 31, 2019 6:10 PM
  • Hi,
    if you use an another type of DataSource you must change all references to the data objects. Try this demo:

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class Form1
    
      Private WithEvents vt_rbn_estimated As New RadioButton With {.Text = "Estimated", .Top = 10, .Left = 10, .Checked = True}
      Private WithEvents vt_rbn_FixPr1 As New RadioButton With {.Text = "Fixed Price 1", .Top = 40, .Left = 10}
      Private WithEvents vt_rbn_FixPr2 As New RadioButton With {.Text = "Fixed Price 2", .Top = 70, .Left = 10}
      Private WithEvents vt_rbn_FixPr3 As New RadioButton With {.Text = "Fixed Price 3", .Top = 100, .Left = 10}
      Private WithEvents vt_tbx_ecost As New TextBox With {.Top = 10, .Left = 120}
      Private WithEvents vt_tbx_tcost1 As New TextBox With {.Top = 40, .Left = 120}
      Private WithEvents vt_tbx_tcost2 As New TextBox With {.Top = 70, .Left = 120}
      Private WithEvents vt_tbx_tcost3 As New TextBox With {.Top = 100, .Left = 120}
      Private WithEvents vt_cbx_fxc1 As New ComboBox With {.Top = 130, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc2 As New ComboBox With {.Top = 160, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents vt_cbx_fxc3 As New ComboBox With {.Top = 190, .Left = 20, .ValueMember = "Div", .DisplayMember = "Description"}
      Private WithEvents bs As New BindingSource
      Private dgv As New DataGridView With {.Top = 220, .Left = 10, .Width = 600, .DataSource = bs}
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SuspendLayout()
        Me.Controls.AddRange(New Control() {vt_cbx_fxc3, vt_cbx_fxc2, vt_cbx_fxc1, dgv})
        Me.Controls.AddRange(New Control() {vt_tbx_tcost3, vt_tbx_tcost2, vt_tbx_tcost1, vt_tbx_ecost})
        Me.Controls.AddRange(New Control() {vt_rbn_FixPr3, vt_rbn_FixPr2, vt_rbn_FixPr1, vt_rbn_estimated})
        bs.DataSource = GetData()
        vt_tbx_ecost.DataBindings.Add("Text", bs, "Estimated", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost1.DataBindings.Add("Text", bs, "FixPr1", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost2.DataBindings.Add("Text", bs, "FixPr2", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_tbx_tcost3.DataBindings.Add("Text", bs, "FixPr3", True, DataSourceUpdateMode.OnPropertyChanged)
        vt_cbx_fxc1.DataSource = GetFxc()
        vt_cbx_fxc2.DataSource = GetFxc()
        vt_cbx_fxc3.DataSource = GetFxc()
        AddHandler bs.CurrentChanged, AddressOf Calculate_SelCost1
        Me.ResumeLayout()
      End Sub
    
      Private Function GetData() As DataTable
    
        Dim table As New DataTable
    
        table.Columns.Add("Estimated", GetType(Decimal))
        table.Columns.Add("FixPr1", GetType(Decimal))
        table.Columns.Add("FixPr2", GetType(Decimal))
        table.Columns.Add("FixPr3", GetType(Decimal))
        table.Columns.Add("SelCost1", GetType(Decimal))
    
    
        table.Rows.Add(25, 50, 70, 90, 0)
        table.Rows.Add(30, 55, 66, 95, 0)
        table.Rows.Add(15, 40, 67, 33, 0)
    
        Return table
    
      End Function
    
      Private Function GetFxc() As List(Of Fxc)
        Dim l As New List(Of Fxc)
        For i = 1 To 5
          l.Add(New Fxc With {.Div = i, .Description = $"Value {i}"})
        Next
        Return l
      End Function
    
      Private Sub Calculate_SelCost1(sender As Object, e As EventArgs) _
        Handles vt_tbx_ecost.KeyUp, vt_tbx_tcost1.KeyUp, vt_tbx_tcost2.KeyDown, vt_tbx_tcost3.KeyUp,
              vt_rbn_estimated.CheckedChanged,
              vt_rbn_FixPr1.CheckedChanged, vt_rbn_FixPr2.CheckedChanged, vt_rbn_FixPr3.CheckedChanged,
              vt_cbx_fxc1.SelectedIndexChanged, vt_cbx_fxc2.SelectedIndexChanged, vt_cbx_fxc3.SelectedIndexChanged
        Dim d = TryCast(bs.Current, DataRowView)
        If d IsNot Nothing Then d.Row("SelCost1") = SetSelectedCost()
      End Sub
    
      Private Function SetSelectedCost() As Decimal
        Dim d = TryCast(bs.Current, DataRowView)
        If d Is Nothing Then Return 0
        Dim cost As Decimal = 0
        Dim div As Decimal = 1
        If Me.vt_rbn_estimated.Checked Then Return d.Row.Field(Of Decimal)("Estimated")
        If Me.vt_rbn_FixPr1.Checked AndAlso Decimal.TryParse(vt_cbx_fxc1.SelectedValue?.ToString, div) Then Return Decimal.Round(d.Row.Field(Of Decimal)("FixPr1") / div, 2)
        If Me.vt_rbn_FixPr2.Checked AndAlso Decimal.TryParse(vt_cbx_fxc2.SelectedValue?.ToString, div) Then Return Decimal.Round(d.Row.Field(Of Decimal)("FixPr2") / div, 2)
        If Me.vt_rbn_FixPr3.Checked AndAlso Decimal.TryParse(vt_cbx_fxc3.SelectedValue?.ToString, div) Then Return Decimal.Round(d.Row.Field(Of Decimal)("FixPr3") / div, 2)
        Return cost
      End Function
    
      Public Class Fxc
        Public Property Div As Decimal
        Public Property Description As String
      End Class
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, September 1, 2019 7:42 AM
  • Thanks Peter. 
    Wednesday, September 4, 2019 7:28 AM