views:

17

answers:

1

I have added a usercontrol to my project like this:

    Public Sub clickAutoDrillLeft(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)

    Dim LSliderItem as New TriplexAmpsControl   
    me.LeftSlider.Items.Add(LSliderItem)

End sub

The "LSliderIn" object is an items control, and the "TriplexAmpsControl" is a usercontrol that has three writeonly properties declared as integers named "AmpsPhaseA", "AmpsPhaseB", and "AmpsPhaseC".

If I instantiate the control at runtime as above, I can immediately assign a value to one of the properties like:

    Public Sub clickAutoDrillLeft(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)

    Dim LSliderItem as New TriplexAmpsControl   
    me.LeftSlider.Items.Add(LSliderItem)
    LSliderItem.AmpsPhaseA = 50

End sub

But only within the sub routine. I don't know how to reference the control values elsewhere in the form, because if I try to call the control by its name from some other sub, the compiler tells me, naturally, that the control is not part of the project because it has not been created yet.

All I have been able to find on the subject concerns the creation of controls in code-behind, but noting on how to connect to user controls instantiated the way I have done it.

A: 

(Pre-emptive note: excuse my VB - i'm a C# coder :)

You need to create a module level variable:

Dim _lSliderItem as TriplexAmpsControl

then in your code somewhere:

Public Sub clickAutoDrillLeft(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
    _lSliderItem = New TriplexAmpsControl   
    me.LeftSlider.Items.Add(_lSliderItem)
End sub

Or if that approach is out of the question for some reason then you can give the dynamically created control a name and later in your code use the FrameworkElement.FindName() method (most UI controls will be derived from FrameworkElement). Or you can code up your own little search function like this (excuse the C# syntax, it shouldn't be a problem for you to translate it to VB):

    public static DependencyObject FindChild(this DependencyObject o, Type childType, string childName, bool checkObjectItself)
    {
        if (checkObjectItself && (((string)o.GetValue(FrameworkElement.NameProperty)) == childName))
            return o;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
        {
            DependencyObject obj2 = VisualTreeHelper.GetChild(o, i).FindChild(childType, childName, true);
            if (obj2 != null)
                return obj2;
        }

        return null;
    }
slugster
Thanks a million! I'm feeling rather stupid now...
MBunds