Perform Swap between textboxes without variables - vb.net

I was attending an interview, the interviewer asked me the question that: Perform Swap between textboxes without variables. i am familiar with c/c++ concept of swap values without using temp, i tried them but it wont give me the result, can anyone suggest me how it become possible?
Thanks in advanceā™„

Using some string handling functions we can easily swap(Interchanging) the contents of two text boxes. see the following code snippet :
Private Sub btnSwap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text &= TextBox1.Text
TextBox1.Text = TextBox2.Text.Substring(0,TextBox2.Text.LastIndexOf(TextBox1.Text))
TextBox2.Text = TextBox2.Text.Replace(TextBox1.Text, "")
End Sub
How It Works:
Concatenate both texts and assign it into TextBox2
Find the SubString based on the index of TextBox1.Text in the concatenated string which gives the TextBox2.Text and are assigned to the TextBox1.Text.
Replace the TextBox1.Text from the Concatenated string will give the initial text in the TextBox1.Text which is assigned to the TextBox2.Text
Example
Let TextBox1.Text="aaa" and TextBox2.Text="bbaaabb"
Step 1: TextBox2.Text= "aaabbaaabb"
step 2: TextBox1.Text= "bbaaabb" ' since textbox2 contains aaabbaaabb and textbox1 contains aaa
step 3: TextBox2.Text= "aaa" 'since textbox2 contains bbaaabb and textbox1 contains bbaaabb

Related

I want to replace some characters from a string. I used the Replace command. But this is not working

I am writing some code in VB.Net to subtract one string from another string, but this is not working. in output nothing is changed in the target string. But there is no error message. Please help. Thanks.
If RadioButton1.Checked Then
TextBox1.Text = ""
positive = (TextBoxp1.Text + TextBoxp2.Text + TextBoxp3.Text)
negative = (TextBoxn1.Text + TextBoxn2.Text + TextBoxn3.Text)
findstring = Replace(positive, negative, "")
TextBox1.Text = findstring
End If
The concatenation symbol in vb.net is the ampersand (&). You may get unexpected results it you use the plus sign and the strings contain numbers. Parenthesis are not necessary to evaluate an expression except to establish order of calculation when it conflicts with order of precedence.
You are using the vb.net Strings.Replace method. I would use the .net String.Replace method because it is easier to move between .net languages when you get used to using .net methods instead of vb specific methods.
This method takes the original string in this case negative and looks for the entire positive string. If it finds the entire string it replaces it with the empty string.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim positive = "b" & "cd" & "ef"
Dim negative = "abc" & "def" & "ghi"
TextBox1.Text = negative.Replace(positive, "")
'Result is aghi
End Sub
If you are trying to remove individual letters from a string then you will have to use a loop. Luckily for us a String is an array of Char.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim positive = "ceg"
Dim negative = "abcdefg"
For Each ch As Char In positive
negative = negative.Replace(ch, "")
Next
TextBox1.Text = negative
'Result abdf
End Sub
You are making this way too complicated. If what you want is to remove a substring from within a string use replace like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = Replace(txtLargeString.Text, txtSearchString.Text, "")
End If
End Sub
All you need is two radio buttons, three text boxes and a button. If you enter 1121221114141 in the txtLargeString text box, 2122 in the txtSearchString text box and execute the code, the result is 111114141 which is the result of removing the txtSearchString input from the txtLargeString input.
Or if as #Mary suggested you want to use the more modern version of replace use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = txtLargeString.Text.Replace(txtSearchString.Text, "")
End If
End Sub

How do I validate user input?

