PDA

View Full Version : [VB.NET] A couple more questions~



Water
12-09-2014, 08:37 AM
I have a few more questions if anyone has time to answer!
1. I'm confused with the GetBetween Function without Regex, if anyone is able to link a good video or detailed answer I'd appreciate it!
2. When making a multi - account login, using a listbox, I want to be able to get the characters before and after :
So user:pass as x:y
then go ahead as login(x, y)
3. How would I go about loading a program correctly onto clraik?

Thank you! :$

DarkByte
12-09-2014, 10:53 AM
@Water ([Only registered and activated users can see links])

1. Getbetween without Regex

Extracting data from a string is simple let me explain how we would make a custom getstringbetween....

"This is a test 123456789 string"

We want to extract the numbers 123456789 in this e.g , so use 2 functions to do this , one is instr() the other is mid() , what in str does is to find the position of a string within a string , so if we say:



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thestring As String = "This is a test 123456789 string"
Dim pos1 = InStr(thestring, "test ")
MsgBox(pos1)
End Sub


this will show a message box with 11 , why? Because if you count the letters in "This is a test 123456789 string" , the 11th letter is the t in "test".

now we want to jump passed the "test " and to the start of the numbers , so we add the length of our search string to the value of pos1 , we could hard code it by just adding +5 but as we will turn out code into a function and the string we search for will be different each time , we wont take that route. instead lets build our function skeleton...




Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thestring As String = "This is a test 123456789 string"
Dim TheResult = GetstringBetween(thestring, "test ", " string")

End Sub

Private Function GetstringBetween(StringtoSearch As String, startstring As String, endstring As String)


Dim pos1 = InStr(StringtoSearch, startstring) + Len(startstring)
If pos1 < 0 Then
Return "-1"
End If


MsgBox(pos1)
End Function


now we are using len() to find the length of our search string and adding that to pos1 , this allows us to jump of the "test " word and now be at the begging of "12345678" (our code will now show 16 if we run it , 11 + len of "test " = 16... makes sense right? :P...

You will also notice I added this code:

If pos1 < 0 Then
Return "-1"
End If

instr returns -1 if our string is not found , so I return "-1" as a string , this will allow us to detect that what we wanted was not found and prevent our program crashing in later code.


So we now have the index of our starting search string... now we need the index of the end position too. instr actually has a third parameter (it gos before the other 2 params we already used), a start position , it tells the program to use instr() to find a position in a string , but to only start searching from where we say.. We will search for the end string but use the position we calculated for the starting search string to search from (pos1).




Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thestring As String = "This is a test 123456789 string"
Dim TheResult = GetstringBetween(thestring, "test ", " string")


End Sub

Private Function GetstringBetween(StringtoSearch As String, startstring As String, endstring As String)


Dim pos1 = InStr(StringtoSearch, startstring) + Len(startstring)
If pos1 < 0 Then
Return "-1"
End If

Dim pos2 = InStr(pos1, StringtoSearch, endstring)
If pos2 < 0 Then
Return "-1"
End If

MsgBox(pos2)
End Function



Now we get 25 , perfect :) we dont need to add the length or anything to this string because it is exactly where we need it to be (at the space after 9). So our code now knows that pos1 = 16 (position of our start search string) and pos2 = 25 (position of our end search string). Lets move on to mid()....


mid can extract a string from within a string , but we need to give it 3 params:

1.The string to extract from.
2.The starting position to extract from.
3.How many letters to extract... Take this e.g...




Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim teststring As String = "rdd rox ur sox but sucks at explaining things like this"

Dim test = Mid(teststring, 5, 3)
MsgBox(test)
End Sub



This would show "rox" because the r in rox is the 5th letter of teststring and we told the code to extract 3 letters... simple eh? Now to make use of this in our function , we already know the start string is pos1 so we can add that to our mid function secondly we need to know how many letters we need to extract , pos2 is the end position we are looking for , so pos2 - pos1 will be the amount of letters we want to extract... Knowing the lets throw it into some code and see how that works...



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thestring As String = "This is a test 123456789 string"
Dim TheResult = GetstringBetween(thestring, "test ", " string")
MsgBox(TheResult)

End Sub

