Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Create Instance of Existing Object via Casting

Posted on 2014-03-02
6
Medium Priority
?
209 Views
Last Modified: 2014-03-03
I am creating a DataGridView dynamically at run-time, and cant seem to be able to instantiate it as an object during casting in another (Form1) subroutine, so that I can change row labels.


Here is how I define the DataGridView in the Form1 Load procedure:


Dim DataGridView1 As New DataGridView

Open in new window


Later, in a subroutine, when I want to change the row labels (numrows is the number of rows), I can't get the cast to work correctly because the cast won't allow me to use "As New" -- as follows:

Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)
For i = 1 To numrows
        DataGridview1.Columns.Add(i, "V" & i)
Next

Open in new window



What is the correct way instantiate as an object a control that was spawned dynamically?
0
Comment
Question by:lep1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900042
what error u got ?
0
 

Author Comment

by:lep1
ID: 39900177
The error is that DataGridView1 is Nothing
0
 
LVL 19

Accepted Solution

by:
Melih SARICA earned 1000 total points
ID: 39900217
try

me.controls.find("datagridview1")
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:lep1
ID: 39900343
It worked out that the the syntax

Dim DataGridView1 As New DataGridView

does not add the name "DataGridView1" to the control, so therefore it can't be found in Me.Controls().

I had to use:

DataGridView1.Name="DataGridView1" -->which corrected the problem of not finding the control.

Question: is there a difference between

Dim DataGridView1 As DataGridView

and

Dim DataGridView1 As New DataGridView

in terms of the Name property?
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 39900379
Dim DataGridview1 As DataGridView = CType(Me.Controls("DataGridView1"), DataGridView)

in this line, ur trying to find a control that is already on the page ? or trying to add a new one ?

I guess ur trying to find a newly created one..

so u ave to set a name, other wise ssytem ll gave a name automaticly.
0
 

Author Comment

by:lep1
ID: 39900396
ok, thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question