So I'm having a problem with my code. I need to validate if the user input is a number or not in a textbox. Now I can get it to see whether or not it is a number and it displays the error message just fine but the problem is that the word still gets inputted in to the textbox when I want there to be only numbers
If tried using if not IsNumeric(Number) then
msgbox.show("ERROR! Data must be a number!")
'Getting user input
Dim Number As String = Me.InputTextbox.Text
UnitsTextbox.AppendText(Environment.NewLine & Number)
'Make the textbox delete the text once the button is clicked
InputTextbox.Text = String.Empty
If Not IsNumeric(Number) Then
MsgBox("ERROR! Data must be a number")
End If
I'm expecting it to accept numbers only
i have a text box for input and a textbox for the results and when the number comes up false I want it to not show in the results textbox
As per #Jens comments, only I changed it to .TryParse. IsNumeric is an old VB6 method that has been optimized in .Net to TryParse.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
InputTextbox.Text = String.Empty
End If
End Sub

how to enumerate listbox?

Hi I'm new to Visual Studio and I am in need of a help in Visual Studio on a Windows Form App. also I don't really know if this is vb.net
I would like to make an application where if a user inputs something in the TextBox, it is presented in the ListBox whilst having some sort of an enumeration.
like for example.
If I type "Wow amazing" in the TextBox and confirm it. Then type another text like "I love you" in the TextBox and confirm it again
It should show up in the Listbox as "1. Wow Amazing" and "2. I love you".
Here's my code. I am not able to get it right and I don't really know how. I tried doing the for Loops and Do While but it would just duplicate the texts or am I doing something wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 0
ListBox1.Items.Add(i + 1 & ". " & TextBox1.Text)
End Sub
You are so close!
On every button click you need to:
Take number of elements in list, to determine the number.
Insert text concatenated with number.
So, you should use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Step 1
Dim i As Integer = ListBox1.Items.Count + 1
'Step 2
ListBox1.Items.Add(i & ". " & TextBox1.Text)
End Sub

How to check a text box for a certain character and replace with a different string in the same spot

I am working on a little morse code translation project and I cannot figure out how to detect when a certain key is in a textbox and replace it with the corresponding morse code dots and dashes in the correct spot.
For example if you type in "a b c" then i would like the program to check and put
".- -... -.-."
but it also needs to be dynamic so if you change up the order of your letters it can update the translation.
as of right now i have a key checking system where you can only type in one forward line and if you mess up you have to clear the whole box. thank you!
Here is a basic example of what I was suggesting in my comments above, i.e. using two separate TextBoxes and translating the whole text every time:
Private morseCodeDictionary As New Dictionary(Of Char, String) From {{"a"c, ".-"},
{"b"c, "-..."},
{"c"c, "-.-."}}
Private Sub inputTextBox_TextChanged(sender As Object, e As EventArgs) Handles inputTextBox.TextChanged
outputTextBox.Text = String.Join(" ",
inputTextBox.Text.
ToLower().
Select(Function(ch) morseCodeDictionary(ch)))
End Sub
Here's an implementation that doesn't use LINQ, so may be more understandable:
Private Sub inputTextBox_TextChanged(sender As Object, e As EventArgs) Handles inputTextBox.TextChanged
Dim characterStrings As New List(Of String)
For Each ch As Char In inputTextBox.Text
characterStrings.Add(morseCodeDictionary(ch))
Next
outputTextBox.Text = String.Join(" ", characterStrings)
End Sub

vb.net matching textbox.text with contents of listbox data

What I want to do is type a number in textbox1 and then match the word that corresponds to it for example
if textbox1.text = 6
then textbox2.text = searchable
but this list could be 10000 items long, so don't want to hard code it.
listbox contains the following data.I am open to changing the layout slightly if needed.
1 example
2 word
3 to
4 find
5 by
6 searchable
7 numbers
then upon button2 click my textbox2 would contain searchable (but not the number)
Thanks
Add this code in your button's click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) <= ListBox1.Items.Count - 1 Then '<--- check the textbox contains a number less than the item count of the list box
TextBox2.Text = ListBox1.Items(TextBox1.Text) '<---- replace the number with carasponding item in the listbox
End If
End Sub
Maybe use Listbox.Findstring(textbox1.text.trim & " ") or Listbox.FindString(Val(TextBox1.Text))
These return the index to the item you are looking for...