Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2007, 08:28 AM
Authorized User
 
Join Date: Jun 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default TextBox Format

Hello Experts,

If I want first character as capital and rest in small and again in caps after a space. What shall i need to do while inserting into a text box.

I am trying to implement this for Windows form using C#.




Thanks

Prasanna
__________________
Thanks

Prasanna
 
Old September 10th, 2007, 11:08 PM
Registered User
 
Join Date: Sep 2007
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Default

C u need to aplly loop

first u recognise space n den apply caps to fisrt char


str="sdf sdf sdf";
for(i=0;i<str.length();i++)
{
  if(i==0)
  {
   apply caps to str[i];//means add ascii of 'A'
  }
  if(str[i]=' ')
  {
 apply caps to str[i+1];

  }

}







 
Old September 11th, 2007, 01:32 AM
Authorized User
 
Join Date: Jun 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Ashwini,

Thanks for the Input.
Can you be a bit more elaborate, considering values entered in Textbox.



Thanks

Prasanna
 
Old September 11th, 2007, 06:30 AM
Authorized User
 
Join Date: Jun 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My Problem is solved, thanks to this link.. Hope its useful to others.

http://planetsourcecode.com/vb/scrip...5918&lngWId=10

Thanks

Prasanna
 
Old September 11th, 2007, 09:50 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
print with the same textbox format abdrabaa C# 1 July 9th, 2007 04:53 AM
numeric format for a textbox Rudner Beginning VB 6 1 November 9th, 2004 02:29 PM
Textbox format sbass .NET Web Services 1 February 28th, 2004 03:32 PM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.