Results 1 to 8 of 8

Thread: [RESOLVED] [2.0] probably a simple solution...

  1. #1

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Resolved [RESOLVED] [2.0] probably a simple solution...

    I have a multiline textbox. Is there any way to add a single character as the first character on every line in the textbox?

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

    Re: [2.0] probably a simple solution...

    Do you mean after the fact or as the text is entered? If it's after the fact then you just get the Lines array and add the character to each element. If it's as the text is entered then you'd have to handle an event and append the character. You may have to experiment a bit but I think KeyUp should do the trick.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2.0] probably a simple solution...

    Just tested this and it works, although you'll have to add the character to the first line yourself:
    Code:
    private void TextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            this.TextBox1.AppendText("X");
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] probably a simple solution...

    This is what i mean. Lets say the textbox contains this:
    Code:
    This is line1
    this is line 2
    this is line3
    4
    5
    6
    i want to add a character at the begining of every line like:

    Code:
    *This is line1
    *this is line 2
    *this is line3
    *4
    *5
    *6
    Sorry for not being so clear in my first post.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2.0] probably a simple solution...

    Then, like I said in my first post, you get the Lines property and add the character to each element.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] probably a simple solution...

    Quote Originally Posted by jmcilhinney
    Then, like I said in my first post, you get the Lines property and add the character to each element.
    I understand what you mean, and thats the way i was going to do it, but i don't understand how (if that makes sense). I have this:

    Code:
    		void BtnAddCharClick(object sender, System.EventArgs e)
    		{
    			for (int i = 0; i < tbx.Lines.Length; i++)
    			{
    				// Stuck from here :S
    			}
    		}

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2.0] probably a simple solution...

    Lines returns an array of String. If you want to add a character to the beginning of a String you would use the '+' concatenation operator. If you want to add a character to the beginning of every element of a String array you would use the concatenation operator in a loop. Once you've made the changes to the array you can then assign it back to the Lines property if you want those changes displayed in the TextBox:
    Code:
    string[] lines = this.textBox1.Lines;
    
    for (int i = 0; i < lines.Length; i++)
    {
        lines[i] = "*" + lines[i];
    }
    
    this.textBox1.Lines = lines;
    Note that Lines is not a live property. By that I mean it is not an existing object that the property returns a reference to. A new array is created each time you get the property. For that reason I have got it only once in the code above. That also means that if you want the changes you make reflected in the TextBox you must assign the changed array back to the property, as I have done in the code above.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] probably a simple solution...

    Thanks. I thought it would be something similar to that.

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