Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Locked Detect last charecter & Do something

  • Friday, August 10, 2012 10:17 AM
     
     

    I want to that.....

    if my textbox focus is lost then

    a "," (Comma) should be entered

    tb.Leave += delegate(object s, EventArgs ev)
                                    {
                                       tb.Text = tb.Text.Trim() + ",";
                                    };

    this working fine (Output = My Sample Text,)

    if this text box focus is lost again (when text has been entered alraddy[My Sample Text,]) then output = My Sample Text,,

    so i decide to set a condition like this.... But no luck........

    if my text box has "," (Comma) as last charecter then should not enter ", " again

    my code is......

    tb.Leave += delegate(object s, EventArgs ev)
                                    {
                                        if (tb.Text.LastIndexOf(tb.Text) != ",")
                                        {
                                            tb.Text = tb.Text.Trim() + ",";
                                        }
                                        else
                                        {
                                            tb.Text = tb.Text.Trim();
                                        }
                                    };

    what is wrong with me here?

    please help.

    i am using DOT NET frame work 2.0

    see error

    http://img838.imageshack.us/img838/1843/errorzc.jpg

All Replies

  • Friday, August 10, 2012 10:19 AM
     
     Answered

    Last index of returns an int.

    You can use tb.Text.EndWith(",")

    http://msdn.microsoft.com/it-it/library/system.string.endswith(v=vs.80).aspx


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

  • Friday, August 10, 2012 10:38 AM
     
     Answered Has Code

    If you want to get the last char of the string:

    tb.Text.Substring(tb.Text.Length -1)

    or

    tb.Text[tb.Text.Lenght -1]

  • Friday, August 10, 2012 11:04 AM
     
     Answered Has Code

    Hi,

    You already have the code you need; you just need to modify it a tiny little bit.

    tb.Leave += delegate(object s, EventArgs ev)
    {
    if(tb.Text.EndsWith(',') 
        {
        tb.Text = tb.Text.Trim();
        }
    else
        {
         tb.Text = tb.Trim() + "," + " "; // Not sure why you are adding a space when you are using Trim() :P
        }
    }

    That's all you need.

    Dave


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

  • Saturday, August 11, 2012 6:16 AM
     
     Answered

    private void button1_Click(object sender, EventArgs e)
            {
                string LastChar = string.Empty;
                LastChar = textBox1.Text;
                LastChar = LastChar.Substring(LastChar.Length-1,1);
                MessageBox.Show("The Last Character is " + LastChar);
            }

    This will detect the last character is textbox, and its up to you what you want to do with last character