By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Create Label Via User Control Property Page

devonknows
100+
P: 137
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
Feb 2 '08 #1
Share this Question
Share on Google+
8 Replies


daniel aristidou
100+
P: 491
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

Expand|Select|Wrap|Line Numbers
  1. If Textbox.text = "" =false then
  2. New lbl as label
  3. lbl.position.x = 'Your positioning value
  4. lbl.position.y = 'your positioning value
  5. lbl.text = textbox.text
  6. End if
Feb 2 '08 #2

devonknows
100+
P: 137
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

Expand|Select|Wrap|Line Numbers
  1. If Textbox.text = "" =false then
  2. New lbl as label
  3. lbl.position.x = 'Your positioning value
  4. lbl.position.y = 'your positioning value
  5. lbl.text = textbox.text
  6. End if
Apologies i should mentions that this is for VB6, VB6 doesnt have the .position methods, i have tried
Expand|Select|Wrap|Line Numbers
  1.         Dim lbl As Label
  2.         lbl.Top = 100
  3.         lbl.Left = 100
  4.         lbl.Caption = TextBox.Text
  5.         lbl.Visible = True
  6.  
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
Feb 2 '08 #3

daniel aristidou
100+
P: 491
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
Feb 2 '08 #4

devonknows
100+
P: 137
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

Expand|Select|Wrap|Line Numbers
  1. Public Sub AddItem(strTemp As String)
  2.     Dim lbl As New Label
  3.     lbl.Top = 100
  4.     lbl.Left = 100
  5.     lbl.Caption = strTemp
  6.     lbl.Visible = True
  7. End Sub
  8.  
And on the Property page that is assigned to to the user control im using this
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     frmMain.WindowsTaskBox1.AddItem "TESTING"
  3. End Sub
  4.  
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
Feb 2 '08 #5

devonknows
100+
P: 137
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
Feb 7 '08 #6

P: 1
Devonknows:

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

Expand|Select|Wrap|Line Numbers
  1. ' General declarations:
  2. ' Create a new control to use as a label
  3. Private MyNewLabel As Control
  4. ' Create a variable that we will use to make sure no two labels have the same name
  5. Dim LabelCount as Integer
  6.  
  7. ' A buttons' click event:
  8. Private Sub Command1_Click()
  9.  
  10. ' 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.
  11. Set MyNewLabel = Me.Controls.Add("vb.label", "MyNewLabel" & LabelCount)
  12.  
  13. ' You MUST set visible to true if you want the new control to be visible
  14. MyNewLabel.Visible = True
  15. ' Set how far the label is from the left
  16. MyNewLabel.Left = 100
  17. ' Set how far the label is from the top
  18. MyNewLabel.Right = 100
  19. ' Finally, set the caption of the label
  20. MyNewLabel.Caption = "This is a new label!"
  21.  
  22. ' Increment the LabelCount variable up by one
  23. LabelCount = LabelCount + 1
  24.  
  25. ' End our sub
  26. End Sub
  27.  
I hope this helps, I registered just to post this, as I only learned how to do this a month ago.

See ya,
Kai
Feb 15 '08 #7

daniel aristidou
100+
P: 491
Thanks kai
Knew we where missing something
Feb 16 '08 #8

devonknows
100+
P: 137
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.Add.

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.Unfortunetly 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
Mar 14 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.