Results 1 to 6 of 6

Thread: Inserting character to the left of a string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Inserting character to the left of a string

    Hi guys,

    I'm trying to insert a character to the left for a right to left writing, similar to the Arabic style.

    Here's the code:

    vb.net Code:
    1. Dim Cache As String
    2.     Dim memory As Integer = 0
    3.     Public Sub TextOutput(ByVal Character As String)
    4.  
    5.         '///////////////////////////
    6.         Cache = TextBox1.Text
    7.         Dim Memory As Integer = TextBox1.SelectionStart
    8.         TextBox1.Text = Cache.Insert(Memory, Character)
    9.         TextBox1.SelectionStart = Memory
    10.  
    11.     End Sub

    The thing is, I'm using this method do that I can insert a character I missed during typing. However the direction of the string goes from left to right. How do I go around this?

    Thanks.

    Vizier87.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Inserting character to the left of a string

    The `startIndex` parameter of the String.Insert method specifies the index of the character BEFORE which you want to insert the value. If you want to insert AFTER a particular character then that is BEFORE the NEXT character, so simply add 1 to the index.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Inserting character to the left of a string

    Hi JMC,

    I tried this:

    vb.net Code:
    1. If Memory = 0 Then
    2.             TextBox1.Text = Cache.Insert(Memory, Character)
    3.         End If
    4.  
    5.  
    6.         If Memory > 0 Then
    7.             TextBox1.Text = Cache.Insert(Memory + 1, Character)
    8.         End If

    But the text direction was still adding to the right. What did I miss?

  4. #4
    Addicted Member omundodogabriel's Avatar
    Join Date
    May 2013
    Posts
    177

    Re: Inserting character to the left of a string

    If I understood, you want to add a character at middle of text, right?
    Try this:
    Code:
    TextBox1.Text = TextBox1.Text.Insert(TextBox1.SelectionStart, "a")
    Last edited by omundodogabriel; Oct 21st, 2014 at 06:02 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Inserting character to the left of a string

    Quote Originally Posted by omundodogabriel View Post
    If I understood, you want to add a character at middle of text, right?
    Try this:
    Code:
    TextBox1.Text = TextBox1.Text.Insert(TextBox1.SelectionStart, "a")
    Hi Gabriel, thanks for the input. However, my objective is on the Right-to-Left method of writing characters, so when I want to key in characters "t", "e", "s" and "t" it would appear as "tset" rather than "test". Seems that the insert method only allows keying in characters from left to right?

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Inserting character to the left of a string

    Right because that's how most languages work... left to right...
    So to go "right to left" - character by character - you have to code for that... usually by changing where the insert point it, by resetting it to the beginning.
    All you should need to to is in the text changed event is to set the SelectionStart property to 0... so each time the user types something, the text changes and the cursor moves to the front of the textbox, allowing the next character to be inserted to the left.

    You probably also will want to change the alignment on the text box too.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width