Results 1 to 8 of 8

Thread: Making 'cut' menu items for specific places in a text box - Windows Form Application

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Post Making 'cut' menu items for specific places in a text box - Windows Form Application

    So basically I'm wanting to make a 'cut' menu item for each specific area of a textbox, for example: 'cut left' (numbers on image) 'cut right' (symbols on image) 'cut middle' (letters on image). I'm also looking to make a 'clean' menu item which removes all symbols from the textbox - how would I achieve these two things?

    cut tool sample.txt

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    You need to make your own contextmenustrip. When you say 'cut left' are you meaning the left part of the current line or the left part of all lines.
    Also have a look at the clipboard object, which has a SetText method. You want to look at String.Split too...

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    Thank you for your reply, sorry I'm not really sure what I'm looking for exactly (I'm very new to programming) and I mean part of all the lines.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    Ok, so you want to take, for example, the left part of all lines. What do you plan to do with those parts? Then, do you want to take the middle parts, or right parts, at different times? So for example you might have 3 buttons. But what you plan to do with those substrings is most important at this stage...

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    In that example, I'm looking to 'cut' (remove) part of all those lines but I'm struggling as I'm not familiar with substrings at this stage.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    Try this...

    Attachment 181301

    Code:
    Public Class Form1
    
        Private leftParts As New List(Of String)
        Private middleParts As New List(Of String)
        Private rightParts As New List(Of String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'cut left
            If leftParts.Count = 0 Then
                leftParts = New List(Of String)
                Dim lines() As String = TextBox1.Lines
                For x As Integer = 0 To lines.Count - 1
                    If lines(x).Trim = "" Then Continue For
                    leftParts.Add(lines(x).Substring(0, lines(x).IndexOf("    ")))
                    lines(x) = lines(x).Replace(leftParts.Last & "    ", "")
                Next
                TextBox1.Lines = lines
                MsgBox(String.Join(Environment.NewLine, leftParts.ToArray))
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'cut middle
            If middleParts.Count = 0 Then
                middleParts = New List(Of String)
                Dim lines() As String = TextBox1.Lines
                For x As Integer = 0 To lines.Count - 1
                    If lines(x).Trim = "" Then Continue For
                    Dim parts() As String = lines(x).Split(New String() {"    "}, StringSplitOptions.RemoveEmptyEntries)
                    middleParts.Add(If(parts.Length = 3, parts(1), parts(0)))
                    lines(x) = lines(x).Replace(middleParts.Last & "    ", "")
                Next
                TextBox1.Lines = lines
                MsgBox(String.Join(Environment.NewLine, middleParts.ToArray))
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            'cut right
            If rightParts.Count = 0 Then
                rightParts = New List(Of String)
                Dim lines() As String = TextBox1.Lines
                For x As Integer = 0 To lines.Count - 1
                    If lines(x).Trim = "" Then Continue For
                    Dim parts() As String = lines(x).Split(New String() {"    "}, StringSplitOptions.RemoveEmptyEntries)
                    rightParts.Add(parts.Last)
                    lines(x) = lines(x).Replace(rightParts.Last, "")
                Next
                TextBox1.Lines = lines
                MsgBox(String.Join(Environment.NewLine, rightParts.ToArray))
            End If
        End Sub
    
    End Class

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    That's exactly what I needed thank you! Is it possible for when pressing one of the 3 buttons, for it to ask how many characters across you'd like to cut?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Making 'cut' menu items for specific places in a text box - Windows Form Applicat

    All 3 of the Button_Click handlers i posted there, just use the 4 character spaces " "
    How do you mean by asking how many characters across? If, for example, you have "123456" do you mean a subset of that string, or a subset of leftPart, or a subset of rightPart? If you're talking all of the leftPart and part of the middlePart, it'd have to be rewritten..

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