Create Label Via User Control Property Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devonknows
    New Member
    • Nov 2006
    • 137

    Create Label Via User Control Property Page

    Hi, I have a property page for my user control that has a text box and a button on it, when the user enters text into the text box and clicks the button i want a new label to appear on the user control but im unsure of how to do this, And i dont want to have to add loads of blank labels as that is rather impractical and would take up unecessary memory.

    If anyone could help i would be most appreciated
    Kind Regards
    Devon
  • daniel aristidou
    Contributor
    • Aug 2007
    • 494

    #2
    Originally posted by devonknows
    Hi, I have a property page for my user control that has a text box and a button on it, when the user enters text into the text box and clicks the button i want a new label to appear on the user control but im unsure of how to do this, And i dont want to have to add loads of blank labels as that is rather impractical and would take up unecessary memory.

    If anyone could help i would be most appreciated
    Kind Regards
    Devon
    Not sure if this will work...ive never tried creating a control
    From code...this might might be incorect...if it doesnt work...most likely the "New lbl" line needs to be changed slightly

    [CODE=vb]If Textbox.text = "" =false then
    New lbl as label
    lbl.position.x = 'Your positioning value
    lbl.position.y = 'your positioning value
    lbl.text = textbox.text
    End if[/CODE]

    Comment

    • devonknows
      New Member
      • Nov 2006
      • 137

      #3
      Originally posted by daniel aristidou
      Not sure if this will work...ive never tried creating a control
      From code...this might might be incorect...if it doesnt work...most likely the "New lbl" line needs to be changed slightly

      [CODE=vb]If Textbox.text = "" =false then
      New lbl as label
      lbl.position.x = 'Your positioning value
      lbl.position.y = 'your positioning value
      lbl.text = textbox.text
      End if[/CODE]
      Apologies i should mentions that this is for VB6, VB6 doesnt have the .position methods, i have tried
      [CODE=vb]
      Dim lbl As Label
      lbl.Top = 100
      lbl.Left = 100
      lbl.Caption = TextBox.Text
      lbl.Visible = True
      [/CODE]
      but all i get is an error saying
      "Object Variable or With Block Variable Not Set"

      Hope this helps you to help me
      Kind Regards
      Devon

      Comment

      • daniel aristidou
        Contributor
        • Aug 2007
        • 494

        #4
        Could you tell be where exactly...the debugger stops on the code...
        also change dim lbl as labale to dim xlbl as new label
        lbl by itself might cause error.....try xlbl

        Comment

        • devonknows
          New Member
          • Nov 2006
          • 137

          #5
          Originally posted by daniel aristidou
          Could you tell be where exactly...the debugger stops on the code...
          also change dim lbl as labale to dim xlbl as new label
          lbl by itself might cause error.....try xlbl
          Ok, The subroutine im using on the UserControl is this

          [CODE=vb]
          Public Sub AddItem(strTemp As String)
          Dim lbl As New Label
          lbl.Top = 100
          lbl.Left = 100
          lbl.Caption = strTemp
          lbl.Visible = True
          End Sub
          [/CODE]

          And on the Property page that is assigned to to the user control im using this
          [CODE=vb]
          Private Sub Command1_Click( )
          frmMain.Windows TaskBox1.AddIte m "TESTING"
          End Sub
          [/CODE]
          But im getting the error message when Command1_Click( ) is called.
          "An Instance of frmMain cannot be created as its designer window is still open"

          Apologies in advance, this is my first user control so still getting used to alot of the commands and such

          And when i put WindowsTaskBox1 .Additem "TESTING" in the frmMain_Load() Method i get.
          "Invalid Use of New Keyword" referring to "Dim lbl As New Label"

          Kind Regards
          Devon

          Comment

          • devonknows
            New Member
            • Nov 2006
            • 137

            #6
            Can no-one help me in this matter? Adding a label to a usercontrol during design time via the UserControl property page? if anyone can help id be most appreciated.

            Example. When you highlight your user control that has been placed on your form, and click "Custom.." in your properties window, you get the properties window that you have assigned (Custom or VB built in) I have a text box and a command button, so when the user clicks the command button it automatically adds a label to the user control whilst still in design mode.

            Kind Regards
            Devon

            Comment

            • Kai001
              New Member
              • Feb 2008
              • 1

              #7
              Devonknows:

              Here's some simple code for VB6 to create and place a new label.

              Code:
              ' General declarations:
              ' Create a new control to use as a label
              Private MyNewLabel As Control
              ' Create a variable that we will use to make sure no two labels have the same name
              Dim LabelCount as Integer
              
              ' A buttons' click event:
              Private Sub Command1_Click()
              
              ' The following function is adding a new label to the form this code is on. The functions' parameters are as follows: Add( The type of control, The control's new name). We add the LabelCount variable to the end of it in order to make sure we have no controls with duplicate names.
              Set MyNewLabel = Me.Controls.Add("vb.label", "MyNewLabel" & LabelCount)
              
              ' You MUST set visible to true if you want the new control to be visible
              MyNewLabel.Visible = True
              ' Set how far the label is from the left
              MyNewLabel.Left = 100
              ' Set how far the label is from the top
              MyNewLabel.Right = 100
              ' Finally, set the caption of the label
              MyNewLabel.Caption = "This is a new label!"
              
              ' Increment the LabelCount variable up by one
              LabelCount = LabelCount + 1
              
              ' End our sub
              End Sub
              I hope this helps, I registered just to post this, as I only learned how to do this a month ago.

              See ya,
              Kai

              Comment

              • daniel aristidou
                Contributor
                • Aug 2007
                • 494

                #8
                Thanks kai
                Knew we where missing something

                Comment

                • devonknows
                  New Member
                  • Nov 2006
                  • 137

                  #9
                  Originally posted by daniel aristidou
                  Thanks kai
                  Knew we where missing something
                  When i add this to the property page for the user control, and then access the property page when i click Custom on the properties for my user control i get

                  'Method or Data Member not Found' and it highlights 'Me.Controls.Ad d.

                  im not sure if i mentioned earlier that i want the control to add during design time, for example the toolbar control, when you click add button, the toolbar control on the form automatically adds a button when you click ok/apply

                  ive looked at the code behind the toolbars and stuff and ive found that it creates like a class for a button, i was jus wondering if i need to do the same with a label, and if i created a label class, what information would i need in it. and how would i get it to add an instance of it to the user control on my form.Unfortunet ly ive looked everywere i can think of to find information on this but cant seem to find any information. So anyone that has created a custom toolbar control, or a status bar control or anything along them lines, any helped would be appreciated

                  Kind Regards
                  Devon

                  Comment

                  Working...