# .NET and More > WPF, WCF, WF >  user control

## rene12358

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

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.

----------


## 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.

----------


## chris128

> 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

I presumed it was outside.

----------


## rene12358

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

Tell me do you want to keep adding it or onece its added to you want to just hide it?

----------


## rene12358

Keep adding it. Cause the button is a add button. And I also have a remove button to remove it.

Thanks

----------


## DeanMc

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

I think I get it. But is there any solution to make the text not overlapping each other....?

Thanks

----------


## DeanMc

What text?

----------


## rene12358

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

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

Wouldn't it be easier to just replace the Grid by a StackPanel?

----------


## chris128

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

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

The controls is not long enough I would imagine increase the height property!

----------


## chris128

Can you do a screenshot and post the image so we can actually see what you mean?

----------


## 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

----------


## DeanMc

should it not be deleting the first text?

----------


## vbNeo

> 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.

----------


## rene12358

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

----------


## vbNeo

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

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

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

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

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

----------


## Hamish

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.

----------

