[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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
0
mascoloj
Asked:
mascoloj
  • 2
  • 2
1 Solution
 
Bane83Commented:
The Replace function returns a string.  You're calling the function, but not assigning the returned value to anything.

Dim Checked As Integer
If ckbvisible.Checked = False Then
    txtposition.Text = "(" & txtposition.Text & ")"
    Checked = 0
ElseIf ckbvisible.Checked = True Then
     txtposition.Text = txtposition.Text.Replace("(", "")
     txtposition.Text = txtposition.Text.Replace(")", "")
     Checked = 1
End If
0
 
mascolojAuthor Commented:
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.
0
 
Bane83Commented:
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?
0
 
mascolojAuthor Commented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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