1

I have 5 checkboxes and 5 Textboxes. I an trying to Replace characters in a string by checking checkboxes. for example if I checked checkbox 2 and 3 then only 2 and 3 textboxes characters should be replace. if I check checkbox 1 then only 1 textbox characters should replace in string. it pick only one checkbox value only.

If CheckBox1.Checked Then
    Dim x As String = Label9.Text
    Dim y As String = x.Replace(x.Substring(1, 2), ComboBox2.Text)
    Label8.Text = y
End If
If CheckBox2.Checked Then
    Dim x As String = Label9.Text
    Dim y As String = x.Replace(x.Substring(15, 2), word)
    Label8.Text = y
End If
If CheckBox5.Checked Then
    Dim x As String = Label9.Text
    Dim y As String = x.Replace(x.Substring(13, 2), ComboBox5.Text)
    Label8.Text = y
End If

If CheckBox8.Checked Then
    Dim x As String = Label9.Text
    Dim y As String = x.Replace(x.Substring(2, 1), ComboBox6.Text.Substring(1, 1))
    Label8.Text = y
End If
3
  • You are replacing the text in Label8, so that it will contain only the text from the last checkbox checked. It is difficult to understand what you are doing. Can you give us examples together with the expected output? Commented Dec 5, 2021 at 13:06
  • label8.text = label9.text .. .....label9 have string for example label9 ="ihavenothing12". so i enter "had" in textbox1 and check checkbox1 then label8 = ihadnothing12 Commented Dec 5, 2021 at 13:10
  • If Label9.Text = "ABC", ComboBox2.Text = "123", ComboBox6.Text.Substring(1, 1) = "Z", CheckBox1.Checked = True, and CheckBox8.Checked = True, then what should the value of Label8.Text be? Commented Dec 5, 2021 at 17:56

2 Answers 2

1

It is not a good idea to store the texts in labels and to apply the logic directly to those controls. You need a data model that makes your logic independent of your controls. The controls are used for input and output, i.e., for the interaction with the user. The data model on the other side reflects the structure of the problem and makes it easier to work with.

Example:

' Data model
Dim sentence As String() = {"I", "have", "nothing", "12"}
Dim replacement As String() = {"You", "had", "something", "8"}
' If you must fill combo boxes, you might need a more complex model, with a list of
' a displayable object containing an array. But I try to keep my answer simple to
' demonstrate the separation of concerns.

Based on this, you can initialize your controls. Assuming that the combo boxes are named ComboBox0 through ComboBox3 (rename them if not).

' Model to UI
Label9.Text = String.Join(" ", sentence)

For i As Integer = 0 To replacement.Length - 1
    Controls("ComboBox" & i).Text = replacement(i)
Next

Then apply your logic

Dim result As String() = New String(sentence.Length - 1) {}
For i As Integer = 0 To sentence.Length - 1
    ' UI to model (where isChecked models the checked state of a ComboBox)
    Dim isChecked As Boolean = CType(Controls("ComboBox" & i), ComboBox).Checked

    ' Logic applied to model
    If isChecked Then
        result = replacement(i)
    Else
        result = sentence(i)
    End If
Next

Finally, output the result:

' Model to UI
Label8.Text = String.Join(" ", result)

You could also use String.Split instead, to initialize the word arrays:

Dim sentence As String() = "I have nothing 12".Split(" "c)
Dim replacement As String() = "You had something 8".Split(" "c)

This is only the first step in separating business logic and code related to UI. A better solution would be to place all of the business logic into its own class. See also: Robert’s Rules of Coders: #11 Separate User Interface Logic From Business Logic

Sign up to request clarification or add additional context in comments.

2 Comments

i need it in vb
Sorry. Replaced by VB code now.
0

Try the following:

Dim y as String
Dim x As String = Label9.Text
If CheckBox1.Checked Then
    y = x.Replace(x.Substring(1, 2), ComboBox2.Text)
End If
If CheckBox2.Checked Then
    y = x.Replace(x.Substring(15, 2), word)
End If
If CheckBox5.Checked Then
    y = x.Replace(x.Substring(13, 2), ComboBox5.Text)
End If
If CheckBox8.Checked Then
    y = x.Replace(x.Substring(2, 1), ComboBox6.Text.Substring(1, 1))
End If
Label8.Text = y

2 Comments

it is stopping after one checkbox checked condition. if i check two checkboxes it is not replacing two checkboxes values in label8
@pankajbabbar Well, yes. That is the way you wrote the code. At each True If you have declared a new 'y'. Label8.Text will only contain the most recent value of y. Even if you declared y before the Ifs you would just be overwriting it for each True If. If you intend to append the text then do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.