1

I have a Listview control in a User Control and I want to access the selected item(s) from Form1. Even if I declare it as a public variable I cannot access it from any other forms.

The following code line would be in Form1:

   Dim Index As Integer = UserControl1.Listview1.SelectedItems(0).Index

The issue is I cannot even get the "Listview1" intellisense to pop up. Is it possible to do that?

2 Answers 2

1

You can create new module and declare a variable as public for your usercontrol and type this into

public myLV as UserControl1.Listview1

So you can change your code with

Dim Index As Integer = myLV.SelectedItems(0).Index
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by default in VB.Net, but you've mentioned other forms in the description. Which form is the UserControl on?...and which form are you trying to access that UserControl from?

If attempting to access across forms, then how are you getting a reference to the other form? I suspect you simply have a wrong reference and are accessing an empty ListView from an invisible form (created but not displayed).

Show us some code where you're attempting this...

7 Comments

What do you mean which form is the UserControl on ... I created it by creating a new item > UserControl ... There is no control "UserControl" to add into my main form. The code I would of paste is "UserControl1.Listview1" ... Yet no Listview1 pops up in the intellisence.
You need an INSTANCE of your Usercontrol, so either add one to your Form (re-build first and it will be at the top of your ToolBox), or create one in code: Dim uc1 As New UserControl1 ... uc1.ListView1.Items You'll be able to see the ListView in the reference to an actual instance of your UserControl.
If your UserControl is called "UserControl1" and you added one to your form, then the default name would be "UserControl11" (note there two ones at the end; the IDE adds a number after the name). So your code snippet would need to be: Dim Index As Integer = UserControl11.Listview1.SelectedItems(0).Index
I'm trying to access my UserControl1 controls (Listview, textboxes, labels etc) from my MainForm.vb. So I'm trying to write the same code you wrote to access my selected items in my UserControl1 listview...
I understand that. Have you dragged the custom UserControl from the ToolBox to your form?...or are you creating it in code? The UserControl by itself is like a cookie cutter. It doesn't have anything in it until you press it into the dough. Adding the UserControl to the form, or creating it via code with the "New" keyword is what makes it a tangible "cookie" that you can work with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.