• 0

[VB.NET] How to reference object from Panel


Question

I have too panels in my program, one of them has a list on it, and I want to reference the list from the other panel. To be more specific, my list is printersList in panel goPanel, and I want panel goPanel to reference it. I get the the error: "Reference to a non-shared member requires an object reference." I've done a lot of research on this error, for about the past hour, but have not been able to make sense out of it, can anyone point me in the right direction?

Thanks

25 answers to this question

Recommended Posts

  • 0

Not sure how I could show it in a screenshot, but basically:

- I have 2 user control forms, startPanel and goPanel

- I have a check list box (printersList) on startPanel, that I want to reference on goPanel, so I can list the things checked in the check box.

- When I try to do startPanel.printersList.CheckedItems, it says "Reference to a non-shared member requires an object reference."

  • 0

I think you have to loop through the controls in the panel, finding one of the right type (cast it as the type of control you're looking for) and then reference it that way.

That or provide a public accessor in the form that returns the control.

  • 0

Winston, it is a user control that is reference by a panel, so the short answer would be that it's a user control.

John, how would I go about doing that? That is what MSDN has said, but I can't get it working.

This is what MSDN says for the error I am getting:

  Quote
You have referenced a non-shared member within your code and failed to supply an object reference. You cannot use the class name itself to qualify a member that is not shared. The instance must first be declared as an object variable and then referenced by the variable name.

Error ID: BC30469

To correct this error

Declare the instance as an object variable.

Reference the instance by the variable name.

I'm not sure how to do this, can someone describe generally how I would set this up?

  • 0

you have to declare the list as public or friend, if u just dragged and dropped it on the form is going to be declared private and not accessible by by any other object than the one that instanciated it. Thats why you get a bad reference. John proposed an even better idea (and the correct OO way) of providing a public property or method to access the control while still keeping the control private.

  • 0

yea, that's what MSDN said to do (John's way), not sure how to go about that though. The checkedlistbox's name is printersList in user control startPanel, so when I try Dim printersList2 As CheckedListBox, and set printersList2 = startPanel.printersList, that doesn't work. Am I going about this wrong?

Edited by JK1150
  • 0

ok scratch what I said up there, new objects are declared as friends. If these are all panels on the same form you should be able to loop through. Can you give a more specific example of that you are trying to do? do you want to show the same list twice? or are you using the values from printerslist to modify another list on the form? also is this winform or webform? aslo can you post a screenie of what you are trying to do (just disable the reference line of code). Visuals help demonstrate your desired behavior

  • 0

i'm not sure what i could take a screenshot of, but I have a mainFrame (Windows App) with a panel in it. The program starts by loading a user control into the Panel, which includes printersList, a list of printers on a network that can be installed. The user will click start, which will change the panel to the user control goPanel with a progress bar, cancel button, etc., which will use what was checked on the checked list in startPanel to install the selected printers. This is a winform. Is there a specific part of this you want to see?

  • 0

It sounds to me like you need to have your user control expose the checked list box as a public property. That way other user controls have access to it.

In your code for your goPanel control, you'd have something like this (this is C#, hopefully you can translate it if you need to)

class MyGoPanelControl : UserControl

{

// Next line probably added by your designer automatically

private CheckedListBox printersList;

public CheckedListBox PrintersList

{

get { return printersList; }

}

}

then, anything that can access an instance of MyGoPanelControl, can access the printersList control inside of it via the property.

Is that what you were looking for?

  • 0

I would be very surprised if you could not returna checkedlistbox in visual basic .net. Its just a class like anything else. It just happens to also have a visual representation.

I ran my code through a converter to get it to VB.NET:

Class MyGoPanelControl

Inherits UserControl

' Next line probably added by your designer automatically

Private printersList As CheckedListBox

Public ReadOnly Property PrintersList() As CheckedListBox

Get

Return printersList

End Get

End Property

End Class

  • 0

Ok, i added a new user control, added a panel into the user control, dropped a checkedlistbox into the panel.

Now i added the new user control to my form, and added a button which adds new items to the checkedlistbox.

