Char edit in a text box

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1534 Views - Last Post: 11 February 2013 - 09:55 PM Rate Topic: -----

#1 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Char edit in a text box

Posted 05 February 2013 - 12:18 PM

Hi, how do I edit a char for example "R" in a text box and set it to a value..But I want to set all the "R"'s to "102"(Int)

Textbox1.Text = "Red Roses"
Textbox2.Text should then be "102ed 102oses"

and if that is posible i want to set all the char to a value
Between each char should be "_"
Space = +
e = 01
s = 23
d = 1
o = 00

Red Roses then must be : "102_01_1+102_00_23_01_23"

Then I would like to do the opposite : "102_01_1+102_00_23_01_23" = Red Roses

Please and thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Char edit in a text box

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 05 February 2013 - 12:29 PM

You could use an array to store the letters and their replacements. I would probably use a Dictionary, storing the letters as keys and their replacements as values (items) of the dictionary.

Create an empty string
Loop through each character of the text
Look up the replacement character in the dictionary
Append the replacement to your (initially) empty string

To reverse the process you might use the same dictionary, but I would probably consider just creating a second dictionary.
(The reverse process is more complicated because you will not be replacing individual characters.)

Just my initial impression :)
Was This Post Helpful? 1
  • +
  • -

#3 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: Char edit in a text box

Posted 06 February 2013 - 12:58 AM

Or if you're doing this in VB6, then a Collection could be used (I believe this is the equivalent of a dictionary (or similar to) in .Net
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 06 February 2013 - 03:42 AM

View Postmaj3091, on 06 February 2013 - 12:58 AM, said:

Or if you're doing this in VB6, then a Collection could be used (I believe this is the equivalent of a dictionary (or similar to) in .Net

Hello. I was referring to VB6. Yes, a Collection is directly available but it is straight-forward to add-in a Dictionary - see the link I provided - which has advantages:

Quote

The Dictionary is quite similar to the Collection object in both functionality and purpose. The Dictionary, however, offers some functionality that is not available with a Collection. Some of these features include:

The option to specify a comparison method for Keys. This allows for a case-sensitive Key, for example.
A method for determining if an object exists in a Dictionary.
A method for extracting all of the Keys into an Array.
A method for extracting all of the Items into an Array.
A method for changing a Key value.
A method for removing all items from the Dictionary.
Dictionary Keys are not limited to String datatype.


It just requires adding a reference to the Microsoft Scripting Runtime.

This post has been edited by andrewsw: 06 February 2013 - 03:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Re: Char edit in a text box

Posted 06 February 2013 - 05:24 AM

Ok, thanks.

I am still new to this so I don't know how to create a Dictionary and I use VS 2012(the closest object to VS was the VB 06 link)

So know I know it is possible, but where to start?

Create a Dictionary and set the values/keys.

But can someone maybe please direct me to a link where i can see how to create it (it's was hard for me to find something that was close to my idea and I didn't get any positive results of how and what to use.

At least I now know I must use a Dictionary but I need to know how to create and call it as well as set the values and edit a specific char like all the "R" in a text box

I am really new to this..So forgive me if I don't understand all the info you have provided to me.

After I created a Dictionary I should make a loop you say..like a timer? or?

Please, if you like to give me a little bit of your time (I know I must give some of the code to you but I don't know where to start and I don't want to look stupid..

Please and thanks
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: Char edit in a text box

Posted 06 February 2013 - 06:21 AM

View Postandrewsw, on 06 February 2013 - 10:42 AM, said:

View Postmaj3091, on 06 February 2013 - 12:58 AM, said:

Or if you're doing this in VB6, then a Collection could be used (I believe this is the equivalent of a dictionary (or similar to) in .Net

Hello. I was referring to VB6. Yes, a Collection is directly available but it is straight-forward to add-in a Dictionary - see the link I provided - which has advantages:

Quote

The Dictionary is quite similar to the Collection object in both functionality and purpose. The Dictionary, however, offers some functionality that is not available with a Collection. Some of these features include:

The option to specify a comparison method for Keys. This allows for a case-sensitive Key, for example.
A method for determining if an object exists in a Dictionary.
A method for extracting all of the Keys into an Array.
A method for extracting all of the Items into an Array.
A method for changing a Key value.
A method for removing all items from the Dictionary.
Dictionary Keys are not limited to String datatype.


It just requires adding a reference to the Microsoft Scripting Runtime.


Thanks Andrew, didn't even know it existed in VB6! :stupid:
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 06 February 2013 - 06:26 AM

VS2012 is the editor (the IDE) that you use to create code in either VB.NET or C# so the closest thing is VB.NET not VB6 (a much older version of Visual Basic).

VB.NET has dictionaries, lists, arrays, and many other collections. But it appears that your knowledge of Visual Basic is limited. I suggest you start by reading through a few tutorials - there is a Tutorials link at the top of this page.

@maj3091

Quote

Thanks Andrew, didn't even know it existed in VB6!

No worries! It should really be native to VB6, rather than added as a reference, as it is so useful :)
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 06 February 2013 - 06:37 AM

@maj3091
FWIW When we add a reference to Microsoft Scripting Runtime we also have access to a TextStream object.

VBScript
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, ts, fileObj, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

' Create the file, and obtain a file object for the file.
FileName = "c:\testfile.txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile(FileName)

' Open a text stream for output.
Set ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault)

