Results 1 to 3 of 3

Thread: VB 2026 trying to access form controls as part of a class?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    123

    VB 2026 trying to access form controls as part of a class?

    Using VB 2026.

    So I can't be sure this worked in 2022 version, but I thought you could do something like:
    Code:
    Private Class MyClass
        Public Yo as String
        Public lbName as Label
        Public cmbChoice as ComboBox
    End Class
    
    ... (button click, can't be bothered to type it out)
      Dim myVar as New MyClass
      myVar.Yo = "Hey there, you beautiful code."
      myVar.lbName = Label1
      myVar.cmbChoice = ComboBox1
    ....
    This seems to work just fine for Yo and lbName, but... MyVar.cmbChoice throws an error: Value of type 'ComboBox' cannot be converted to 'VisualStyleElement.ComboBox'.

    Obviously I plan to make an array or list of these as there's going to be lots of them, with multiple controls needing to be assigned to one variable so they can all be altered or read with respect to that item. So I'd like a variable class that just points to the controls instead of trying to make control arrays and accessing them all that way. I think it just makes it clearer what's going on, why they're being recorded that way, and so on. Would this work in VB 2022? If so... I may just go back to it rather than trying to figure out this mess.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: VB 2026 trying to access form controls as part of a class?

    Regarding your actual issue, for whatever reason the ComboBox type used in MyClass is referencing VisualStyleElement instead of System.Windows.Forms. This can be fixed easily by fully qualifying the name:
    Code:
    Public cmbChoice as System.Windows.Forms.ComboBox
    Alternatively, if the Visual Studio autocomplete incorrectly imported VisualStyleElement, you can replace it instead:
    Code:
    'Imports VisualStyleElement ' Remove this line
    Imports System.Windows.Forms ' Add this line
    Private Class MyClass
        Public Yo as String
        Public lbName as Label
        Public cmbChoice as ComboBox
    End Class
    Regarding what you're actually wanting to do:
    with multiple controls needing to be assigned to one variable so they can all be altered or read with respect to that item
    There's probably a better way of doing this, such as the built-in data binding feature, but to be honest I'd need a better example of what it is that you're trying to do in order to give you an accurate example. Not that you didn't give us a lot of information, I just don't fully understand the expected end result is all.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    123

    Re: VB 2026 trying to access form controls as part of a class?

    Quote Originally Posted by dday9 View Post
    There's probably a better way of doing this, such as the built-in data binding feature, but to be honest I'd need a better example of what it is that you're trying to do in order to give you an accurate example. Not that you didn't give us a lot of information, I just don't fully understand the expected end result is all.
    So initially I wanted to use the "details" version of a listbox, because I have a list of lots and lots of items over which a bunch of options (all the same ones) can be selected. But I couldn't find an easy way to incorporate a combo box (or, maybe, a set of radio buttons since each combo box will have at most 5 options) into a cell of that. Then I looked at a Datagrid and... it was, tbh, kinda ugly looking. And adding boxes wasn't gonna be easy, there, either.

    My programming abilities are... quite limited (though I try to learn more, slowly), and this isn't something that "needs" to happen, it's purely for my own use and just for fun (it's about having an easier way to make these selections for a game). Thus my current solution attempt is to have a bunch of controls on screen and a scrollbar, then just change the contents of the various controls (a label, three combo boxes, two text boxes) to whatever the appropriate data is for where they all should be as the scrollbar moves, allowing change by the user afterwards and updating the data (boolean switch so changing via code doesn't trigger change in the data, just to be safe). It's not as smooth as the listbox or data grid, but seems a bit prettier than the data-grid.

    As an example, suppose it's a list of Veggies. Each veggie is named (label) and is by default not included at all, but is otherwise in the salad, grilled, or both (first combo box/radio button set, including 'none' which is the default). If in just one, there's an option for little, some, moderate, lots, or tons (second combo box/radio button set), along with a notes section (first textbox), and if in both a second option set and notes section open up (become active) to deal with that.

Tags for this Thread

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