Textboxes reacting to data entered

A

Anders Eriksson

In a WPF project using VS2010.

I have two textboxes. When the user enters a C in the first textbox I
want my program to enter a 1 in the second. But if the user enters a 1
in the second textbox I want to enter a C in the first one.


How would I accomplish this?

My very simple XAML code

<Window x:Class="ChordDefinition.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left"
Margin="42,35,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left"
Margin="46,76,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>

// Anders
 
A

Anders Eriksson

There are a variety of techniques available, depending on your exact needs.

If you simply have a straight mapping from a full string in one text box to
a full string in the other text box, then the TextChanged event is likely
to be most appropriate.
The TextChanged event worked perfectly!

But now I also wanted to add so the user only can enter uppercase
letters and only one letter!

I tried to use KeyDown to check if I already have entered a character
and return e.handled=false if this is the second character or more...
Is this a correct way of doing it?

private void NoteTextbox_KeyDown(object sender, KeyEventArgs e)
{
TextBox tb = (TextBox)sender as TextBox;
string s = tb.Text.Trim();
if (s.Length >= 1)
{
Console.Beep();
e.Handled = false;
}
else
{
e.Handled = true;
}
}

I also tried to add a ToUpper in the TextChanged event but then when I
update the TextBox it will (of cause) trigger the TextChanged event...
So how do I get uppercase letters even if the user enters lowercase?

// Anders
 
A

Anders Eriksson

But now I also wanted to add so the user only can enter uppercase
letters and only one letter!

I tried to use KeyDown to check if I already have entered a character
and return e.handled=false if this is the second character or more...
Is this a correct way of doing it?

private void NoteTextbox_KeyDown(object sender, KeyEventArgs e)
{
TextBox tb = (TextBox)sender as TextBox;
string s = tb.Text.Trim();
if (s.Length >= 1)
{
Console.Beep();
e.Handled = false;
}
else
{
e.Handled = true;
}
}

I just realized that I have e.Handled mixed up! it should be set to true
when I don't want to add a character. I.e. the character is already handled!
I also tried to add a ToUpper in the TextChanged event but then when I
update the TextBox it will (of cause) trigger the TextChanged event...
So how do I get uppercase letters even if the user enters lowercase?
Found CharacterCasing="Upper" that will fix uppercase!

Sorry for posting without checking myself first!

// Anders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top