Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Referencing Custom Control names and properties within... RSS

6 replies

Last post May 15, 2009 04:42 AM by jlgraybill

(0)
  • jlgraybill

    jlgraybill

    Member

    4 Points

    10 Posts

    Referencing Custom Control names and properties within VB Code Behind

    May 13, 2009 04:28 AM | LINK

    Sorry for the basic questions, but I've been searching the web for a few 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?

    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 my control (such as hiding a Line, and changing the text).

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

  • codeblock

    codeblock

    Contributor

    4070 Points

    716 Posts

    Re: Referencing Custom Control names and properties within VB Code Behind

    May 13, 2009 07:20 AM | LINK

    These are similar in C# or VB. I'm unable to write samples in vb (because I have not installed it on my machine) but I think these are simple...

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

    When you attach an event you will have a method called in your codebehind with 2 parameters (object and MouseButtonEventArgs). The object parameter is the sender of the event. If you click a rectangle it will be a reference to the rectangle that has been clicked.

    > 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 my control (such as hiding a Line, and changing the text).

    once you get a reference to an object you can manipulate it using its properties. If you want to change the text of a TextBox you can simply set its Text property

    <TextBox x:Name="mytextbox" />

    ...

    in code

    mytextbox.Text = "hallo"

    to hide parts of the control you can use the Visibility property

    mytextbox.Visibility = Visibility.Collapsed

    hth

    bye

    If this answers your question, please "mark it as answer"

    --
    Andrea Boschin
    MVP Silverlight & Silverlight Insider
    , Me on Twitter
  • jlgraybill

    jlgraybill

    Member

    4 Points

    10 Posts

    Re: Referencing Custom Control names and properties within VB Code Behind

    May 13, 2009 01:31 PM | LINK

    Thanks for responding Andrea,

    That doesn't really answer my questions though.  I've done what you're talking about, but that's not what I'm looking for.  I'm looking for actual names of my instances, and I'm looking to manipulate pieces of those instances (both the one which was clicked and other instances).

    Take this sample code into consideration, and I've asked my questions within the VB code for the control:

    My UserControl:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        x:Class="MyControl1"
        x:Name="UserControl"
        d:DesignWidth="60" d:DesignHeight="59">

        <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>
    </UserControl>

     

    My xaml App using the Custom Control:

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Window1"
        x:Name="Window"
        Title="Window1"
        Width="640" Height="480" xmlns:Tester="clr-namespace:Tester">

        <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>
    </Window>

     

    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 Test4.Rectangle1 for example.


        End Sub
    End Class

     

    Thanks,

    -Jeremy

  • codeblock

    codeblock

    Contributor

    4070 Points

    716 Posts

    Re: Re: Referencing Custom Control names and properties within VB Code Behind

    May 13, 2009 02:31 PM | LINK

    While your xaml shows a <Window /> tag you are working with WPF and not Silverlight so this is not the correct forum where to post.

    However, to get the element that initially raised the event you may rely on e.OriginalSource. This contains a reference to this instance. To get the name you have simply to read the Name property.

    While the controls (MyControl1 are one on top of the other you can handle the click event into every control and act to change the properties inside of it.

    hth

    If this answers your question, please "mark it as answer"

    --
    Andrea Boschin
    MVP Silverlight & Silverlight Insider
    , Me on Twitter
  • jlgraybill

    jlgraybill

    Member

    4 Points

    10 Posts

    Re: Re: Referencing Custom Control names and properties within VB Code Behind

    May 13, 2009 10:28 PM | LINK

    Thanks for responding Andrea,

    First of all, I only created a small test to pass on the code, as my code is much too large to include here.  My code is a Silverlight Project created within Microsoft Blend 2, accessing VB code behind, so if this is the wrong forum for that, I apologize but I'm not sure where I should write then.  Please let me know if you know of a better place to find my answers, as I would greatly appreciate it.  The test sample I created was 2 minutes spent on creating the essence of my issue.

    Unfortunately, I had tried what you recommend, and it doesn't work either.  In my example, accessing e.OriginalSource.Name returns the individual item within my control which was clicked.  For example, it doesn't return Test1, Test2 or Test3 (names of my control instances).  Instead, it returns Rectangle1, Line1, Line2, etc, which are components of the Custom Control.  I have created a custom control which will be used 200 - 250 times, and I need to know the name of the individual instance which was selected, not the components within the control which are selected.

     Thanks a lot,

    -Jeremy

  • codeblock

    codeblock

    Contributor

    4070 Points

    716 Posts

    Re: Re: Re: Referencing Custom Control names and properties within VB Code Behind

    May 14, 2009 07:08 AM | LINK

    you ca recursive navigate Parent property while the type is MyControl1

    public MyControl1 FindParent(FrameworkElement element)
    {
       while(element != null)
       {
           if (element.GetType() == typeof(MyControl1))
              return element;

           element = element.Parent as FrameworkElement;
       }

       return null; // not found
    }

    then use this function:

    MyControl1 instance = FindParent(e.OriginalSource);

    this is c#... but the concept is clear

    bye

    If this answers your question, please "mark it as answer"

    --
    Andrea Boschin
    MVP Silverlight & Silverlight Insider
    , Me on Twitter
  • jlgraybill

    jlgraybill

    Member

    4 Points

    10 Posts

    Re: Re: Re: Referencing Custom Control names and properties within VB Code Behind

    May 15, 2009 04:42 AM | LINK

    Thanks Andrea...you the man!  This works great.  And I found a C# to VB converter in another post which worked great.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thanks again for your help.