Private Function GetstringBetween(StringtoSearch As String, startstring As String, endstring As String)


Dim pos1 = InStr(StringtoSearch, startstring) + Len(startstring)
If pos1 < 0 Then
Return "-1"
End If

Dim pos2 = InStr(pos1, StringtoSearch, endstring)
If pos2 < 0 Then
Return "-1"
End If

Dim finalstr As String = Mid(StringtoSearch, pos1, pos2 - pos1)
Return finalstr
End Function



We now get a message box with "123456789" perfect! Hope that explains GetStringBetween for you! I will get round to ur other questions in a hour or so I need to go out atm! This is rough code and not meant to replace any Getbetween function u already have as im sure thats better and been tested more , but you should have a grasp of how it works now!

---------- Post added at 09:53 AM ---------- Previous post was at 08:34 AM ----------



[Only registered and activated users can see links]



Dim pos1 = InStr(StringtoSearch, startstring) + Len(startstring)
If pos1 < 0 Then
Return "-1"
End If


do you see the bug? we add the len of the startstring to our code , so the if statement that follows will never actually be 0. So instead we can just say


Dim pos1 = InStr(StringtoSearch, startstring) + Len(startstring)
If pos1 = len(startstring) Then
Return "-1"
End If


Now that code works , if our startstring is not found , we get -1.



The second question....

A account manager is simple I dont recommend using a ":" in a list box as this means anyone can steal someone elses passwords if they have physical access to there pc while ur prog runs , lets make a custom class instead..... by right clicking our project files , going to add > class... like so..

[Only registered and activated users can see links]

I gave it the name of "AccountInfo.vb"

Now we can add a form and a list box with 2 buttons , 1 to add a new account and one to login to the selected account... it will look like this...

[Only registered and activated users can see links]

now lets edit our class file so it looks like this...



Public Class AccountInfo
Public username As String
Public password As String
End Class



Now jump into the code for our form , and lets edit that...



Public Class Form1

Dim Accountlist As New List(Of AccountInfo)


End Class



All we are doing here is making a place to store all out accounts.. This is called a buffer..

Next inside the add account button we will prompt the user to pass a username/password , how you do this is upto you but im just using a simple prompt box...



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str_username = InputBox("Enter a username")
Dim str_password = InputBox("Enter a password")
End Sub


Next we will make a new instance of our AccountInfo class and store that in our Accountlist buffer then list the accounts username in our list box...



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str_username = InputBox("Enter a username")
Dim str_password = InputBox("Enter a password")

Dim newaccount = New AccountInfo
newaccount.username = str_username
newaccount.password = str_password
Accountlist.Add(newaccount)
ListBox1.Items.Add(str_username)
End Sub




Next in our login button we want to add some code to log the currently selected username in... to do this we first make sure the user as a account in the listbox highlighted to prevent a error.... like so



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex = -1 Then
MsgBox("Select an account first , please!")
Exit Sub
End If
End Sub


After that we just want to grab the currently selected usernames info from our class to do this we loop each item in the Accountlist buffer , if the username of the current account is the same as the currently selected username in our listbox , we log them in....




Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex = -1 Then
MsgBox("Select an account first , please!")
Exit Sub
End If


For Each account In Accountlist
If account.username = ListBox1.SelectedItem.ToString Then
MsgBox("Logging into account " & account.username & " with password " & account.password)
''LOGIN CODE HERE...
Exit Sub

End If
Next
End Sub



If you must do it with a ":" this is simple to....

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thestring As String = "usernamehere:passwordhere"
Dim test = thestring.Split(":")
MsgBox("username = " & test(0) & " Password = " & test(1))
End Sub


You final question , how to upload to c.k is simple scroll to the top of this page and under the ck banner hit downloads. Just under where u pressed downloads there is a Add file button , hit that and follow the wizard. A programmer or mod / admin will approve your download when its been verified as not having password stealer/malware ect. This shouldn't take to long a few hours usually!

Water
12-09-2014, 11:52 AM
You must spread some Reputation around before giving it to DarkByte again.

Thank you so much for taking the time out to help me! :$
You really helped me understand the getbetween! I'll let you know how the class goes for the login as it is 4am and everything is turning into much
thanks a bunch!!