Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write a macro that inserts "!!!" at the beginning of a textbox if there is a "XX" located in any part of that text box. Ideally the macro will run this procedure for every textbox in the presentation, but I can figure out how to loop it through if someone can help me with the basic procedure.
For example, a text box with the following text:
I ate XX hamburgers on XX/XX/20XX
would become
!!!I ate XX hamburgers on XX/XX/20XX
I hope this can help.
Sub test()
Dim TestString As String
TestString = "I ate XX hamburgers on XX/XX/20XX"
Variable = InStr(1, TestString, "X")
If Variable > 0 Then
output = "!! " & TestString
End If
Debug.Print output
End Sub
Here TestString = your input string
The InStr function tests if "X" is present in the string, if it is then "!!" is joined to the variable "Output"
This should be quite easy to adapt?
i hope this will help you.
Sub test()
Dim s As String
s = "Test XX"
If InStr(1, s, XX, vbTextCompare) Then
s = "!!!" + s
End If
MsgBox s
End Sub
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a textbox of maxLength 8. the 1st two characters must be "PM" or "00". I tried split(), but didnt work.
Use substring() method
Dim s As String = TextBox1.Text.Substring(0, 2)
If s = "PM" Or s = "00" Then
MessageBox.Show("good!")
Else
MessageBox.Show("bad!")
End If
Or you can use StartsWith()
If TextBox1.Text.StartsWith("PM") OR TextBox1.Text.StartsWith("00") Then
'Do something
End If
Another option is to use regular expressions:
Dim re As New Regex("PM|00")
If re.IsMatch(TextBox1.Text) Then
'do something
End If
The benefit is that when you decide what to do with the other 6 characters, you can modify the above to capture and return those (in full or part), without having to rewrite your code. You can even process multiple occurrences of PM|00 in one string and capture them all.
Useful resource, a Regex sandbox:
https://www.regex101.com/
Maybe you can try this:
if textbox1.text like "PM*" or textbox1.text like "00*" then
Do something
else msgbox("You don't have pm or 00 to start with!")
end if
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Hi i really need some help with this, I need to make a multiple choice question program that reads the questions and answers from 2 different text files (notepad files)
but when I try I cant seem to get it working. i have tried loops but that didn't work then I tried arrays but that didn't meet the requirements of reading form a text file
So I come to you all I need help is with reading a text file line by tine and then updating it when a new question needs to be given
I cannot 1 read the line by line (questions.txt) and i need to have match the question which are in answers.txt then i need to update it when next question is clicked
VB.Net
Program i need to create must do the following
Questions and answers should be loaded from a file called questions.txt and answers.txt respectively
-Questions should also appear in random order, every time program is executed
-update form for next question
-keep track on how many questions are correct
Any resources or tutorials on any of the above would be muchly appreciated
Total edits: 198305769 lol. Cleaned up the answer, and this should get you nearly complete. Cheers.
Declare a global variable (integer); that's where you'll assign the amount of questions the user has answered:
Public Class Form1
Dim keepScore As Integer = 0
Not the neatest, but it appends each line from a selected text file into an array and then you can iterate through it.
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
Dim xstr = ofd.FileName
Dim questions() As String = IO.File.ReadAllLines(ofd.FileName)
Dim answers() As String = IO.File.ReadAllLines(ofd.FileName)
Dim sw As New StringBuilder
Dim i As Integer = 0
Do Until i = questions.Count()
sw.AppendLine(Trim(questions(i)))
MsgBox(questions(i)) 'Only added this so you can see the lines
i = i + 1
Loop
Do Until i = answers.Count()
sw.AppendLine(Trim(answers(i)))
MsgBox(answers(i)) 'Only added this so you can see the lines
i = i + 1
Loop
Add onto the end of this function an if statement:
If CorrectAnswer.Checked = True 'Assuming you are using a RadioButton Group, or CheckBox
keepScore = keepScore + 1
End If
Here is a quick number randomiser, assuming you have 20 questions (change the 20 accordingly to whatever amount of questions you have):
Randomize()
Dim i As Integer = CInt(Int(20 * Rnd() + 1))
MsgBox(i)
Best of luck.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a quiz with an answer box and a check my answer box.
I put the code in and then when I put in the answer it says "wrong try again" and when I write nothing in the box then it says "correct well done."
The code I put in is below:
Private Sub CommandButton1_Click()
If TextBox1.Value = motherboard Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Private Sub CommandButton1_Click()
If TextBox1.Value = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Without Defining motherboard in another location, I am assuming you want the Literal answer to be the Text "motherboard" So you need to add quotes around the word.
The reason it accepts your answer when there isn't one is because when you add a variable that isn't defined it is defaulted with a value of nothing. So when you enter nothing it is a match to a variable that also equals nothing. By adding the quotes it makes it look for exactly that word.
You also might want to add some kind of filters to the incoming text to make it more likely for a correct answer that may have an extra space for a capital letter to be excepted.
Private Sub CommandButton1_Click()
If Trim(LCase(TextBox1.Value)) = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Adding the LCase command will make all entered text lower case. And Adding the Trim command will remove all white space before and after the word.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a large text file with over 100k of lines. Some of the lines are duplicates. I would like to be a to dedupe these entries before processing them. I am using visual basic 2010 Express to write this.
Text file example:
132165
165461
646843
654654
321358
132165
165461
I want to dedupe these entries before processing them
You can use a HashSet(Of T)
Dim nodupes As New HashSet(Of String)(File.ReadLines(path))
For Each str As String In nodupes
' no duplicate here '
Next
Edit Since a HashSet(Of T) does not guarantee to preserve the insertion order you can use following code if you need to ensure this order:
Dim nodupeSet As New HashSet(Of String)
Dim nodupes = From line In File.ReadLines(path)
Where nodupeSet.Add(line)
For Each str As String In nodupes
' no duplicate here '
Next
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using Visual Basic, I want to replace a text within a String with another String. I do not know the location of this text within the String. How can I accomplish this?
Thanks for any help.
Try:
X = X.Replace("original"," toReplace")
Or this link:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Input string.
Dim input As String = "Dot Net Not Perls"
' Use Regex.Replace with string arguments.
Dim output As String = Regex.Replace(input, "N.t", "NET")
' Print.
Console.WriteLine(input)
Console.WriteLine(output)
End Sub
End Module
Output
Dot Net Not Perls
Dot NET NET Perls
The replace method does not require you to know the location. You specify the original value, the string to look for, and the string to change it too. Very straight forward.
Dim aString As String = Replace("String to Search", "String to Find", "String to Replace With")