0

I'm creating a Windows 8 app and I have the need for events to be raised during a GotFocus event for a TextBox element. I'm not sure what's wrong or which direction to go in exactly as 1. I'm not that good with events in C# to begin with, and 2. I reckon it's a bit different in WindowsRT. The TextBoxListArray() method is started through another event.

public sealed partial class MainPage : Page
{
    List<TextBox> textBox = new List<TextBox>();
    List<RichEditBox> editBox = new List<RichEditBox>();
    static int tally;
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void TextBoxListArray()
    {
        textBox.Add(new TextBox());
        int i = textBox.Count();
        i = i - 1;
        tally = i - 1;
        textBox[i].HorizontalAlignment = HorizontalAlignment.Stretch;
        textBox[i].VerticalAlignment = VerticalAlignment.Top;
        textBox[i].TextWrapping = TextWrapping.NoWrap;
        textBox[i].Margin = new Thickness(10);
        textBox[i].Text = i.ToString();
        textBox[i].IsReadOnly = true;
        textBox[i].Height = 40;
        stackNotes.Children.Add(textBox[i]);
        textBox[i].GotFocus += new EventHandler(TextBoxList_GotFocus);
    }

    private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
    {
        textBox[tally] = sender as TextBox;
        textBox[tally].Background = new SolidColorBrush(Colors.Yellow); 
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
Keith
  • 139
  • 3
  • 12
  • `I'm not sure what's wrong` - Please learn MVVM before you ever write a single line of code in any of the XAML-based technologies. Don't create or manipulate UI elements in procedural code. That's what XAML is for. – Federico Berasategui Sep 17 '13 at 02:42
  • These are not static elements and they need to be dynamically generated on a raised event. Is MVVM helpful for this? If so do you have any recommended readings? – Keith Sep 17 '13 at 02:52
  • XAML's concept of "dynamic" is really different from other UI frameworks. My point still stands. Don't create or manipulate UI elements in procedural code. I don't have Windows 8 right now so I could not test, but I could give you a WPF-based example if you wish. – Federico Berasategui Sep 17 '13 at 02:53
  • Yes, that would be helpful. I think they're close enough to get me going. – Keith Sep 17 '13 at 02:56

3 Answers3

3

This is the right way to do what you're looking for in WPF (or any of the XAML-based UI frameworks)

<Window x:Class="MiscSamples.TextBoxItemsControlSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBoxItemsControlSample" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}" x:Name="TextBox"/>
                <DataTemplate.Triggers>
                    <Trigger SourceName="TextBox" Property="IsKeyboardFocusWithin" Value="True">
                        <Setter TargetName="TextBox" Property="Background" Value="Yellow"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

Code Behind:

public partial class TextBoxItemsControlSample : Window
{
    public TextBoxItemsControlSample()
    {
        InitializeComponent();

        DataContext = Enumerable.Range(0, 100).Select(x => "Text" + x.ToString());

    }
}

Result:

enter image description here

  • There is not a single line of code that manipulates ANY UI element. You don't need that. DataBinding is powerful enough to remove the need for all the horrendous hacks that were abundant in dinosaur UI frameworks.
  • The Trigger makes the TextBox's Background yellow whenever IsKeyboardFocusWithin is true for any given TextBox. That removes the need for procedural approaches.
  • I did not test this in WinRT XAML. There might be differences (such as different property names, etc).
  • You can further customize the UI by defining properties of the ItemsControl (such as ItemsPanel).
  • WPF / XAML rocks. Just copy and paste my code in a File -> New Project -> WPF Application and see the results by yourself.
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

the problem with your code is using tally for get the focused item from the list. since you have sender parameter you can cast it to a TextBox and then set the background.

there may be better approach specially for WPF as other answers.

private void TextBoxListArray()
{
    var tb =  new TextBox() { Text = textBox.Count(), ....... };
    tb.GotFocus += new EventHandler(TextBoxList_GotFocus);
    textBox.Add(tb);
    stackNotes.Children.Add(tb);
}

private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
{
     var txtBox = sender as TextBox;
     txtBox.Background = new SolidColorBrush(Colors.Yellow); 
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0

Here's what I did to make it work. It's definitely a hack but it got the result I needed. I will also try it the "proper" way through XAML.

private void TextBoxListArray()
    {
        textBox.Add(new TextBox());
        int i = textBox.Count();
        i = i - 1;
        tally = i - 1;
        textBox[i].HorizontalAlignment = HorizontalAlignment.Stretch;
        textBox[i].VerticalAlignment = VerticalAlignment.Top;
        textBox[i].TextWrapping = TextWrapping.NoWrap;
        textBox[i].Margin = new Thickness(10);
        textBox[i].Text = i.ToString();
        textBox[i].IsReadOnly = true;
        textBox[i].Height = 40;
        stackNotes.Children.Add(textBox[i]);
        textBox[i].GotFocus += new RoutedEventHandler(TextBoxList_GotFocus);
        textBox[i].LostFocus += new RoutedEventHandler(TextBoxList_LostFocus);
    }

    private void TextBoxList_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBoxSender = sender as TextBox;
        textBoxSender.Background = new SolidColorBrush(Colors.Beige);
    }

    private void TextBoxList_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBoxSender = sender as TextBox;
        textBoxSender.Background = new SolidColorBrush(Colors.White);
    }
Keith
  • 139
  • 3
  • 12
  • Why you assign `sender ` to `textBox[0]`? have you check my answer? – Damith Sep 17 '13 at 03:20
  • I didn't notice you actually had the same answer. The difference between the naming was subtle so it didn't catch my eye. – Keith Sep 17 '13 at 03:32