is it possible to change a Char into a String with a loop/if - vb.net

I got a knew problem which should be the last one from my bonus exercise. At first let's explain the rules, I have a word which is shuffle. I want my user to find this word and he has a determined number of tries.
Now I want to let the user know which char from his input (txtBox.Text) was right and which one aren't. So I tried to create a method which is able to Color a char in green If the char is correctly positionned else in red. I ask my teacher and it seems to be a hard thing to do, I tried to find the solution he gave me with richBox but it's way too hard right now, i'm struggling way to much.
So ! I think of something way simpler, or at least i thought it was simpler, I get the user input in a String and I do a loop around it, every time the correct word and the input char doesn't match I replaced it by a dot.
It doesen't work either if I try to put an index to my :
If word(i) IsNot proposition(j) Then
I'm facing an out of bound and if I try without it, it consider my string as an array of 1 and add to my listbox a single dot.
Here's my code :
Public Sub charRight&Wrong()
Dim proposition() As String = {txtInput.Text} //get the proposition from the user
Dim dot As Char = "."
Dim word() As String = {theWord} //theWord represent the right answer
For i = 0 To theWord.Length - 1
For j = 0 To proposition.Length - 1
If word(i) IsNot proposition(j) Then
proposition(j) = dot
End If
Next
Next
lboAttempts.Items.AddRange(proposition.ToArray)
End Sub
I don't really know where I'm wrong I hope you can point it out. Thanks again.

Related

Reverse number array multiline textboxes

basically, I want to reverse the numbers. (in the textbox there will be only 2-digit numbers)
if I have Textbox1.text:
12
2
41
71
70
I want to display in the box (Textbox1.text)
21
2
14
17
70
Function:
Public Shared Function Reverse(num As Integer) As Integer
Dim _reverse As Integer = 0
While num <> 0
_reverse *= 10
_reverse += num Mod 10
num \= 10
End While
Return _reverse
End Function
it should work, it actually works, but I don't know how to arrange it to work in all lines.
For Each lines In TextBox1.Lines
Dim rev = Reverse(lines)
lines.Replace(lines, rev)
Next
This is a perfect example of what happens when people try to write code without knowing what the code is supposed to. What the code is supposed to do is not just the end result but the steps to get there. If you don't know what the steps are then you shouldn't be writing any code because it's unlikely that what you write will do anything useful. Code is simply an implementation of logic so you should be getting the logic down first. It doesn't take any programming experience to work out the logic because we could all do this if it was a manual process and that would be the same logic.
So, what are the steps involved?
Get the lines of the text.
Loop over the lines.
Reverse the current line.
Replace the original line with the result of reversing.
Replace the text with the complete results.
If you actually consider each of those steps, it should be obvious that you cannot use a For Each loop because that will only let you get data out of a list, not put data into it. That would make it obvious that a For loop is the right choice, because will let you get data out and put it in. Now you can write code that actually does something useful.
Dim lines = TextBox1.Lines
For i = 0 To lines.GetUpperBound(0)
Dim line = lines(i)
Dim number = CInt(line)
Dim result = Reverse(number)
lines(i) = result.ToString()
Next
TextBox1.Lines = lines
Simple stuff but, again, if you don't know what the code has to actually do, writing code to do it is a challenge. Always break the problem down into smaller parts first, so you can work on each part individually, and always work out the logic you're trying to implement - and test that logic manually - before trying to write code to implement it.

task 1 for project

I need to produce code within Visual Basic that identify's a words position. For example, my sentence could write 'This is my Visual Basic Project'. If the user entered the word 'my', the output will open another form displaying 'Your word is in the 3rd position'. Its required to use strings then split it into an array, then using the match function give each word individual properties/positions.
I am fairly new to programming and would love any help. I would appreciate it if you could return some code for my design e.g buttons and listboxes. I have tried incredibly hard to get this program fully functioning but i'm finding it very challenging.
Really please. Many thanks!!
First of all, I am not a Visual Basic or .NET person, but I really liked the problem and so optimization of my code is possible . I am little confused by, what do you mean by match function. Are you looking for REGEX or something for string matching over here?
Anyways, based on your description, I tried to code something for you, which I think is something what you are looking for.
CODE:
The whole logic is within the click of the button "FIND POSITION OF WORD". Split the sentence then compare the entered word with each word in sentence
Public Class FindTheWord
Private Sub buttonFindTheWord_Click(sender As Object, e As EventArgs) Handles buttonFindTheWord.Click
Dim inputSentence As String = TextBox1.Text
Dim inputWord As String = TextBox2.Text
Dim SplittedSentence As String() = inputSentence.Split(" ")
Dim Position As Integer = 0
For Each word In SplittedSentence
Position = Position + 1
If (word = inputWord) Then
MessageBox.Show("Your word is at position : " + Position.ToString)
End If
Next
End Sub End Class
Hope this helps.

