converting characters to another character in key press

lanche26

New member
Joined
Mar 29, 2012
Messages
4
Programming Experience
Beginner
Good readings every one, im new here in this forum because ive heard that this forum is good and can answer question clearly,

by the way i want to make a program that for example i will type in text box 1 "sample" but the text that will appear is a different letters or word, example of that is i type in text box the word "single" but the text that will appear is "sample".... i know this will be in the key press event?

ill give credits to those who can share their ideas clearly.
please help me. thanks in advance and more power to you guys.
 
I give credits to those who look in the documentation before asking questions. Maybe you didn't know before but now you do: always read the documentation first. The documentation for the KeyPress event would tell you that the event handler receives a KeyPressEventArgs. It also provides a code example that sows how to use that object. The documentation for the KeyPressEventArgs class would tell you that it has a KeyChar property, that gets or sets the character entered. You can get the current value and, if appropriate, assign a new value.
 
I give credits to those who look in the documentation before asking questions. Maybe you didn't know before but now you do: always read the documentation first. The documentation for the KeyPress event would tell you that the event handler receives a KeyPressEventArgs. It also provides a code example that sows how to use that object. The documentation for the KeyPressEventArgs class would tell you that it has a KeyChar property, that gets or sets the character entered. You can get the current value and, if appropriate, assign a new value.




what documentation sir? and where can i find it??
 
what documentation sir? and where can i find it??
"Help" is the keyword here, most computer programs will have a help system available when you press F1 key or use main menu Help options. That goes for the VS/VB programs also, which will lead you to the MSDN Library. MSDN
Library is a place of all wonders when it comes to learning resources for VS, VB, .Net and development in general. As for "documentation", every little bit of information regarding the VB language and the .Net Framework class library is documented, and this reference documentation is also included in the library.
 
what documentation sir? and where can i find it??

Another way of accessing the MSDN help is to type the event (KeyPress +MSDN) or the EventArgs (KeyPressEventArgs +MSDN) into google and then look for the MSDN result. I prefer to do it this way as I seem to get my answer a lot quicker.

Regards
 
Sample code for replacing user input of text with other characters.

How about this sample? Place 3 textboxes and 3 labels on the form. The first one will replace the key with the character next in the alphabet. The second one will reverse the alphabet. And the third one will select a random character. The labels will display the original input.

VB.NET:
    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If TextBox1.Text = "" Then  'clear label for new word
            Label1.Text = ""
        ElseIf e.KeyChar = Chr(8) Then  'backspace deletes last character in textbox and label
            Dim n As Integer = TextBox1.Text.Length
            TextBox1.Text = TextBox1.Text.Substring(0, n)
            Label1.Text = Label1.Text.Substring(0, n - 1)
            TextBox1.SelectionStart = n
            Exit Sub
        End If
        Label1.Text &= e.KeyChar
        e.KeyChar = Chr(Asc(e.KeyChar) + 1)  'display next character in alphabet
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If TextBox2.Text = "" Then  'clear label for new word
            Label2.Text = ""
        ElseIf e.KeyChar = Chr(8) Then  'backspace deletes last character in textbox and label
            Dim n As Integer = TextBox2.Text.Length
            TextBox2.Text = TextBox2.Text.Substring(0, n)
            Label2.Text = Label2.Text.Substring(0, n - 1)
            TextBox2.SelectionStart = n
            Exit Sub
        End If
        Label2.Text &= e.KeyChar
        Dim ltr As Integer, v As Integer = 0
        Dim mychar As String = Convert.ToString(e.KeyChar).ToLower()
        For r As Integer = 97 To 122
            If mychar = Chr(r) Then
                ltr = 122 - v
                Exit For
            End If
            v += 1
        Next r
        e.KeyChar = Chr(ltr)
    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        Dim rnd As New Random()
        Dim ltr As Integer
        If TextBox3.Text = "" Then  'clear label for new word
            Label3.Text = ""
        ElseIf e.KeyChar = Chr(8) Then  'backspace deletes last character in textbox and label
            Dim n As Integer = TextBox3.Text.Length
            TextBox3.Text = TextBox1.Text.Substring(0, n)
            Label3.Text = Label1.Text.Substring(0, n - 1)
            TextBox3.SelectionStart = n
            Exit Sub
        End If
        Label3.Text &= e.KeyChar
        ltr = rnd.Next(97, 123)
        e.KeyChar = Chr(ltr)
    End Sub
 
thank you so much sir, you are the true master, what if it doesnt have to be randomize? i mean, i want it to be like an array of character like "sagutin mo toh"


sample is user input in text box "i love you" , the out put in the label will be "i love you" but in the text box, it will be "sagutin mo toh"

if the user inputs "abcdefghij" , the out put in the text box will be also "sagutin mo toh" -----------it has the same text box lengths,

i cant seem to work it out cause error validation always comes up,un handled exception, invalid use of array or something like that

should i use array for each character? and how do i do that?
 
Using a string

No array needed. Just declare a string with the desired text to use as a replacement for the input. Note the code for the backspace is a little different.

VB.NET:
Private Sub TextBox4_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        Dim mystr As String = "sagutin mo toh"  
        Dim l As Integer = mystr.Length
        Dim t As Integer = TextBox4.Text.Length
        Dim ltr As String
        If TextBox4.Text = "" And e.KeyChar = Chr(8) Then Exit Sub
        If t >= l And e.KeyChar <> Chr(8) Then
            e.Handled = True
            Exit Sub
        End If
        If TextBox4.Text = "" Then  'clear label for new word
            Label4.Text = ""
        ElseIf e.KeyChar = Chr(8) Then  'backspace deletes last character in textbox and label
            TextBox4.Text = TextBox4.Text.Substring(0, t)
            Label4.Text = Label4.Text.Substring(0, t - 1)
            TextBox4.SelectionStart = t
            Exit Sub
        End If
        Label4.Text &= e.KeyChar
        ltr = mystr.Substring(t, 1)
        e.KeyChar = Convert.ToChar(ltr)
    End Sub
 
it really works! but can we declare a character for this code to work? i mean, for example, the "." character will activate this code and the user input " .i love you.etc etc etc etc " the i love you will only be in the label, and the remaining strings will not be in the label, i think i should declare a variable for the original text from the text box 4, and then vb will get the strings only inside the two "."(dots), and where should i put this? in the lost focus declaration? if i put this on the lost focus declaration, it will not type the strings "sagutin mo toh" one by one,??? how could this be possible?
 
Sorry, but you're not making any sense. Where are the 2 dots coming from?

You seem to be presenting me with a challenge to solve. This is YOUR problem. It's your turn to write some code and see how it works. If you are still having a problem, then post your code and be more specific.
 
Back
Top