Results 1 to 28 of 28

Thread: user control

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: user control

    Quote 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...)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: user control

    I presumed it was outside.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  7. #7
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: user control

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

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    Re: user control

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

    Thanks

  9. #9
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    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.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    Re: user control

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

    Thanks

  11. #11
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: user control

    What text?

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: user control

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

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  17. #17
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: user control

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

  18. #18
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: user control

    Can you do a screenshot and post the image so we can actually see what you mean?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  20. #20
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: user control

    should it not be deleting the first text?

  21. #21
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: user control

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

  22. #22

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  23. #23
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    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.

  24. #24
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  26. #26
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    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?

  27. #27

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    32

    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

  28. #28
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    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
  •  



Click Here to Expand Forum to Full Width