Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Create class from known control fails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vbtrying - 20 Aug 2007 20:50 GMT
I am trying to create a dynamic control, based on the
System.Windows.Form.Label control. The program compiles with no errors, but
errors out when trying to access this code:

       class MyLabels : Label
       {
       }

       public ucTime()
       {
           InitializeComponent();
           BuildLabels();
       }

       public void  BuildLabels()
       {
           int xLeft = 20;
           int xTop = 20;
           int xINC = 34;
           for (int i = 0; i < 7; i++)
           {
               MyLabels[] MyDates = new MyLabels[7];
               MyDates[i].Text = "Label " + Convert.ToString(i);
               MyDates[i].Width = 70;
               MyDates[i].Height = 25;
               MyDates[i].BorderStyle = BorderStyle.FixedSingle;
               MyDates[i].Left = xLeft;
               MyDates[i].Top = xTop;
               this.Controls.Add(MyDates[i]);
               xTop += xINC;
           }

       }

with this error:

"use the 'new' keyword to create an object instance, and it fails on the
first line where I'm trying to assign the "Text" value.

Any ideas?

Thanks in advance,
Tom Porterfield - 20 Aug 2007 21:01 GMT
> I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors, but
[quoted text clipped - 36 lines]
>
> Any ideas?

You have (at least) two problems.  First, you should create you array of
MyLabels outside of the loop as you only want to do that one time.
Second, while you have created an instance of an array of MyLabel
objects, you have not yet created the MyLabel objects.  So I would make
the first line of code inside the loop as follows:

MyDates[i] = new MyLabels();

That should get you going in the right direction.
Signature

Tom Porterfield

sloan - 20 Aug 2007 21:02 GMT
At the very least, you need to put

MyLabels[] MyDates = new MyLabels[7];

outside of your for (i) loop

>I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors,
[quoted text clipped - 39 lines]
>
> Thanks in advance,
Brad Wery - 20 Aug 2007 21:04 GMT
It looks like the array is initialized with with "non instantiated"
MyLabels. And I don't think you should be declaring the array in the loop.

Can you do this instead?

private void button3_Click(object sender, EventArgs e)
        {

            int xLeft = 20;
            int xTop = 20;
            int xINC = 34;
            for (int i = 0; i < 7; i++)
            {
                MyLabels MyDates = new MyLabels();
                MyDates.Text = "Label " + Convert.ToString(i);
                MyDates.Width = 70;
                MyDates.Height = 25;
                MyDates.BorderStyle = BorderStyle.FixedSingle;
                MyDates.Left = xLeft;
                MyDates.Top = xTop;
                this.Controls.Add(MyDates);
                xTop += xINC;
            }

        }

If not then intantiate the MyLabels class:

MyDates[i] = new MyLabels();
MyDates[i].Text...

Brad

> I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors, but
[quoted text clipped - 38 lines]
>
> Thanks in advance,

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.