none
If Radiobutton's checked then text is added before current text in textbox? RRS feed

  • Question

  • Hello :D

    I'm trying to make it so that, when a radiobutton gets checked, some text is added before the current text within a textbox. I've managed to do that, but how do I make it so that when it gets unchecked, the text that was previously added gets removed?

    This is my code:

        Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
            If RadioButton6.Checked = True Then
                TextBox1.Text = "example" & TextBox1.Text
            Else TextBox1.Text = "" & TextBox1.Text
            End If
        End Sub

    I can see why the Else statement here doesn't work to clear the added text, but I tried :P

    So how do I clear just the added text?

    Thanks!

    Sunday, October 29, 2017 9:33 PM

Answers

  • Hi

    If you store the added text in a variable, then, you can do something like this on a Button click

        Dim texttoadd As String = "example "
        TextBox1.Text = texttoadd & TextBox1.Text
    ' -----------------------------

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If TextBox1.Text.StartsWith(texttoadd) Then TextBox1.Text = TextBox1.Text.Replace(texttoadd, Nothing) End If End Sub



    Regards Les, Livingston, Scotland

    • Marked as answer by Lucy Qu Sunday, October 29, 2017 10:31 PM
    Sunday, October 29, 2017 10:04 PM
  • I can see why the Else statement here doesn't work to clear the added text, but I tried :P

    The Replace method can be used to remove a substring.

        Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
            Dim InsertText As String = "example"
            If RadioButton6.Checked = True Then
                TextBox1.Text = InsertText & TextBox1.Text
            Else
                TextBox1.Text = TextBox1.Text.Replace(InsertText,"")
            End If
        End Sub

    If the substring does not exist in the text then nothing happens.  If you only want to remove it if it appears as the initial part of the text then you will need to check before replacing it:

      If TextBox1.Text.StartsWith(InsertText) Then ...

    • Marked as answer by Lucy Qu Sunday, October 29, 2017 10:31 PM
    Sunday, October 29, 2017 10:27 PM

All replies

  • Hi

    If you store the added text in a variable, then, you can do something like this on a Button click

        Dim texttoadd As String = "example "
        TextBox1.Text = texttoadd & TextBox1.Text
    ' -----------------------------

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If TextBox1.Text.StartsWith(texttoadd) Then TextBox1.Text = TextBox1.Text.Replace(texttoadd, Nothing) End If End Sub



    Regards Les, Livingston, Scotland

    • Marked as answer by Lucy Qu Sunday, October 29, 2017 10:31 PM
    Sunday, October 29, 2017 10:04 PM
  • I can see why the Else statement here doesn't work to clear the added text, but I tried :P

    The Replace method can be used to remove a substring.

        Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
            Dim InsertText As String = "example"
            If RadioButton6.Checked = True Then
                TextBox1.Text = InsertText & TextBox1.Text
            Else
                TextBox1.Text = TextBox1.Text.Replace(InsertText,"")
            End If
        End Sub

    If the substring does not exist in the text then nothing happens.  If you only want to remove it if it appears as the initial part of the text then you will need to check before replacing it:

      If TextBox1.Text.StartsWith(InsertText) Then ...

    • Marked as answer by Lucy Qu Sunday, October 29, 2017 10:31 PM
    Sunday, October 29, 2017 10:27 PM
  • Thanks all.
    Sunday, October 29, 2017 10:31 PM