Skip to main content

Microsoft Silverlight

Answered Question Access User Control (Textbox) Used Multiple TimesRSS Feed

(0)

surrounded
surrounded

Member

Member

15 points

47 Posts

Access User Control (Textbox) Used Multiple Times

I have a textbox user control.  I add this control to a stack panel twice; need to get two different addresses.  When accessing the textbox value, how can I differentiate between the two textboxes.   They both have the same x:Name, and setting a Tag does not seem to work.  The tag is always null. 

If I loop through Layout.Children it only has one UIElement because the TextBox is all that's defined on Textbox.xaml.

 Thanks....

ccoombs
ccoombs

Contributor

Contributor

5168 points

758 Posts

Re: Access User Control (Textbox) Used Multiple Times

 you need to create a new instance of the control...

david.yanez.v
david.ya...

Member

Member

397 points

69 Posts

Re: Access User Control (Textbox) Used Multiple Times

Hi

you can modify the properties of  user controls using  Expression Blend ,  click on each textbox and change their name.
 

Regards 

please click answer if this solved your problem.

David !!!

surrounded
surrounded

Member

Member

15 points

47 Posts

Re: Access User Control (Textbox) Used Multiple Times

Do you mean the following:

DP.Textbox pFirstTextbox = new DP.Textbox();

DP.Textbox pTextbox = new DP.Textbox();

I am doing this already.  The logic for the textboxes is handled in Textbox.xaml.cs, but of course it is the same logic for both of them.

 OR

do you mean I need to create a new user control for the second texbox.

surrounded
surrounded

Member

Member

15 points

47 Posts

Re: Access User Control (Textbox) Used Multiple Times

David, there is only one textbox in the user control, so I cannot change the names in Expression Blend as there is only one name

ccoombs
ccoombs

Contributor

Contributor

5168 points

758 Posts

Re: Re: Access User Control (Textbox) Used Multiple Times

My fault; I thought you meant you were trying to reuse the same instance.

as i understand it, you've got a usercontrol which contains a textbox.  your usercontrol is added to a stack panel, and you need to be able to address the textbox contained in each usercontrol respectively.

 If you have two instances of your usercontrol (pFirstTextbox and pTextbox), then you have to just reference the textbox contained in those instances.  they'll both have the same name as defined in xaml (like you said), but they're tied to a specific instance.  So if your textbox's x:Name is "baseTextBox", then you just do:

pFirstTextbox.baseTextBox

pTextbox.baseTextBox

surrounded
surrounded

Member

Member

15 points

47 Posts

Re: Re: Access User Control (Textbox) Used Multiple Times

Thanks, but I'm still unclear how to access pFirstTextbox and pTextbox (which are defined in Page.xaml.cs) in Textbox.xaml.cs.  Textbox.xaml.cs contains the logic to interact with the textbox, such as MouseEnter, KeyUp, etc.

 

ccoombs
ccoombs

Contributor

Contributor

5168 points

758 Posts

Answered Question

Re: Re: Re: Access User Control (Textbox) Used Multiple Times

 you never said you wanted to manipulate one control from the other.  i'm still not even quite sure that's what you're trying to accomplish.  can you explain what you're trying to do?

if you're just trying to get one instance to react to the actions occuring in the other, i would suggest creating your own events for the user control, and wiring them up wherever your instances are defined (Page.xaml.cs).

surrounded
surrounded

Member

Member

15 points

47 Posts

Re: Re: Re: Access User Control (Textbox) Used Multiple Times

In Page.xaml.cs, I add one or more textboxes to a stack panel depending on a user selection.  Once the textboxes are added, the user then enters information in the textbox.  I had created a separate user control for a generic textbox (Textbox.xaml) to handle all this.  It's a TextBox, nothing special, just has all the event handling in Textbox.xaml.cs instead of Page.xaml.cs

So for example, the user may want to map a route between two street addresses.  The program then adds two textbox user controls to the stackpanel.   He/she would enter the first street address in one textbox, the second address in the next textbox.  The problem, as you can see, is how to differentiate between the two textboxes in the event handling (Textbox.xaml.cs) - as they are created with the same user control.  Thought there would be a way to do this, such as using the Tag property or something, but haven't figured it out. 

ccoombs
ccoombs

Contributor

Contributor

5168 points

758 Posts

Answered Question

Re: Re: Re: Re: Access User Control (Textbox) Used Multiple Times

your user controls are instanced.  the events only apply to 'this' instance.

instance 1 has no knowledge of instance 2, or any other instance for that matter.  imagine if you had 1000 instances of your control.  if you've got a keydown event wired up to your textbox, it will only fire for that single instance of your usercontrol.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities