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.
First, I am not a programmer, I mainly just do simple scripts however there are somethings that are just easier to do in VB, I am pretty much self taught so forgive me if this sounds basic or if I can't explain it to well.
I have run into an issue trying to load a multi-column text file into a list box. There are two separate issues.
First issue is to read the text file and only grab the first column to use in the listbox, I am currently using ReadAllLines to copy the text file to a string first.
Dim RDPItems() As String = IO.File.ReadAllLines(MyDocsDir & "\RDPservers.txt")
However I am having a difficult time finding the correct code to only grab the first Column of this string to put in the listbox, if I use the split option I get an error that "Value of type '1-dimensional array of String' cannot be converted to 'String'"
The code looked like
frmRDP.lstRDP.Items.Add() = Split(RDPItems, ";", CompareMethod.Text)
This is the first hurdle, the second issue is what I want to do is if an item is selected from the List box, the value of the second column gets pulled into a variable to use.
This part I'm not even sure where to begin.
Example data of the text file
Server1 ; 10.1.1.1:3389
Server2 ; 192.168.1.1:8080
Server3 ; 172.16.0.1:9833
.....
When it's working the application will read a text file with a list of servers and their IPs and put the servers in a listbox, when you select the server from the listbox it and click a connect button it will then launch
c:\windows\system32\mstsc.exe /v:serverip
Any help would be appreciated, as I can hard code a large list of this into the VB application it would be easier to just have a text file with a list of servers and IPs to load instead.
The best practise for this would probably be to store your "columns" in a Dictionary. Declare this at class level (that is, outside any Sub or Function):
Dim Servers As New Dictionary(Of String, String)
When you load your items you read the file line-by-line, adding the items to the Dictionary and the ListBox at the same time:
Using Reader As New IO.StreamReader(IO.Path.Combine(MyDocsDir, "RDPservers.txt")) 'Open the file.
While Reader.EndOfStream = False 'Loop until the StreamReader has read the whole file.
Dim Line As String = Reader.ReadLine() 'Read a line.
Dim LineParts() As String = Line.Split(New String() {" ; "}, StringSplitOptions.None) 'Split the line into two parts.
Servers.Add(LineParts(0), LineParts(1)) 'Add them to the Dictionary. LineParts(0) is the name, LineParts(1) is the IP-address.
lstRDP.Items.Add(LineParts(0)) 'Add the name to the ListBox.
End While
End Using 'Dispose the StreamReader.
(Note that I used IO.Path.Combine() instead of simply concatenating the strings. I recommend using that instead for joining paths together)
Now, whenever you want to get the IP-address from the selected item you can just do for example:
Dim IP As String = Servers(lstRDP.SelectedItem.ToString())
Hope this helps!
EDIT:
Missed that you wanted to start a process with it... But it's like charliefox2 wrote:
Process.Start("c:\windows\system32\mstsc.exe", "/v:" & Servers(lstRDP.SelectedItem.ToString()))
Edit: #Visual Vincent's answer is way cleaner. I'll leave mine, but I recommend using his solution instead. That said, scroll down a little for how to open the server. He's got that too! Upvote his answer, and mark it as correct!
It looks like you're trying to split an array. Also, ListBox.Items.Add() works a bit differently than the way you've written your code. Let's take a look.
ListBox.Items.Add() requires that you provide it with a string inside the parameters. So you would do it like this:
frmRDP.lstRDP.Items.Add(Split(RDPItems, ";", CompareMethod.Text))
But don't do that!
When you call Split(), you must supply it with a string, not an array. In this case, RDPItems is an array, so we can't split the entire thing at once. This is the source of the error you were getting. Instead, we'll have to do it one item at a time. For this, we can use a For Each loop. See here for more info if you're not familiar with the concept.
A For Each loop will execute a block of code for each item in a collection. Using this, we get:
For Each item In RDPItems
Dim splitline() As String = Split(item, ";") 'splits the item by semicolon, and puts each portion into the array
frmRDP.lstRDP.Items.Add(splitline(0)) 'adds the first item in the array
Next
OK, so that gets us our server list put in our ListBox. But now, we want to open the server that our user has selected. To do that, we'll need an event handler (to know when the user has double clicked something), we'll have to find out which server they selected, and then we'll have to open that server.
We'll start by handling the double click by creating a sub to deal with it:
Private Sub lstRDP_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstRDP.MouseDoubleClick
Next, we'll get what the user has selected. Here, we're setting selection equal to the index that the user has selected (in this case, the first item is 0, the second is 1, and so on).
Dim selection As Integer = lstRDP.SelectedIndex
Lastly, we need to open the server. I'm assuming you want to do that in windows explorer, but if I'm mistaken please let me know.
Dim splitline() As String = Split(RDPItems(selection), ";")
Dim location As String = Trim(splitline(1))
We'll need to split the string again, but you'll notice this time I'm choosing the item whose location in the array is the same as the index of the list box the user has selected. Since we added our items to our listbox in the order they were added to our array, the first item in our listbox will be the first in the array, and so on. The location of the server will be the second part of the split function, or splitline(1). I've also included the Trim() function, which will remove any leading or trailing spaces.
Finally, we need to connect to our server. We'll use Process.Start() to launch the process.
Process.Start("c:\windows\system32\mstsc.exe", "/v:" & location)
For future reference, to first argument for Process.Start() is the location of the process, and the second argument is any argument the process might take (in this case, what to connect to).
Our final double click event handler looks something like this:
Private Sub lstRDP_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstRDP.MouseDoubleClick
Dim selection As Integer = lstRDP.SelectedIndex
Dim splitline() As String = Split(RDPItems(selection), ";")
Dim location As String = Trim(splitline(1))
Process.Start("c:\windows\system32\mstsc.exe", "/v:" & location)
End Sub
A final note: You may need to put
Dim RDPItems() As String = IO.File.ReadAllLines(MyDocsDir & "\RDPservers.txt")
outside of a sub, and instead just inside your class. This will ensure that both the click handler and your other sub where you populate the list box can both read from it.
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.
I am completely new to VB.net and have only been learning in for a few weeks
I am doing a project where i need to make an EPOS systems using notepad as a data base. I am able to make the values of the buttons appear in the list box, however I have numerous buttons all with different values but only the first value in the text box is appearing each time a different button is pressed.
E.G
When Heineken button pressed "Heineken €5.00" is displayed
when Guiness button pressed "Heineken €5.00" is displayed
Any help is greatly appreciated!
Imports System.IO
Public Class Form1
Private Sub btnHeineken_Click(sender As Object, e As EventArgs) Handles btnHeineken.Click
Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
'File DATABASE.TXT is the the debug folder
Dim name As String
Dim stock, price As Double
name = sr.ReadLine
stock = CDbl(sr.ReadLine)
price = CDbl(sr.ReadLine)
lstBox.Items.Add(name & "" & FormatCurrency(price))
name = sr.ReadLine
End Sub
Private Sub BtnGuiness_Click(sender As Object, e As EventArgs) Handles BtnGuiness.Click
Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
'File DATABASE.TXT is the the debug folder
Dim name As String
Dim stock, price As Double
name = sr.ReadLine
stock = CDbl(sr.ReadLine)
price = CDbl(sr.ReadLine)
lstBox.Items.Add(name & "" & FormatCurrency(price))
name = sr.ReadLine
End Sub
DATBASE.txt
Heineken
5.00
20
Guiness
4.50
50
Bulmers
5.00
25
Both your methods have exactly the same code. Thus, they do exactly the same thing: They show the contents of the first entry in your text file.
If you want your methods to do different things, you need to put different code in them.
Unfortunately, putting arbitrary code in your methods won't make them do what you want. It looks like you already discovered that. So the next step is to take a more structured approach:
Decide what your button click should do. It looks like you already did that: You want to display "Guiness €4.50" when the "Guiness" button is clicked.
Next, think about how your program can do that. Apparently, that's where you are stuck. You have a text file with a list of entries, how do you get the one you want?
Translate the result of step 2 (the "algorithm") in code.
You tried to do step 3 before step 2. That won't work, and that's the reason why your code doesn't work.
I suggest that you think really hard about step 2 (How do I find data in a text file? How would I do it if I had the file printed out in front of me and were searching for the data personally?), come up with an algorithm and then return here and ask a new question if you need help translating it to code.
I'm novice and I'm trying to understand how to get information from a webpage, I've already read about HtmlAgilityPack and I'm using it, but after 2 days trying to understand how I can do this, here am I asking for help.
Ok, the thing is: I want to read some informations from a page and write it in a label text.
The page I'll use as example is: http://www.tibia.com/community/?subtopic=characters&name=Huur
I want to show in different labels the level, the vocation and the guild informations...
But, all I got is this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myWeb As HtmlWeb = New HtmlWeb
Dim myDoc = myWeb.Load("http://www.tibia.com/community/?subtopic=characters&name=" & TextBox1.Text.Trim())
Dim myRoot As HtmlNode = myDoc.DocumentNode
Dim myElements As List(Of HtmlElement) = New List(Of HtmlElement)
Dim MainContentArea As HtmlNode
myWeb.Load("http://www.tibia.com/community/?subtopic=characters&name=" & TextBox1.Text.Trim())
MainContentArea = myDoc.GetElementbyId("characters")
TextBox2.Text = MainContentArea.InnerHtml
End Sub
As you guys can see, I found a way to read all the character informations, but I don't know how to find the thing that I want that is: level, vocation and guild informations and show it in differents labels text.
Can you guys help me please? :}
(In the code I'm using Textbox2.Text to show the page content cause it shows alot of things and I've got errors when trying to show the content in a label text.)
Sorry for the bad english guys.
First I would suggest looking at xpath if you aren't familiar with it. Secondly, you will need to figure out your html structure. You can use Firefox and go to what you are looking for and right-click on inspect element. It will lay out the structure of the document and give you information you can use for the xpath.
For instance if you want to get the level you can use "/html/body//div[#class='BoxContent'/table/body/tr[td='Level:']/td" to get the element that contains the level indicator and then move to the HtmlNode.NextSibling to get the next element whose text contains the value of the level you are looking for.
I hope that is enough to get you started.