Generating Random Letters [closed] - vba

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 6 months ago.
Improve this question
I am very new in VBA and honestly no idea of the codes/functions needed to use and what their purpose are but I need to make a PowerPoint that includes a Command Button to create a random letter during the presentation. Can you give me the codes and if possible, explain why use those codes? Thank you very much!

A few Notes
This is set up such that it will occur on slide 1.
The text box which will contain the letter is named: "Random_Letters"
The active x command button which triggers the macro is named "Generate_Random_Letter"
Sub Generate_Random_Letter_Click()
' Read this stuff (or dont lol)
' vba number to letter (chr) https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/character-set-0127
' vba random https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/rnd-function
Randomize
Dim upperBound As Integer: upperBound = 90
Dim lowerBound As Integer: lowerBound = 65
Dim randomIndex As Integer: randomIndex = Int((upperBound - lowerBound + 1) * Rnd + lowerBound)
Dim p As Presentation: Set p = ActivePresentation
Dim s As Slide: Set s = p.Slides.Item(1)
Dim t As Shape: Set t = s.Shapes.Item("Random_Letters")
t.TextFrame.TextRange.Text = Chr(randomIndex)
End Sub
Good Luck, and remember to actually learn the content.

Related

How do I count files in a folder based on their sizes using visual basic .net (vb.net)? [closed]

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 4 years ago.
Improve this question
I want to count files in a folder based on their sizes. For example, how many files that are less than 512KB and how many files that are more than 512KB. Please help me.
Below sub-routine will help you to get the count
Sub GetFileDetails(ByVal sFolderPath As String, ByRef Filelessthan512KB As Integer, ByRef FileMorethan512KB As Integer)
Dim sFiles() As String = Directory.GetFiles(sFolderPath)
For Each file As String In sFiles
Dim oFileDetails As New FileInfo(file)
If (oFileDetails.Length / 1024) < 512 Then
Filelessthan512KB = Filelessthan512KB + 1
Else
FileMorethan512KB = FileMorethan512KB + 1
End If
Next
End Sub

Check how many times a character occurs in a word (VB) [closed]

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 4 years ago.
Improve this question
I have an array that contains characters converted from a word via .ToCharArray method. I would like to check how many times a letter occurred in this array (word). How can I do this?
One way is a lookup:
Dim letterLookup = yourCharArray.ToLookup(Function(c) c)
' For example the letter e, note that it's case sensitive '
Dim eLetterCount As Int32 = letterLookup("e"c).Count()
This is efficient and has the advantage that you can even check letters which aren't contained in the String/Char(), you will get 0 as result.
By the way, you don't need to use ToCharArray, you could use this code on with original string.
If you wanted to list all contained letters:
Dim allLetters As IEnumerable(Of Char) = letterLookup.Select(Function(kv) kv.key)
If you wanted to ignore the case, so treat e and E as equal:
Dim letterLookup = yourCharArray.tolookup(Function(c) c, StringComparer.CurrentCultureIgnoreCase)
For example the word 'tent'. The program would check which letter
occurs more than once (t) and find the position in the array (in this
case 0,3). It will also find the position in the array of the other
letters.
Then i would use a different approach also using LINQ:
Dim duplicateLetterPositions As Dictionary(Of Char,List(Of Integer)) = yourCharArray.
Select(Function(c, index) New With {.Char = c, .Index = index}).
GroupBy(Function(c) c.Char).
Where(Function(grp) grp.Count > 1).
ToDictionary(Function(grp) grp.Key, Function(grp) grp.Select(Function(x) x.Index).ToList())

I want to remove repeated characters in textbox [closed]

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 5 years ago.
Improve this question
I am using VB.Net.
I need to remove all the repeated characters in textbox
For Example:
myy naaaame isss Johnn
to
my name is John
can anyone help me out please?
So even I, knowing zilch about VB.NET and RegEx figured it out in like 20 mins:
Sub Main()
Dim input As String = "myy naaaame isss Johnn"
' You need a regex group that matches any char: (.)
' ... and a back reference: \1
' ... and a count more than one: {1,}
Dim rgx As New Regex("(.)\1{1,}")
' use the regex to Replace by the first char of the match group
Dim output As String = rgx.Replace(input, New MatchEvaluator(Function(ByVal m)
Return m.Value.First
End Function))
End Sub

How to read From Text Files line by line - multiple choice Quiz [closed]

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.

Hangman game - VB.net [closed]

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