Execute or Initilize a Class??

Hobbyist_programmer 621 Reputation points
2021-03-16T21:24:12.907+00:00

Hallo

I have a Bindinglist of Objects bound to a datagridview. I populate this list with a Sub. Problem is these list of objects are not initialized unless i click or activate the datagridview. Is there a way to initilize these objects so they do their work without activating them first?.

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,735 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2021-03-17T08:57:12+00:00

    Hi, here is my sample code . also you can see the problem in the attached Gif,

    basically i pass my properties to another class and do some calculations there and get the total in "Sample" class. If the DGV is active then it returns the total. How can i fix it?

    78742-sample.gif

    Imports System.ComponentModel  
    Imports System.Reflection  
    Imports System.Linq.Expressions  
      
    Public Class Form1  
      
        Public SampleList As New Samples  
        Public WithEvents BS_SampleList As New BindingSource  
      
        Public CalList As New BindingList(Of Calculation)  
        Public BS_SampleCalculation As New BindingSource  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            BS_SampleList.DataSource = SampleList  
            DataGridView1.DataSource = BS_SampleList  
      
            BS_SampleCalculation.DataSource = CalList  
            DataGridView2.DataSource = BS_SampleCalculation  
      
        End Sub  
      
        Private Class PropertyList  
            Inherits List(Of Calculation)  
            Public Sub New(obj As Object)  
                For Each prop In obj.GetType().GetProperties(BindingFlags.Public Or BindingFlags.Instance)  
                    Me.Add(New Calculation(obj, prop))  
                    Debug.Print(obj.ToString & prop.Name)  
                Next  
            End Sub  
        End Class  
      
      
        Public Class Calculation  
            Public Sub New(obj As Object, prop As PropertyInfo)  
                Me.obj = obj  
                Me.prop = prop  
            End Sub  
      
            Private obj As Object  
            Private prop As PropertyInfo  
            Public ReadOnly Property ID As String  
                Get  
                    Return Me.prop.Name  
                End Get  
            End Property  
            Public ReadOnly Property Description As String  
                Get  
                    For Each att As CustomAttributeData In Me.prop.CustomAttributes  
                        Dim args = att.ConstructorArguments  
                        If args.Count > 0 Then Return args(0).ToString.Substring(1, args(0).ToString.Length - 2)  
                    Next  
                    Return String.Empty  
                End Get  
            End Property  
            Public Property Quantity As Object  
                Get  
                    Return Me.prop.GetValue(Me.obj)  
                End Get  
                Set(value As Object)  
                    Me.prop.SetValue(Me.obj, CInt(value))  
                End Set  
            End Property  
            Public ReadOnly Property Cost As Integer?  
                Get  
                    If ID = "TT" Then  
                        Dim Tot As Integer? = Form1.getTot()  
                        prop.SetValue(Me.obj, Tot)  
                        Return Form1.getTot()  
                    Else  
                        If CInt(Me.prop.GetValue(Me.obj)) > 0 Then  
                            Return CInt(Me.prop.GetValue(Me.obj)) * 100  
                        Else  
                            Return 1000  
                        End If  
                    End If  
                End Get  
            End Property  
      
        End Class  
      
        Public Function getTot() As Integer?  
            Return Aggregate e In CalList Where e.ID Like "I*" Into Sum(e.Cost)  
        End Function  
      
      
        Public Class Sample  
      
            <Description("Item 1")> <Browsable(False)> Public Property I1 As Integer?  
            <Description("Item 2")> <Browsable(False)> Public Property I2 As Integer?  
            <Description("Item 3")> <Browsable(False)> Public Property I3 As Integer?  
            <Description("Total")> <Browsable(False)> Public Property TT As Integer?  
            Public Property ID As String  
            Public Property Name As String  
            Public ReadOnly Property CTotal As Integer?  
                Get  
                    Return TT  
                End Get  
            End Property  
      
        End Class  
      
        Public Class Samples  
            Inherits BindingList(Of Sample)  
        End Class  
      
      
        Private Sub Btn_AddSample_Click(sender As Object, e As EventArgs) Handles Btn_AddSample.Click  
      
            SampleList.Add(New Sample With {.ID = 1, .Name = "Sample1"})  
      
        End Sub  
      
        Private Sub BS_SampleList_CurrentChanged(sender As Object, e As EventArgs) Handles BS_SampleList.CurrentChanged  
      
            Dim CurrentS = CType(BS_SampleList.Current, Sample)  
      
            If Not CurrentS Is Nothing Then  
                CalList.Clear()  
                Dim selected_properties = CurrentS.GetType().GetProperties().Where(Function(p) p.GetCustomAttributes().Any(Function(a) TypeOf a Is DescriptionAttribute))  
                For Each prop In selected_properties  
                    CalList.Add(New Calculation(CurrentS, prop))  
                Next  
      
            End If  
      
        End Sub  
      
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click  
            DataGridView2.Refresh()  
            DataGridView1.Refresh()  
        End Sub  
    End Class  
      
    
    0 comments No comments

  2. Xingyu Zhao-MSFT 5,366 Reputation points
    2021-03-19T02:47:38.513+00:00

    Hi @Hobbyist_programmer ,
    You can consider using the following remedies:
    When you click cell in DataGridView1, TabControl will switch to the TabPage where DataGridView2 is located, and then return to the original TabPage.

        Private pageName As String  
        Private pageIsChanged = False  
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick  
      
            If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "CTotal" Then  
      
                If Not TabControl1.SelectedTab.Name.Equals("TabPage2") Then  
                    pageName = TabControl1.SelectedTab.Name  
                    TabControl1.SelectedTab = TabControl1.TabPages("TabPage2")  
                    pageIsChanged = True  
                End If  
            End If  
        End Sub  
        Private Sub DataGridView1_CellMouseLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave  
            If pageIsChanged Then  
                TabControl1.SelectedTab = TabControl1.TabPages(pageName)  
                pageIsChanged = False  
            End If  
        End Sub  
    

    Result of my test :
    79465-gif.gif
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.