Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to navigate to previous letter in a single word
Posted
Comments
Om Prakash Pant 11-Nov-11 0:52am    
can you show the code which you have already written for this?
Mehdi Gholam 11-Nov-11 0:55am    
what do you mean by navigate?
Tejas Vaishnav 11-Nov-11 0:57am    
provide proper detail so we can solve your problem..
like what you have done so far and what you want from us...
member60 11-Nov-11 0:57am    
try to elaborate your question , include a code snippet to explain
Amol_27101982, India 11-Nov-11 0:58am    
What exactly you want to do ???

if your "word" is stored in string, you can access any symbol at any time through [] operator
 
Share this answer
 
Hi muskesh009singh,

From your comment I'm assuming you want to do something like jumping in the text with special keyboard inputs. I created an example with an possible approach (maybe it's not exactly what you want, but I think you could adapt it - if you like it). Just copy to a C# 4 Forms project and replace Program.cs with the following code:

C#
using System;
using System.Windows.Forms;

namespace NavigateToLetter
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create a Form with a TextBox
            Form form = new Form();
            form.Text = "NavigateToLetter";
            TextBox textbox = new TextBox();
            textbox.Multiline = true;
            textbox.Dock = DockStyle.Fill;
            form.Controls.Add(textbox);
            // We handle the PreviewKeyDown event to jump to a new cursor
            // position on entered "input", this could be any key-combination
            //(NUM block, function keys, letters, ...)
            textbox.PreviewKeyDown +=
            delegate(object sender, PreviewKeyDownEventArgs e)
            {
                // ... so how to control the jumping while typing -
                // hmm.. lets take the function keys
                const int iF1_KEY = 112; // the F1 key
                const int iF24_KEY = 135; // the F24 key
                //
                int iKey = (int)e.KeyCode; // so grab the pressed key's code
                // ... and look if it's a function key
                // (we use the Keys enumaration values to shorten the code)
                if (iKey >= iF1_KEY && // the F1 Key 
                    iKey <= iF24_KEY) // the F24 Key (if you have a keyboard
                                         // with that many Function-Keys)
                {
                    string strWord = String.Empty; // the current word.
                    int iIndexOfLastSpace = textbox.Text.LastIndexOf(' ');

                    // Where is the last word?
                    if (iIndexOfLastSpace < 0) // no previous words found
                    {
                        // so we assume we are on the first word
                        strWord = textbox.Text;
                    }
                    else
                    {
                        // let's take the string starting from the last space as
                        //current word
                        strWord = textbox.Text.Substring(iIndexOfLastSpace + 1);
                    }

                    // do we have a word now?
                    if (!String.IsNullOrEmpty(strWord))
                    {
                        // lets think about the jumping:
                        // get the new cursor position the user gave us
                        int iEnteredPosition = iKey - iF1_KEY;
                        // but, is the word long enough?
                        // (else we stay on the current cursor position)
                        if (strWord.Length >= iEnteredPosition)
                        {
                            // calculate the new cursor position
                            // (begin of last word + entered position)
                            int iNewPosition = textbox.Text.Length -
                                               strWord.Length + iEnteredPosition;
                            // and finally: set the cursor position 
                            textbox.Select(iNewPosition, 0);
                        }
                    }
                }
            }; 
            // Ok, let's run the form and test it!
            Application.Run(form);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900