Results 1 to 3 of 3

Thread: [RESOLVED] How to get the BindingSource.Current of a different form?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] How to get the BindingSource.Current of a different form?

    Hi -

    Is it possible for form B to read form A's BindingSource current position?

    For example, if "x" is an integer variable on Form B:

    Dim frm as Form
    frm = FormA

    x = frm.BindingSource.Current("RecordID")
    I can read other Form A properties from Form B (e.g., the Form A's ActiveControl.Name).

    Appreciate anyone's input on this question.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get the BindingSource.Current of a different form?

    That code is rather horrible for several reasons. First of all, you're relying on late-binding in two places in three lines of code. I strongly suggest that you turn Option Strict On for this project and also turn it On in the IDE options, so that it will be On by default for all future projects.

    You first declare your 'frm' variable as type Form and then assign a FormA object to it. Unless you want the variable to refer to several different types of forms, declare it as the type it's going to refer to, i.e. FormA. By doing so, you will then see members of FormA, instead of just members of Form, in the Intellisense listing. That allows you to use early-binding in that case rather than late-binding.

    The second instance of late-binding is when you get the "RecordID" field from the current record. The Current property of a BindingSource is type Object and the Object class cannot be indexed. You obviously know the specific type of the object returned by the property so you should be casting as that type. If you've bound a DataTable to that BindingSource then each record will be a DataRowView, so that's the type you should be using.

    Actually, you are likely relying on an implicit conversion there too. I'm guessing that 'x' is an Integer variable and the "RecordID" column contains Integers. Indexing a DataRowView returns an Object reference though. As such, you should be casting that as well:
    vb.net Code:
    1. Dim frm As FormA
    2.  
    3. frm = FormA
    4. x = CInt(DirectCast(frm.BindingSource1.Current, DataRowView)("RecordID"))
    Finally, you appear to be using the default instance of FormA. If so then what's the point of using a variable at all? You're just making the code harder to read. If you want to use the default instance then use the default instance directly:
    vb.net Code:
    1. x = CInt(DirectCast(FormA.BindingSource1.Current, DataRowView)("RecordID"))
    That code is going to do exactly as you'd expect, i.e. get the value of the "RecordID" column from the record currently selected in BindingSource1 on the default instance of FormA. If that's what you want then that's what you'll get. If that's not what you want then you'll need to tell us what you want. For instance, do you actually want to use the default instance? If you don't know what a default instance is and how it differs from the alternative, follow the Blog link in my signature and check out my post on the subject.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: How to get the BindingSource.Current of a different form?

    Thank you jmcilhinney. My question exposed that I'm learning VB.Net, and you've helped me out a lot with your answer. I really appreciate that you did more than just give me the code, you explained how I could do things better. I have much to learn...thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width