Link to home
Start Free TrialLog in
Avatar of mascoloj
mascolojFlag for United States of America

asked on

Replace function help

I have a simple code snippet that I am having some problems with and I think I am just missing something stupid. In my code I want to change a textbox value according to the value of a checkbox. If the checkbox is checked, take the textbox value and put it in parentheses (), if the checkbox is unchecked remove them. My issue is removing them and my code is below. Any help would be great. Currently the result of the code does not remove them. Maybe you can't replace something with nothing?


PROBLEMATIC CODE:
txtposition.Text.Replace("(", "")
txtposition.Text.Replace(")", "")

CODE USAGE:
Dim Checked As Integer
If ckbvisible.Checked = False Then
    txtposition.Text = "(" & txtposition.Text & ")"
    Checked = 0
ElseIf ckbvisible.Checked = True Then
     txtposition.Text.Replace("(", "")
     txtposition.Text.Replace(")", "")
     Checked = 1
End If
ASKER CERTIFIED SOLUTION
Avatar of Bane83
Bane83
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mascoloj

ASKER

OK, So working with your comment, I replaced the code with the following (used M for lack of better thought) but it still didn't work. If I read the code I would assume that it would find ')' and replace it with 'M' but it doesn't. Maybe I am just missing the understanding of your comment. Here's the code I used:

txtposition.Text.Replace("(", "M")
txtposition.Text.Replace(")", "M")


Maybe changing my thinking is needed. Instead of replacing the '(' and ')' with a replace function, could I instead remove the '(' and ')' if they exist with nothing another way? I also have to work with the possibility that the user might remove the '(' and ')' manually in which case the code should not find anything to remove.
I think maybe you missed my change.  Look carefully at the change I made for the case where ckbVisible.Checked = true:

     txtposition.Text = txtposition.Text.Replace("(", "")
     txtposition.Text = txtposition.Text.Replace(")", "")

Notice that I'm assigning the result back to the Text property of txtPosition?
Your right - I totally missed it. Thanks for pointing it out. I'm closing the question and assigning you the points. Thanks again for my stupid simple mistake! I knew it was simple.