Check out the attachment to see if that's what you're after.

  • 0

If you put that button on another user control rather than in the main frame, that is basically what I need to know how to do.

(I have the users select printers from a list, click go, and then a progress screen will come up where the "goPanel" class will need to know which printers the user selected in the "startPanel" class)

Thanks for all your help so far, VB.NET certainly is an interesting learning experience.

Edited by JK1150
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I can tell you I don't need one. I use Start11 to make it Win10 styled and custom game tiles so it's essentially an easier way to launch games than opening Steam but I definitely don't 'need' it and don't use it to launch any applications.
    • I was replying to a user comment that literally started with "So, user error then."
    • Is the Start menu in Windows actually useful? Do we even need it? by Usama Jawad A few days ago, I shared my viewpoint regarding how Microsoft hasn't been able to convince me to migrate to Windows 11 as my sole daily driver, despite the OS being available for roughly four years. Within that piece, I highlighted the fact that even though the Start menu has been completely revamped in the latest version of Windows, it's not something that I use regularly. And that got me thinking: is the Start menu really a crucial piece of my Windows experience? I get it, I know that the Start menu is an iconic Windows element that has existed in the operating system for as long as many of us can remember. And yet, I can't really tell you about the last time I actually used the Start menu. The reason for italicizing the word "used" above is that while the Start menu can be launched when you click on the Windows icon in the Taskbar or the Windows key, I am not counting that as actual usage for the purpose of this piece. This is because I do launch the Start menu several times a day, I actually use it to get to Windows Search. I don't actually use the list of apps, app folders, and recently opened files. I simply tap the Windows key and start typing the search query for the item that I am looking for. For example, if I want to open Power BI, I don't open the Start menu and find the pinned app. I just tap on the Windows key and start typing "Po..." and quickly hit Enter as soon as the right option comes up, which is usually after a couple of keystrokes. Similarly, if I want to open "Gifts.xlsx", I don't open Excel from the Start menu or even peruse my list of recently opened files, I just start typing "Gi..." and press Enter. I feel like this is a much faster experience and you don't need to rely on muscle memory to find the item that you are looking for. And as your list of installed software grows, it only becomes more tedious to keep track of where you pinned an app or the folder that you kept it in. Windows Search gets rid of that problem completely. Additionally, the Start menu detracts me from whatever else I may be focusing on in parallel. If I am using Word and now want to open PowerPoint, I would probably get distracted by the Start menu as I launch it and try to locate the PowerPoint icon. Instead, I can just get to PowerPoint within a few keystrokes without even realizing the effort involved. I'd also like to highlight that I am not inherently against the idea of pinning stuff that you use frequently or recently, but I do think that the Taskbar and Desktop view already solves that problem to some extent. Right now, my Taskbar contains my most used software, while my desktop contains other secondary software and files. I understand that people have their fair share of complaints against Windows Search, but it works quite well for my use-cases. As it currently stands, my actual usage of the Start menu is nil. And although this popular Windows UX is always the subject of debate each time Microsoft makes even a minor change to it, I simply couldn't care less. That said, while the Start menu in its traditional use is dead to me, I am not sure how many people actively utilize this UX. Any evidence I have is purely anecdotal, but I would be very interested in seeing the telemetry that Microsoft tracks regarding its usage. Seeing how frequently Microsoft makes changes to the Start menu and the ensuing debate that follows, I feel like I am in the minority. But I just can't imagine opening the Start menu and then relying on muscle memory or a fixed grid of icons to get to the app I need, when I can do more with a couple of keystrokes.
    • >Fixed: Certain displays might be unexpectedly green. I believe that this fixed the Windows mishandling Dolby Vision issue in RGB 12 bit Full mode we saw recently.
  • Recent Achievements

    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
    • Week One Done
      Karan Khanna earned a badge
      Week One Done
    • First Post
      MikeK13 earned a badge
      First Post
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      681
    2. 2
      ATLien_0
      277
    3. 3
      Michael Scrip
      208
    4. 4
      +FloatingFatMan
      172
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!