Replacing nothing with a string

This may be a simple question, but I have searched quite a few sites and have tried a few of my own ideas and I still cannot seem to find a simple way to get Visual Studio to replace all of the listbox items with the string of nothing with some other text.
Using things such as :
For Each S In ListBox1.Items
S.Replace("", "Not Blank")
Next
Shows:
Error
String cannot be of zero length
Which is quite annoying because the actual listbox item contains no text.
This seems to be one of the easiest things I have ever encountered while using vb.net. But it now seems very hard for what should be a simple command.
A couple of problems. The Replace function returns a new value, and you promptly ignore it. Second, you can't really modify the collection as you For-Each over it, so a For-Loop would be more appropriate.
I think you want something like this instead:
For i As Integer = 0 To ListBox1.Items.Count - 1
If String.IsNullOrEmpty(ListBox1.Items(i).ToString) Then
ListBox1.Items(i) = "Not Blank"
End If
Next

How to restrict swear word input when using a textbox within Visual Basic

I have had several ideas yet most of the solutions online only restrict certain keys or only numbers, not full strings.
I want a solution that as soon as a keypress of a swear word has been inputted, VB detects it then disallows it. I have been able to do exact string matching so if a user puts the work F*** in, then it shows an error message, but this would not work if the user decided to put in "F***nugget". It would not be elegant nor time feasible to enter every possible combination of swear words. Thanks in advance for help.
If e.KeyChar = "F***" Then
SelectionTextBoxTeamName.Clear()
MessageBox.Show("Please choose something else")
You need a 'string contains' function.
If you're writing in vb.net try String's Contains function, which is used like this:
Dim b As Boolean
b = s1.Contains(s2)
If you're not writing in .net
try InStr function. You can use it as follows:
InStr("find the comma, in the string", ",")
EDIT: after op's comment
If you want to check several swears you can put them in an array and check for each:
Dim swears = {"F***", "S***"}
Dim hasSwears As Boolean = false
For Each swear As String In swears
If InStr(textBox.Text, swear) > 0 Then
hasSwears = true
End If
Next
Now you know if there are any swears according to hasSwears.

VS 2010 macro - select from here to there

I'm writing a macro to let me replace the spaces in a string in my code file with underscores. I've gotten as far as finding the beginning and end of the string as instances of VirtualPoint. Now I'm trying to select from the first VirtualPoint to the second. And I can't figure it out.
I know the VirtualPoints are correct, because I'm using MessageBox.Show to tell me their values when I run the macro. I just don't know the correct command to set the TextSelection from the first to the second. I've tried this:
selection.MoveToPoint(firstVirtualPoint)
selection.MoveToPoint(secondVirtualPoint, True)
This seems like it ought to work, but it doesn't. The cursor just moves to the end of the line (as far as I can tell).
Does anybody know the correct command for doing this?
As these things tend to go, after I give up, suddenly it hits me. Perhaps this will help someone else, though.
A fuller sample of the code is this:
Dim selection As TextSelection =
CType(DTE.ActiveDocument.Selection, TextSelection)
selection.StartOfLine()
selection.FindText("some string at start")
Dim pointAfterStart = selection.BottomPoint
selection.FindText("some string at end")
Dim pointBeforeEnd = selection.TopPoint
selection.MoveToPoint(pointAfterIt)
selection.MoveToPoint(pointBeforeLambda, True)
The idea is to find the initial text, then the ending text, and then select everything in between. What I found in the debugger was that the values in pointAfterStart and pointBeforeEnd were changing. Perhaps deceived by the name (since System.Drawing.Point is a struct), I wasn't realizing they were references that pointed to the current selection position.
I solved it this way:
selection.FindText("It ")
Dim pointAfterIt = selection.BottomPoint.CreateEditPoint
selection.FindText(" = () =>")
Dim pointBeforeLambda = selection.TopPoint.CreateEditPoint
This made copies of the selection points, so that they didn't change when the selection moved later.