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 8 years ago.
Improve this question
I am making a hangman game for my son, in VB.net. I have made buttons for each letter, and i have also made a list of words. The problem I am encountering is when I am trying to print the guessed letters to the labels. I donĀ“t know how to this. Can anyone help me please?
I will try to help you although you haven't show your code.
A simple approach is:
Private sWord As String 'Your word here
Dim arrayLetters As Array
arrayLetters = sWord.ToCharArray
For i = 0 To arrayLetters.Length - 1
Dim lbl As New Label
lbl.Text = "_"
lbl.Tag = arrayLetters(i)
lbl.AutoSize = True
Me.FlowLayoutPanel1.Controls.Add(lbl) ' Assuming that you have added a FlowLayoutPanel in your form to handle your labels (AutoSizeMode=GrowAndShrink)
Next
Now you need a sub to check the if the user has pressed the right letter:
Private Sub CheckLetter(ByVal letter As Char)
For Each lbl As Label In FlowLayoutPanel1.Controls
If lbl.Tag = letter Then
lbl.Text = letter
Else
'Whatever you like if the user make a mistake
End If
Next
End Sub
Now in the event that handles the buttons click
CheckLetter("Here you put the corresponding letter")
Of course you can have one event to handle all the letters (or even use keyboard for input),add capital letters etc.
Show us your efforts
Related
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Some basic terms that interchange that i have no one to ask, and i guess to stupid to understand my self.
Method,procedure, parameters, operation, operator, function, instaniantiantion, separator,
I really need help!
I really need to see a long module of code that every thing is broken down for a stupid guy like me.
For instance break this down, not what it does but how it does it, and the terminology. Word by word,, all dots, = parentheses, the passes the gets all that stuff, I know that this is simple to everyone else. And i will need more help until i get it... Maybe we can work something out. Some one that is available,,, that knows this stuff backwards and forwards and has the patience, that would like to help me... I have tried books i just do not get it HELP
Public Class ViewerForm1
Private Sub btnSelectPicture_Click(sender As Object, e As EventArgs) Handles btnSelectPicture.Click
'Show the open dialog box.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
'Load the picture in the box.
btnSelectPicturePicture.Image = Image.FromFile(ofdSelectPicture.FileName)
'Show the name of the file in the forms caption.
Me.Text = "Picture Viewer "(ofdSelectPicture.FileName)
'Close the window at exit the application.
Me.Close()
End If
End Sub
Private Sub btnSelectPicture_Click(sender As Object,
e As EventArgs) Handles btnSelectPicture.Click
This line declares a default Click event handler and binds button's click event to it.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
Opens an open file dialog and if user presses OK, processes the result.
btnSelectPicturePicture.Image = Image.FromFile(ofdSelectPicture.FileName)
Loads the image into the button, based on the file previously selected.
Me.Text = "Picture Viewer "(ofdSelectPicture.FileName)
Line does not make sense and probably won't compile. Missing a & or a + for string concatenation. Assuming it had one, it would assign a form caption/title to a constant value + a file name that was selected.
Me.Close()
Closes the current form.
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
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 is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I would like to add a counter to a power point presentation. Someone mentioned to me that this might be doable in VBA. Do you know if this could be done in VBA and how?
Basically here is what I would like to do:
display a counter representing for example the number of cars rented since the beginning of my presentation. So for example, at the start the counter is at 0 and every minute is incremented of 2000 (this is just an example). We can see the counter on every slide, so at the end of my talk people can see (and I'll tell them) that since the beginning of the talk X(large number) cars have been rented.
I tried to find something on the internet but without success... I hope someone will be able to help me?
I give you some ideas. Possibly they will be helpful even I do not provide any code.
Generally you need to have something like 'timer' in your presentation which would start with your presentation and count the time used. Unfortunately there is nothing like this in PowerPoint. You could possibly use some external solution like C# COM add-in but it's rather very complicated.
You could use PP application events but value of the car will not change every minute but every new slide you enter or any other event fires (like moving reverse, etc). It's a bit complicated but within our (StackOverflow users) knowledge.
You could possible search or ask under that link where I used to find lot's of interesting ideas.
I promised to provide solution therefore I'd like to do it even the question is closed. Therefore I do it by re-edition of that answer which I hope is allowed.
We have to be sure that there is a 'text box' where 'count value' would be placed on each slide. Add the following code into Module1 and run it.
Sub Add_CarValue_Text()
Dim SLD As Slide, SHP As Shape, shCarValue As Shape
Dim boCarValue As Boolean
For Each SLD In ActivePresentation.Slides
For Each SHP In SLD.Shapes
If SHP.Name = "CarValue" Then
boCarValue = True
Exit For
End If
Next
If Not boCarValue Then
Set shCarValue = SLD.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 0, 150, 50)
With shCarValue
.Name = "CarValue"
.TextFrame.TextRange.Text = "Cars counter: "
End With
End If
boCarValue = False
Next
End Sub
Add new Class Module and place below code there. Change if necessary.
Public WithEvents PPApp As Application
Private TimerStart As Long
Private Const increasePerMinute = 1000
Private Sub PPApp_SlideShowBegin(ByVal Wn As SlideShowWindow)
TimerStart = Int(Timer)
End Sub
Private Sub PPApp_SlideShowNextSlide(ByVal Wn As SlideShowWindow)
If Not Wn.View.Slide.Shapes("CarValue") Is Nothing Then
Dim Lap As Integer
Lap = (Int(Timer) - TimerStart) / 10 'change for 60 to change from 10sec to 1 min
Wn.View.Slide.Shapes("CarValue").TextFrame.TextRange = "Cars volume: " & Lap * increasePerMinute
End If
End Sub
Add the following code to Module2 and run the procedure.
Public tmpPPApp As New AppClass
Sub StartUp()
Set tmpPPApp.PPApp = PowerPoint.Application
End Sub
Start your presentation.
Important! If you change anything in code please run step 3 again. Moreover, just in case, you need to run procedure 3 always before you lunch the presentation.