PDA

Click to See Complete Forum and Search --> : user control


rene12358
Feb 4th, 2009, 09:16 AM
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

chris128
Feb 4th, 2009, 09:29 AM
Well first off i dont think you need to do any of that Convert.ToString stuff, just something simple like this should work:
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.

DeanMc
Feb 4th, 2009, 10:06 AM
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.

chris128
Feb 4th, 2009, 10:43 AM
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...)

DeanMc
Feb 4th, 2009, 10:48 AM
I presumed it was outside.

rene12358
Feb 4th, 2009, 11:32 AM
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

DeanMc
Feb 4th, 2009, 11:42 AM
Tell me do you want to keep adding it or onece its added to you want to just hide it?

rene12358
Feb 4th, 2009, 11:48 AM
Keep adding it. Cause the button is a add button. And I also have a remove button to remove it.

Thanks

DeanMc
Feb 4th, 2009, 12:06 PM
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.

rene12358
Feb 4th, 2009, 12:33 PM
I think I get it. But is there any solution to make the text not overlapping each other....?

Thanks

DeanMc
Feb 4th, 2009, 03:25 PM
What text?

rene12358
Feb 4th, 2009, 07:51 PM
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

chris128
Feb 5th, 2009, 07:07 AM
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

Hamish
Feb 5th, 2009, 08:25 AM
Wouldn't it be easier to just replace the Grid by a StackPanel?

chris128
Feb 5th, 2009, 08:35 AM
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

rene12358
Feb 5th, 2009, 11:53 AM
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

DeanMc
Feb 5th, 2009, 01:34 PM
The controls is not long enough I would imagine increase the height property!

chris128
Feb 5th, 2009, 05:36 PM
Can you do a screenshot and post the image so we can actually see what you mean?

rene12358
Feb 5th, 2009, 11:18 PM
Image1:
http://img21.imageshack.us/img21/9809/image1ux5.th.jpg (http://img21.imageshack.us/my.php?image=image1ux5.jpg)

Image2:
http://img261.imageshack.us/img261/7932/picture2ul8.th.jpg (http://img261.imageshack.us/my.php?image=picture2ul8.jpg)

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

DeanMc
Feb 6th, 2009, 04:03 AM
should it not be deleting the first text?

vbNeo
Feb 6th, 2009, 04:10 AM
Image1:
http://img21.imageshack.us/img21/9809/image1ux5.th.jpg (http://img21.imageshack.us/my.php?image=image1ux5.jpg)

Image2:
http://img261.imageshack.us/img261/7932/picture2ul8.th.jpg (http://img261.imageshack.us/my.php?image=picture2ul8.jpg)

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.

rene12358
Feb 6th, 2009, 11:22 AM
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/archive/2006/03/02/542641.aspx

Thanks

vbNeo
Feb 6th, 2009, 03:53 PM
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?

techgnome
Feb 6th, 2009, 04:27 PM
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

rene12358
Feb 6th, 2009, 10:34 PM
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

Hamish
Feb 7th, 2009, 05:04 AM
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?

rene12358
Feb 7th, 2009, 05:38 AM
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/archive/2006/03/02/542641.aspx

Thanks

Hamish
Feb 7th, 2009, 06:46 AM
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.