change backcolor

hi

I write below codes to change the color when focused ,the problem is in "?" what should i write instead of "?"

void textbox1_KeyDown(object sender,KeyEventArgs e)

{

if (e.KeyCode ==Keys.Enter)

{

textbox2.Focus();

name.BackColor=?;

}

}

[683 byte] By [ehsano2] at [2008-1-8]
# 1
Hi,

Please don't take this as an offense, but you are trying to do some strange things.

1. You are writing unmaintable code. You really don't want to write code that regulates navigation through your form like this.
2. You are changing the default behavior of a Windows UI. The Enter key normally is a key shortcut to for example a default button. Navigating through your form is conventionally done by using the Tab key. I would not change this unless this would be a hard requirement of your customer. I would try to talk it out of his head though.

Configuration of the navigation through a form is done by using the TabIndex property.

I would do this like so (no code, just clean understandable, maintainable markup):

Code Snippet

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Item 1:" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="Item 2:" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="Item 3:" Grid.Row="2" Grid.Column="0"/>
<TextBlock Text="Item 4:" Grid.Row="3" Grid.Column="0"/>
<TextBlock Text="Item 5:" Grid.Row="4" Grid.Column="0"/>
<TextBlock Text="Item 6:" Grid.Row="5" Grid.Column="0"/>

<TextBox Grid.Row="0" Grid.Column="1" Width="200" TabIndex="1"/>
<TextBox Grid.Row="1" Grid.Column="1" Width="200" TabIndex="2"/>
<TextBox Grid.Row="2" Grid.Column="1" Width="200" TabIndex="3"/>
<TextBox Grid.Row="3" Grid.Column="1" Width="200" TabIndex="4"/>
<TextBox Grid.Row="4" Grid.Column="1" Width="200" TabIndex="5"/>
<TextBox Grid.Row="5" Grid.Column="1" Width="200" TabIndex="6"/>

<StackPanel Orientation="Horizontal" Grid.Row="6" Grid.ColumnSpan="2" HorizontalAlignment="Center">
<Button Content="Save" TabIndex="7"/>
<Button Content="Cancel" TabIndex="8"/>
<Button Content="Clear" TabIndex="9"/>
</StackPanel>
</Grid>
</Page>


Then using the Tab key, you walk through your form and the background of your TextBoxes change as they get focus.

Best regards,

Benny

BennyTops at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

Dear ehsano2,

Check this post.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2003177&SiteID=1

HTH,

Suprotim Agarwal.

SuprotimAgarwal at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
Hi,

From your code, I do not understand what is 'name' object? Clarify which color (back color of what) you intend to change.

If you want to change the background color of textbox2 then you can do so by

textbox2.Background = Brushes.Blue (any brush object).

Regards,
Jayanta

JayantaDey at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified