• Log in or sign up for an account here: https://www.sythe.org/login

automatically add - to a multiline textbox

  • Thread starter Thread starter WalangAlam
  • Start date Start date
W

WalangAlam

Guest
how to automatically add dash (-) to the beginning of the next line of a multiline textbox when the user press ctrl+Enter. It will serve like a bullet everytime the user enter a new in the multiline textbox

-Entry One
-Entry Two
-Entry Three
 
on your keydown event do somthign like

If e.KeyCode = Keys.Enter Then
If e.Modifiers = Keys.Control Then
'do some code here
End If
End If

You can also do this:

If e.KeyCode = Keys.Enter Then
If e.Control Then
'do some code here
End If
End If

this will capture the ctrl+Enter key combination then you can just write some logic to do what you want
 
use what he said..and for the dash its
Code:
Text1.Text = Text1.Text & vbNewLine & "-" & Data

guessing "Data" is a variable.
 
Text1.Text = Text1.Text & vbNewLine & "-" & Data

the code above results in a space above the first text line and everytime i press control + enter

--------------------------------------------------------
(space is created here by the code)
(cursor goes here everytime i press control + enter)

-Entry One
-Entry Two
-

--------------------------------------------------------

how about a code that will press the dash (-) for me everytime i press control + enter. how to do that?
 
two things

1
If e.KeyCode = Keys.Control Then
If e.Modifiers = Keys.Enter Then

it should be

If e.KeyCode = Keys.Enter Then
If e.Modifiers = Keys.Control Then

the enter key is not the Modifier in this case

2
you cant just do it like this:
Text1.Text = Text1.Text & vbNewLine & "-" & Data

this way all the text will have a "-" in front instead the single line

you will have to find what is ur current line then change only that like

hint: TextBox1.lines and TextBox1.SelectionStart
 
here after spending a few minutes on this here is how you do it

Code:
If e.KeyCode = Keys.Enter Then
            If e.Modifiers = Keys.Control Then
                Dim SelectionStart As Integer = TextBox1.SelectionStart
                Dim LineIndex As Integer = TextBox1.GetLineFromCharIndex(SelectionStart)
                Dim LineArray() As String = TextBox1.Lines
                LineArray(LineIndex) = "-" & TextBox1.Lines(LineIndex)
                TextBox1.Lines = LineArray
                TextBox1.SelectionStart = SelectionStart + 2
            End If
        End If

you may want to add some error handling and also handle when there is already a "-" at the beginning of a line, you can do this by comparing id the first character in the line is a "-"
 
two things

1
If e.KeyCode = Keys.Control Then
If e.Modifiers = Keys.Enter Then

it should be

If e.KeyCode = Keys.Enter Then
If e.Modifiers = Keys.Control Then

the enter key is not the Modifier in this case

2
you cant just do it like this:
Text1.Text = Text1.Text & vbNewLine & "-" & Data

this way all the text will have a "-" in front instead the single line

you will have to find what is ur current line then change only that like

hint: TextBox1.lines and TextBox1.SelectionStart

Thats what ya get when i don't know much about .net AND throw something together on the spot based on vb6.
 
Runescape Gamblingrunescape gambling
Back
Top