-
Feb 4th, 2009, 09:16 AM
#1
Thread Starter
Member
user control
Hi,
I created a user control that contains a label. I had a textbox and button in my application too. Whatever the user text in the textbox and click on the button, the text will appear in the label. Anyone know how to do it...? I'm using VB code.
Below is the coding I'd done. But it can only read the text from the textbox once. And the user control can only appear once. How can I do it in order to make the user control appear as many times as when the user click on the button...? I'm using WPF application.
Dim user As New UserControl3
If TxtBox.Text <> "" Then
Dim test As String
test = Convert.ToString(TxtBox.Text)
testGrid.Children.Add(user)
user.textLabel.Content = test
user.Visibility = Windows.Visibility.Visible
Thanks
-
Feb 4th, 2009, 09:29 AM
#2
Re: user control
Well first off i dont think you need to do any of that Convert.ToString stuff, just something simple like this should work:
vb Code:
user.textLabel.Content = TxtBox.Text
As for the usercontrol only working once, that could be due to the fact that you are not specifying a new location for the control so when you keep adding it to your testGrid grid it might just get stuck in the same place each time. I dunno, just a thought.
-
Feb 4th, 2009, 10:06 AM
#3
Frenzied Member
Re: user control
I see you are adding it ok but what about removing it? how are you doing that? If you don't remove it you cant re add it.
-
Feb 4th, 2009, 10:43 AM
#4
Re: user control
Originally Posted by DeanMc
I see you are adding it ok but what about removing it? how are you doing that? If you don't remove it you cant re add it.
Well you can add it again because its a new instance that is being added (assuming the "Dim user As New UserControl3" is actually in the button click event too anyway...)
-
Feb 4th, 2009, 10:48 AM
#5
Frenzied Member
Re: user control
I presumed it was outside.
-
Feb 4th, 2009, 11:32 AM
#6
Thread Starter
Member
Re: user control
DeanMc was right. I declared the usercontrol outside button click event. It should be declared inside button click event...?
Do I need to do a loop to re-add it...? Where should I put the remove coding...? Is there any example...? I really have no idea.
Thanks
-
Feb 4th, 2009, 11:42 AM
#7
Frenzied Member
Re: user control
Tell me do you want to keep adding it or onece its added to you want to just hide it?
-
Feb 4th, 2009, 11:48 AM
#8
Thread Starter
Member
Re: user control
Keep adding it. Cause the button is a add button. And I also have a remove button to remove it.
Thanks
-
Feb 4th, 2009, 12:06 PM
#9
Frenzied Member
Re: user control
OK first thing you need to do is before you add your user control you need to see if it exist already and if it does call a remove method on it. so before this line:
testGrid.Children.Add(user)
you do an:
IF testGrid.Children.Contains(user) Then
testGrid.Childern.Remove(User)
EndIf
to check if the control is added to the grid already. Regarding your remove button just use the same code but use IF NOT remember this is important as you cannot remove something that is not there.
-
Feb 4th, 2009, 12:33 PM
#10
Thread Starter
Member
Re: user control
I think I get it. But is there any solution to make the text not overlapping each other....?
Thanks
-
Feb 4th, 2009, 03:25 PM
#11
Frenzied Member
-
Feb 4th, 2009, 07:51 PM
#12
Thread Starter
Member
Re: user control
The textLabel, or in fact the user control itself.
Cause when I type "text" in the textbox and click add button, the textLabel containing "text" will appear. When I type "text1" in the textbox and click add button again, the textLabel containing "text1" will overlap the textLabel containing "text".Or in fact, it appears on top of the textLabel containing "text". So is there any solution to make it not overlapping...?
Thanks
-
Feb 5th, 2009, 07:07 AM
#13
Re: user control
Thats why you need to set a different location/margin for each one like I mentioned in post #2.
You could use a variable to keep track of the location of the last instance of this control you placed on the grid and then each time you add a new one just update that variable by adding a certain amount to it, then set the new instance to use that variable as its location/margin.
hope that helps
-
Feb 5th, 2009, 08:25 AM
#14
Hyperactive Member
Re: user control
Wouldn't it be easier to just replace the Grid by a StackPanel?
-
Feb 5th, 2009, 08:35 AM
#15
Re: user control
You are probably right, I've not really used the stackpanel much so I dont know how well it reacts to controls being added/removed on the fly but I imagine it would be well suited to this scenario as you say
-
Feb 5th, 2009, 11:53 AM
#16
Thread Starter
Member
Re: user control
But when I added my usercontrol to the stackpanel, the textLabel does not appear when the user click on the button 2nd time.
It is the same coding as I tried on the grid. And just change the grid to stack panel. It doesn't work for me. Anyone have any suggestion...?
Thanks
-
Feb 5th, 2009, 01:34 PM
#17
Frenzied Member
Re: user control
The controls is not long enough I would imagine increase the height property!
-
Feb 5th, 2009, 05:36 PM
#18
Re: user control
Can you do a screenshot and post the image so we can actually see what you mean?
-
Feb 5th, 2009, 11:18 PM
#19
Thread Starter
Member
Re: user control
Image1:
Image2:
When the user type in the textbox and click the button the second time, the text will overlap each other. I tried to adjust the margin but it does not work.
Thanks
-
Feb 6th, 2009, 04:03 AM
#20
Frenzied Member
Re: user control
should it not be deleting the first text?
-
Feb 6th, 2009, 04:10 AM
#21
Frenzied Member
Re: user control
Originally Posted by rene12358
Image1:
Image2:
When the user type in the textbox and click the button the second time, the text will overlap each other. I tried to adjust the margin but it does not work.
Thanks
I'm not entirely sure I understand your post - but I assume you want to add elements to a Grid, as in a container.
First of all, you might want to consider just using a listbox with an itemtemplate instead, as this will make everything much easier for you. If you do insist on using a Grid, you need to define the rows and columns of the grid - and set the rows and columns of each control you add to it.
When you just add items to a Grid without specifing in what row/column they should be rendered the standard value for both will be 0. Hence they will be rendered in Row 0, Column 0, and as a result they get rendered on top of each other in the order that xaml is being parsed.
I think that might be the problem you're having right now.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Feb 6th, 2009, 11:22 AM
#22
Thread Starter
Member
Re: user control
It works with stackpanel. I didnt know I need to do the declaration in VS. But I have another problem.
How can I declare one stackpanel as a different name each time the user type the text in textbox...? Cause I'm going to do drag and drop for each of the textLabel.
Below is the coding I use for drag and drop:
http://blogs.msdn.com/marcelolr/arch...02/542641.aspx
Thanks
-
Feb 6th, 2009, 03:53 PM
#23
Frenzied Member
Re: user control
That confirms what I thought was the problem, the stackpanel automatically ensures items aren't rendered on top of each other. Uh - why would you need to instantiate a new Stackpanel each time?
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Feb 6th, 2009, 04:27 PM
#24
Re: user control
I think you misunderstood the recomendation of the stackpanel....
In design, plop down the stack panel...
Then in code, each time you need to... create the label, set its text, and add it to the stackpanel... the stackpanel should be your container, and there should only be the one. Adding the labels to it will cause the next one to float underneath the previous one, there by "stacking" them...
-tg
-
Feb 6th, 2009, 10:34 PM
#25
Thread Starter
Member
Re: user control
Ok...I understood what coding to add. But I not sure where to put the coding. When the user click once on the ok button, the textLabel will appear. When the user click on the second time, another textLabel will appear. I know how to make the textLabel appear. But I dunno how to check if the user click once, twice or thrice on the button...? What coding must I add...?
Thanks
-
Feb 7th, 2009, 05:04 AM
#26
Hyperactive Member
Re: user control
Why do you need to know how often the user has clicked the button? With a StackPanel, any control you add will automatically be added beneath the existing controls, so you don't need it to place the button. Any other reason?
-
Feb 7th, 2009, 05:38 AM
#27
Thread Starter
Member
Re: user control
I need to drag and drop the textlabel. For example, when the user type "test" in the textbox for the first time, the textlabel for "test" will be able to drag and drop. And its the same for the 2nd, 3rd and fourth time.
The website I use for drag and drop is this:
http://blogs.msdn.com/marcelolr/arch...02/542641.aspx
Thanks
-
Feb 7th, 2009, 06:46 AM
#28
Hyperactive Member
Re: user control
I still don't get it. What has being able to drag/drop a Label to do with how many times a user has clicked a Button?
If you can show us the code you have so far, maybe we understand what you want to do a little better.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|