' Write to the text stream.
ts.WriteLine "Hello World!"
ts.WriteLine "The quick brown fox"
ts.Close

' Open a text stream for input.
Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault)

' Read from the text stream and display the results.
Do While ts.AtEndOfStream <> True
    TextLine = ts.ReadLine
    document.Write TextLine & "<br />"
Loop

ts.Close

MSDN

This post has been edited by andrewsw: 06 February 2013 - 06:39 AM

Was This Post Helpful? 1
  • +
  • -

#9 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Re: Char edit in a text box

Posted 06 February 2013 - 09:30 AM

Well I created a program that search (for each "A" in text box 1) and adds "010" to next text box
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       
        Dim a As String = ""

        If TextBox1.Text.Contains("A") Then

            If TextBox1.SelectedText = "A" Then

                For Each a In (TextBox1.SelectedText)

                    TextBox2.Text = TextBox2.Text + "010"
                    TextBox1.SelectedText = ""
                Next

            End If
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim Search As String
        Dim Where As String
        ' Get search string from user.

        ' Find string in text.
        Where = InStr(TextBox1.Text, "A")
        If Where Then
            TextBox1.Focus()
            TextBox1.Selectionstart = Where - 1
            TextBox1.SelectionLength = Len("A")
        Else
            MsgBox("String not found.")
        End If

    End Sub
End Class



So my problem is when I take the "A" and make it "010" the text loses order eg. "ABCDA" then text2 = "010010" and the "BCD"

So how can i still keep the char location eg. "ABCDA" first A = 1 and last A = 4 so it will be "010BCD010"
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 06 February 2013 - 09:56 AM

I see that you are taking a different approach and also working with SelectedText and the selection. Fair enough :)

Anyway, just in case it helps, the following code changes "ABCDA" to "010BCD010".

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sCoded As String = ""

        For Each ch As Char In TextBox1.Text.ToUpper
            If ch = "A"c Then
                sCoded += "010"
            Else
                sCoded += ch
            End If

        Next
        TextBox1.Text = sCoded
    End Sub

Was This Post Helpful? 1
  • +
  • -

#11 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Re: Char edit in a text box

Posted 09 February 2013 - 12:36 PM

Thanks allot! andrewsw
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 09 February 2013 - 12:38 PM

View PostRjjvv, on 09 February 2013 - 12:36 PM, said:

Thanks allot! andrewsw

You are welcome.
Was This Post Helpful? 0
  • +
  • -

#13 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Re: Char edit in a text box

Posted 09 February 2013 - 12:40 PM

One last thing, how do I make 010 A again?
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,511
  • Joined: 12-December 12

Re: Char edit in a text box

Posted 09 February 2013 - 12:49 PM

View PostRjjvv, on 09 February 2013 - 12:40 PM, said:

One last thing, how do I make 010 A again?

That is trickier. You still have to loop through all the characters, but use IsNumeric() to discover if the current character is a number. If it is, then save it in a string and keep appending to it until the next character that is not a number. Good luck!
Was This Post Helpful? 0
  • +
  • -

#15 Rjjvv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-January 13

Re: Char edit in a text box

Posted 09 February 2013 - 01:08 PM

Ok, I see that it says True/False but how do I use each 3 char in textbox1
eg: If the following 3chars are = to 010 then TB2.text = A

It is quit hard, to use this when the char > than 1

I see I can do it easy the same way if I make A = @(a 1 char)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2