Prasanna,
The PlanetSourceCode was interesting. I chose to solve the problem differently.
<code>
...
public partial class Form1 : Form
{
private bool CapNext = true;
private bool ChangingText = false;
private void ReCase(object sender)
{
if (!ChangingText)
{
String tmp = ((TextBox)sender).Text;
if (tmp.Length > 0)
{
char ch = tmp[tmp.Length - 1];
if (CapNext)
{
ChangingText = true;
((TextBox)sender).Text = tmp.Substring(0, tmp.Length - 1);
((TextBox)sender).AppendText(new String(char.ToUpper(ch), 1));
CapNext = false;
ChangingText = false;
}
else
{
Console.WriteLine("last char:{0}", (long)ch);
switch ((long)ch)
{
case 32:
{
CapNext = true;
break;
}
case 10: // new line
{
CapNext = true;
break;
}
case 46: // period
{
CapNext = true;
break;
}
}
}
}
else
{
CapNext = true;
}
}
}
...
private void textBox2_TextChanged(object sender, EventArgs e)
{
ReCase(sender);
}
private void textBox2_Enter(object sender, EventArgs e)
{
CapNext = true;
}
...
</code>
This dynamically changes the case to your specifications. You could add more requirements (like the period, which would otherwise not be necessary as spaces generally follow a period) if you liked. Such as a comma, colon, etc.
Note that each textbox on the form sets CapNext to true when it gets the focus and calls ReCase when it is changed.
Another approach would be to derive your own textbox class from TextBox and add the necessary code to handle the re-casing.
What you don't know can hurt you!
|