none
replacing letters with other letters after type.

    Question

  • Just trying to make a FUN program. I am a learner of C#.

    1. I need a form where there I put a 1 Rich Text Box (richTextBox1)

    2. Whatever button I press in keyboard OR gave input in that richTextBox1 box, I would like to change text.

    3. There is sentense "I am a Learner." I want to display in that richbox.

    4. But, I tell my (blindfolded) fren to type "God bless America" in that box.

    Now, If he type any letter in Richtextbox,

    Example: 

    if he type first letter G, then G should be replace by "I", "o" replace by " " (space), "d" replace by "a", .... and this way, 

    "God bless America" would be change to sentense "I am a Learner". 

    this should also go through loop, so after sentense finish also same sentense would repeat letter by letter. 

    Here, I did this.

    private string getword(int x)
    {
          string[] sentense = new string[] { "i", " ", "a", "m", " ", "a", " ", "L", "e", "a", "r", "n", "e", "r", ".", " " };
           return sentense[x];
    }

    string all = "";
    int leftNumber;
    int richBoxLength;
    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

                all = richTextBox1.Text;
                richBoxLength = richTextBox1.TextLength; 
                int lengthWithoutLastLetter = richBoxLength - 1; 
                leftNumber = lengthWithoutLastLetter % 14; //length of array OR total letters is 14 
                string ma = getword(leftNumber);
                richTextBox1.Text += ma;
               //string withoutLast = all.Substring(0, lengthWithoutLastLetter);            
                //richTextBox1.Text = withoutLast + "" + getword(leftNumeber);

            }

    Anybody please help. 

    Sunday, September 22, 2013 9:30 PM

Answers

  • Ok. I don't see the problem. Just use the example I have given you and change the parameters of methods in the way you need. Just use the Length-1 as a counter so you can use the specific array position. That is all.

    Microsoft Student Parnter Microsoft Technology Associate


    Monday, September 23, 2013 12:22 AM
  • Here you go. This code goes in the textbox's KeyPress event.  The button will clear the textbox and reset the count to 0.

    Public Class Form1 Private count As Integer Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim mytext As String = "I am a learner." Dim ln As Integer = mytext.Length Dim mychars(ln - 1) As Char If count >= ln Then e.Handled = True Else mychars = mytext.ToCharArray e.KeyChar = mychars(count) count += 1 End If End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
            count = 0
        End Sub End Class



    Solitaire


    Monday, September 23, 2013 11:36 PM

All replies

  • I don't really know if I understand you. But I don't think that you can do this in the way you think you can. Who guarantees you that your friend wouldn't type the same letter twice?

    Also you can make comparison where you will compare last letter of the string with your array and if that letter match some value from the array, you will just switch that letter with another one.

    For an instance, to get the last letter from the string, you can use this example:

    String a = "asdjadsnaksda";
    MessageBox.Show(a.ElementAt(a.Length - 1).ToString());

    With this code, you will get the "a" as the result in the MessageBox.

    And to replace a last letter with some other letter you can use this example:

    MessageBox.Show(a.Replace(a.ElementAt(a.Length - 1), 'b'));

    I used b as a char value, but you can use any value from the defined array.

    Microsoft Student Parnter Microsoft Technology Associate

    Sunday, September 22, 2013 11:04 PM
  • not only sentense "God bless America"....... I just gave this example... but he can type anything.... any letter should be change to another letter from array.

    "m" - "I"

    "my" - "I "

    "my " - "I a"

    "my n" - "I am"

    ..............................

    Whatever the letter my friend type, it should change to letter given in array. He can mistype, no problem for that. here what I am saying is... not to replace specific letter to specific letter that given but specific letter position should replace from same position letter from array. I hope you understand...


    Monday, September 23, 2013 12:16 AM
  • Ok. I don't see the problem. Just use the example I have given you and change the parameters of methods in the way you need. Just use the Length-1 as a counter so you can use the specific array position. That is all.

    Microsoft Student Parnter Microsoft Technology Associate


    Monday, September 23, 2013 12:22 AM
  • I don't understand :( 

    Can you please write more lines of code for me??

    Monday, September 23, 2013 10:44 PM
  • Here you go. This code goes in the textbox's KeyPress event.  The button will clear the textbox and reset the count to 0.

    Public Class Form1 Private count As Integer Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim mytext As String = "I am a learner." Dim ln As Integer = mytext.Length Dim mychars(ln - 1) As Char If count >= ln Then e.Handled = True Else mychars = mytext.ToCharArray e.KeyChar = mychars(count) count += 1 End If End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
            count = 0
        End Sub End Class



    Solitaire


    Monday, September 23, 2013 11:36 PM