tags:

views:

703

answers:

1

Sorry for the basic questions, but I've been searching the web for a number of days and can't find the answer to these questions.

I've created a Custom Control, and I will be placing a large number of instances of that Custom Control on my xaml page. In working with that Custom Control in the VB Code Behind, how do I do the following?

  1. How do I reference the name of the Custom Control (in my VB code) which was clicked with the MouseLeftButtonDown event? For example, if I have 10 instances of my Custom Control in xaml, each with a different x:name (say 1-10), when a particular instance is clicked, how can I see which one was clicked? I've tried a number of things including e.OriginalSource.Name (which returns the component within the control which was clicked and not the name of the instance of the control).

  2. My Custom Control consists of numerous parts and pieces (Rectangles, Lines, Text, etc). Each of these items is a part of my layer. In VB code, once I can reference a particular Control, how can I hide or change certain parts of that control (such as hiding a Line, and changing the text). Also, I need to modify more than just the control which was clicked, so I need to be able to access properties of all of the controls, not just what was clicked. For example, if I click Control instance Test1, I also need to modify Test2, Test3, and Test5 in some way.

Here is some test code I through together as part of a Silverlight project using MS Blend 2. My control is much larger, and I need 200 - 250 instances/copies of that custom control, so I really need to know which control instance/copy was clicked.

My UserControl:

<Grid x:Name="LayoutRoot" MouseLeftButtonDown="OnMouseClick">
    <Rectangle x:Name="Rectangle1" Fill="#FFFFFFFF" Stroke="#FF000000"/>
    <TextBox Background="{x:Null}" x:Name="TextBox1" Text="Test" TextWrapping="Wrap"/>
    <Ellipse x:Name="Circle1" Fill="{x:Null}" Stroke="#FF000000"/>
    <Path Margin="1,29,0,29" x:Name="Line1" Fill="{x:Null}" Stretch="Fill" Stroke="#FF000000" Data="M74,80 L132,80"/>
    <Path Margin="0,0,1,14" x:Name="Line2" VerticalAlignment="Bottom" Height="1" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M73,95 L131,95"/>
    <Path Margin="0,0,0,4" x:Name="Line3" VerticalAlignment="Bottom" Height="1" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M73,105 L132,105"/>
</Grid>

My xaml App using the Custom Control:

<Grid x:Name="LayoutRoot">
    <Tester:MyControl1 HorizontalAlignment="Left" Margin="56,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test1"/>
    <Tester:MyControl1 HorizontalAlignment="Left" Margin="116,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test2"/>
    <Tester:MyControl1 HorizontalAlignment="Left" Margin="176,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test3"/>
    <Tester:MyControl1 HorizontalAlignment="Left" Margin="236,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test4"/>
    <Tester:MyControl1 HorizontalAlignment="Left" Margin="296,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test5"/>
</Grid>

My Custom Control VB Code:

Partial Public Class MyControl1 Public Sub New() MyBase.New()

    Me.InitializeComponent()

    ' Insert code required on object creation below this point.
End Sub

Private Sub OnMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

    Dim int_Temp As Integer
    Dim str_InstanceName As String

    str_InstanceName = "1.What code here tells me the name of the instance which was checked?  Test1, Test2, etc. for example."

    int_Temp = MessageBox.Show(str_InstanceName, "Testing", MessageBoxButton.OK)

    '2.What code here lets me manipulate parts of my control instances (and not just the instance which was clicked)?

            'I want to hide Test1.Line1 and Test2.Line3 and Test3.Circle1 and change the background of Test5.Rectangle1 for example.

End Sub

End Class

Thanks in advance, and sorry to all the C# experts that I need this in VB. Thanks.

A: 

It looks like you're talking about a User Control, not a Custom Control. There is a little bit of a difference when working with the two. You can read more about those differences here:

Custom Controls Vs. User Controls

In this case, you want to be looking at the 'sender' object in your event handler. You want to cast the sender as your User Control (this will be safe since you're only using that event handler on controls of your type).

Private Sub OnMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

    Dim senderAsControl As MyControl1 = sender As MyControl1

    ' Get the instance name from the sender
    Dim instanceName As String = senderAsControl.Name

    ' You can also access your children from the sender once cast
    senderAsControl.Rectangel1.IsVisible = False ' Hide the rectangle

End Sub

I can't compile the code to double check myself at the moment...but it should give you the idea.

Justin Niessner