• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1601
  • Last Modified:

Remove 2 lines from a textbox (VB.NET)

Hi Experts,

My WinForm as a Textbox(multi-line) and a Button.

The data in the Textbox is as follows: e.g.
1: Static String
2: Rand String
3: Rand String
4: Rand String
5: Static String

The Static String Lines never change.. so I want to only keep the Rand String and remove line 1 & 5. Lines 1 & 5 are not same line of text either.

I searched the web looking for examples and this one was close but not quite what I had in mind.

TextBox1.Lines = New String() _
 {"somethingt", "something else"}
        Dim a As String() = TextBox1.Lines
        Dim b(TextBox1.Lines.Length - 1) As String
        TextBox1.Clear()
        For i As Integer = 1 To a.Length - 1
            b(i - 1) = a(i)
        Next
        TextBox1.Lines = b

Open in new window


So, search textbox for "specific line of text?", then remove it. and also copy all leftover text to clipboard ;)

Thanks,
Roberto
0
RobertoFreemano
Asked:
RobertoFreemano
  • 3
1 Solution
 
RobertoFreemanoAuthor Commented:
Tried this piece of code, based off net example

   Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text.Contains("This is an example1") And TextBox1.Text.Contains("This is an example2") Then
            TextBox1.Text = TextBox1.Text.Replace("This is an example1", "") And TextBox1.Text.Replace("This is an example2", "")
        End If
    End Sub

Open in new window

But there's an issue after AND...

System.InvalidCastException was unhandled
  Message=Conversion from string "
This is a Server
This is a Se" to type 'Long' is not valid.
  Source=Microsoft.VisualBasic
  StackTrace:
       at Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
       at WindowsApplication1_bin.Form1.Button2_Click(Object sender, EventArgs e) in C:\Users\bob\AppData\Local\Temporary Projects\WindowsApplication1_bin\Form1.vb:line 20
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1_bin.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.FormatException
       Message=Input string was not in a correct format.
       Source=Microsoft.VisualBasic
       StackTrace:
            at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String Value, NumberFormatInfo NumberFormat)
            at Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
       InnerException:
0
 
RobertoFreemanoAuthor Commented:
This works
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text.Contains("This is an example1") Then
            TextBox1.Text = TextBox1.Text.Replace("This is an example1", " ")
        ElseIf TextBox1.Text.Contains("This is an example2") Then
            TextBox1.Text = TextBox1.Text.Replace("This is an example2", " ")
        End If
    End Sub

Open in new window

But it takes two button clicks to achieve it... I want one click action.
0
 
RobertoFreemanoAuthor Commented:
Ok resolved it

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text.Contains("This is an example1") Then
            TextBox1.Text = TextBox1.Text.Replace("This is an example1", " ")
        End If
        If TextBox1.Text.Contains("This is an example2") Then
            TextBox1.Text = TextBox1.Text.Replace("This is an example2", " ")


        End If
    End Sub

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now