C# - Creating form objects from a base control?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gun Slinger
    New Member
    • Feb 2008
    • 27

    C# - Creating form objects from a base control?

    Hi i just have a quick question.

    I ran a search but i was not sure really how to word it correctly and couldnt find anything.

    I would like to know how I can create a form object like a textbox, label or combo box from the one variable?

    Obviously for a label it is easy by itself, i have just been doing..

    public System.Windows. Forms.Label[,] obj_field_contr ol = new System.Windows. Forms.Label[19, 9];

    but now i want to have this public declaration as a variant type array that allows me to create different objects depending on what i assign it. Is this only possible through inheritence?

    I want to make it

    public System.Windows. Forms.VariantOb jectThing[,] obj_field_contr ol = new System.Windows. Forms.VariantOb jectThing[19, 9];

    and then simply be able to do

    obj_field_contr ol[0, 0] = new System.Windows. Forms.TextBox() ;
    obj_field_contr ol[0, 1] = new System.Windows. Forms.Label();
    obj_field_contr ol[0, 2] = new System.Windows. Forms.ComboBox( );

    I would appreciate any help :)

    Thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I am moving this to the .NET forum.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Hmm.
      You might be able to get away with using an array of Control[,] and then assigning the various index positions to be a TextBox or Label or whatever.
      There is also a ControlCollecti on object that you can .Add() to?
      Although you might consider trying to use a more generic approach and using like LinkedList (LinkedList<Con trol>) or ArrayList.

      Comment

      • Gun Slinger
        New Member
        • Feb 2008
        • 27

        #4
        Hi thanks for the replys guys. Also sorry, i thought i did post it in the .net forum.

        You suggested using the .control fuction correct? i have actually tried this before, but maybe my method was incorrect...

        i have tried:

        public System.Windows. Forms.Control[,] obj_field_contr ol = new System.Windows. Forms.ComboBox[19, 9];

        Just to see if any of of the combobox controls would actually work. However i get an error saying that the .item is not a recognised command for a control.

        I have also tried:

        public System.Windows. Forms.Control[,] obj_field_contr ol = new System.Windows. Forms.Control[19, 9];

        obj_field_contr ol[0, 1] = new System.Windows. Forms.TextBox() ;

        But it gave my the same kind of problem, as it only gives a generic amount of commands for the control.



        That control collection add sounds like a good idea, but after i have added the object, how do i actually reference it?

        obj_field_contr ol[0, 1].Add(new System.Windows. Forms.TextBox() );

        What would i make this so i can reference the location of the textbox?

        obj_field_contr ol[0, 1].Location

        EDIT

        I have found that if i write the syntax like this:

        obj_field_contr ol[0, 1][0].Location

        It works, however i find that the [0] is a bit of a pain as it will remove some of the efficiency of my program. Any ideas?

        EDIT 2:

        After trying it out a little bit i have still found that the control collection will not allow me to use properties that are uncommon with all objects. The .Item will still not work. Any suggestions?

        Comment

        • Gun Slinger
          New Member
          • Feb 2008
          • 27

          #5
          Thanks for the help.

          I managed to fix it myself. I just ended up using a Control and then type casting the controls like (TextBox). Works like a charm. Thanks

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Yeah after your other post I was going to say you need to remember to cast the control back to it's correct type to get all the extra properties and such.

            Comment

            Working...