Adblock breaks this site

automatically add - to a multiline textbox

Discussion in 'Programming General' started by WalangAlam, May 8, 2007.

  1. WalangAlam

    WalangAlam Guest

    Referrals:
    0
    automatically add - to a multiline textbox

    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
     
  2. dodge

    dodge Active Member
    Banned

    Joined:
    Mar 26, 2007
    Posts:
    125
    Referrals:
    1
    Sythe Gold:
    5
    automatically add - to a multiline textbox

    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
     
  3. hmm

    hmm Active Member

    Joined:
    Jan 21, 2007
    Posts:
    181
    Referrals:
    2
    Sythe Gold:
    5
    automatically add - to a multiline textbox

    use what he said..and for the dash its
    Code:
    Text1.Text = Text1.Text & vbNewLine & "-" & Data
    
    guessing "Data" is a variable.
     
  4. WalangAlam

    WalangAlam Guest

    Referrals:
    0
    automatically add - to a multiline textbox

    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?
     
  5. Covey

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816
    automatically add - to a multiline textbox

    Code:
    If e.KeyCode = Keys.Control Then
    If e.Modifiers = Keys.Enter Then
    text1 = text1 & "-"
    End If
    End If
     
  6. dodge

    dodge Active Member
    Banned

    Joined:
    Mar 26, 2007
    Posts:
    125
    Referrals:
    1
    Sythe Gold:
    5
    automatically add - to a multiline textbox

    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
     
  7. dodge

    dodge Active Member
    Banned

    Joined:
    Mar 26, 2007
    Posts:
    125
    Referrals:
    1
    Sythe Gold:
    5
    automatically add - to a multiline textbox

    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 "-"
     
  8. WalangAlam

    WalangAlam Guest

    Referrals:
    0
    automatically add - to a multiline textbox

    thanks a lot dodge, it finally worked!
     
  9. Covey

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816
    automatically add - to a multiline textbox

    Thats what ya get when i don't know much about .net AND throw something together on the spot based on vb6.
     
< Saving and retrieving image from SQL Server database | Loading a form into a tabpage >


 
 
Adblock breaks this site