I'm trying to obtain a random generated output with this kind of options:
Firstly a letter which could be R or L
Secondly a number that could only be 15, 30, 45 or 60.
For example an output could be R45 or L15.
All this should be generated randomly every time the slide is opened.
I tried with this code but without results, no output appears in the label. Can someone give me a hint?
signA = "R"
signB = "L"
cont = Int(Rnd * 100) + 1
Angle = Int(Rnd * 120) + 1
valore = cont Mod 2
If valore = 0 Then
If Angle <= 30 Then
vardec = "15"
var = signA & vardec
Else
If Angle <= 60 Then
vardec = "30"
var = signA & vardec
Else
If Angle <= 90 Then
vardec = "45"
var = signA & vardec
Else
vardec = "60"
var = signA & vardec
End If
End If
End If
Else
If Angle <= 30 Then
vardec = "15"
var = signA & vardec
Else
If Angle <= 60 Then
vardec = "30"
var = signA & vardec
Else
If Angle <= 90 Then
vardec = "45"
var = signA & vardec
Else
vardec = "60"
var = signA & vardec
End If
End If
End If
End If
ActivePresentation.Slides(4).Shapes("Label2").OLEFormat.Object.Caption = CStr(var)
The instructions actually work, i tested it on a text label, just check you're referencing the label correctly.
Related
The code must add and subtract points based on the following criteria.
+5 for having one upper case
+5 for having one lower case
+5 for one digit
+5 for having one of these symbols !"$%^&()_-
-5 only containing upper case
-5 for only containing lowercase
-5 for only containing digits
-5 for only containing symbols
I am struggling to make the code add 5 points once per criteria instead of multiple times any help would be appreciated in VB.net. Sorry forgot to post my code.
points = Len(password)
counter = 1
While counter < Len(password) + 1
letter = GetChar(password, counter)
convletter = Asc(letter)
'Ascii values for uppercase exclusive
If 64 < convletter < 91 Then
'Adds 5 points
points = points + 5
'Ascii values for lowercase letters exclusive
ElseIf 96 < convletter < 123 Then
'If letter is lowercase + 5 points
points = points + 5
'Ascii values for digits exclusive
ElseIf 47 < convletter < 58 Then
points = points + 5
End If
counter = counter + 1
End While
Console.WriteLine(points)
Console.ReadLine()
I need the code to cycle through the letters of the string but only add 5 points once instead of repeatedly how could I solve this. *Sorry I'm new round here
Try this:
Dim symbols = "!""$%^&()_-"
Dim plusses = { _
password.Any(Function(c) symbols.Contains(c)), _
password.Any(Function(c) Char.IsDigit(c)), _
password.Any(Function(c) Char.IsUpper(c)), _
password.Any(Function(c) Char.IsLower(c)) _
}.Where(Function (x) x).Count() * 5
Dim minusses = { _
password.All(Function(c) symbols.Contains(c)), _
password.All(Function(c) Char.IsDigit(c)), _
password.All(Function(c) Char.IsUpper(c)), _
password.All(Function(c) Char.IsLower(c)) _
}.Where(Function (x) x).Count() * 5
Dim score = plusses - minusses
Dim UpCase As Boolean = False
Dim LowCase As Boolean = False
Dim Digit As Boolean = False
Dim Symbol As Boolean = False
Dim UpCaseOnly As Boolean = False
Dim LowCaseOnly As Boolean = False
Dim SymbolOnly As Boolean = False
Dim DigitOnly As Boolean = False
Dim Password As String = "!" & chr(34) & "/$%?&*()-_"
Dim Points As Integer = Password.Length
'check password
For Each xChar As Char In Password
Select Case xChar
Case "A" To "Z"
UpCase = True
Case "a" To "z"
LowCase = True
Case "0" To "9"
Digit = True
Case "!" To "/", "_" 'ascii 33 to 47 !"/$%?&*()- and 95 _ '
Symbol = True
End Select
Next
If (Symbol = True) And (UpCase = False) And (LowCase = False) And (Digit = False) Then SymbolOnly = True
If (Symbol = False) And (UpCase = True) And (LowCase = False) And (Digit = False) Then UpCaseOnly = True
If (Symbol = False) And (UpCase = False) And (LowCase = True) And (Digit = False) Then LowCaseOnly = True
If (Symbol = False) And (UpCase = False) And (LowCase = False) And (Digit = True) Then DigitOnly = True
'check for points
If SymbolOnly = True Then Points -= 5
If UpCaseOnly = True Then Points -= 5
If LowCaseOnly = True Then Points -= 5
If DigitOnly = True Then Points -= 5
If (LowCaseOnly = False) And (LowCase = True) Then Points += 5
If (UpCaseOnly = False) And (UpCase = True) Then Points += 5
If (DigitOnly = False) And (Digit = True) Then Points += 5
If (SymbolOnly = False) And (Symbol = True) Then Points += 5
I want to deal with the arrangement of the buttons. for example i have 10 buttons, i want that after 5 buttons the next 5 buttons will go to the nextline.
Here is the code that i have used:
For i = 1 To 10
Dim btn As New Button
btn.Width = 40
btn.Height = 30
btn.TextAlign = ContentAlignment.MiddleCenter
If i.ToString.Length = 1 Then
btn.Text = "B" & "0" & i
Else
btn.Text = "B" & i
End If
btn.Visible = True
btn.Tag = "Button" & i
Panel1.Controls.Add(btn)
If i <= 5 Then
btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10)
Else
btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10 * 1 + ((i - 1) * btn.Height))
End If
i get the wrong positioning of the buttons. kindly help me on this.
i always get this kind of position. ex:
* * * * *
*
*
*
*
*
What i want is this:
* * * * *
* * * * *
additional: How can i do it with backgroundworker...?
Assuming you are using winforms, instead of trying to position your buttons by hand, I would suggest you use a FlowLayoutPanel control instead of a straight up panel. Then you can just add them and let the panel manage their positions.
For i = 1 to 10
Dim btn As New Button
btn.Width = 40
btn.Height = 30
btn.TextAlign = ContentAlignment.MiddleCenter
If i.ToString.Length = 1 Then
btn.Text = "B" & "0" & i
Else
btn.Text = "B" & i
End If
btn.Visible = True
btn.Tag = "Button" & i
FlowLayoutPanel1.Controls.Add(btn)
Next
If you must have 5 per line (assuming your panel is wide enough), you can use SetFlowBreak:
For i = 1 to 10
'.....
FlowLayoutPanel1.Controls.Add(btn)
'Use this line if you must have only 5 buttons per line.
if i Mod 5 = 0 Then FlowLayoutPanel1.SetFlowBreak(btn, true)
Next
Try this:
If i <= 5 Then
btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10)
Else
btn.Location = New Point(10 * 1 + ((i - 6) * btn.Width), 10 + btn.Height)
End If
EDIT:
If you wanted to change the loop so that you wanted to multiple lines of buttons then look at this:
Dim noOfButtonsPerLine As Integer = 5
Dim buttonIndex As Integer = 0
Dim y As Integer = 10
For i = 1 To 15
Dim btn As New Button With {.Height = 40, .Width = 30}
If buttonIndex = noOfButtonsPerLine Then
buttonIndex = 1
y += btn.Height
Else
buttonIndex += 1
End If
btn.TextAlign = ContentAlignment.MiddleCenter
If i.ToString.Length = 1 Then
btn.Text = "B" & "0" & i
Else
btn.Text = "B" & i
End If
btn.Visible = True
btn.Tag = "Button" & i
Panel1.Controls.Add(btn)
btn.Location = New Point(10 * 1 + ((buttonIndex - 1) * btn.Width), y)
Next
Change the variable noofButtonsPerLine to what suits you. I've gone for 5 as per the question but you can change it and it should adapt.
I have a problem with my blackjack game in vb.net. This code I have will add the player's score perfectly, but when it comes to the dealer's score, it will not. It only takes the second card that the dealer has.
It is called with this:
addScore("p") 'add player's score
addScore("d") 'add dealer's score
And this is "addScore()":
Public Function card(player As String, index As Integer) As Label
Try
If player = "p" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "YouCard" & index.ToString()).Single()
ElseIf player = "d" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "DealerCard" & index.ToString()).Single()
End If
Catch
Return Nothing
End Try
End Function
Public Sub addScore(ByVal player As String)
Dim currScore As Integer
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (currScore + 11) <= 21 Then
result = currScore + 11
ElseIf card(player, value).Text = "A" AndAlso (currScore + 1) <= 22 Then
result = currScore + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = currScore + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = currScore + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
currScore shouldn't be there. Replace it with result
Public Sub addScore(ByVal player As String)
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (result + 11) <= 21 Then
result = result + 11
ElseIf card(player, value).Text = "A" AndAlso (result + 1) <= 22 Then
result = result + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = result + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = result + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
I am currently programming a sheet which visualizes data sets in graphs. Because the user of this sheet will not need all the graphs, I would like to let them choose the ones needed through a UserForm. Since the amount of data sets is variable, the UserForm will have the same amount of checkboxes as there are datasets.
The Userform code is as follows.
Private Sub UserForm_Initialize()
Dim chkBoxA As MSForms.CheckBox
Dim chkBoxB As MSForms.CheckBox
Dim lblBox As MSForms.Label
Dim cnt As Control
Amount = Sheet4.Range("C4").Value 'Amount of datasets
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
CommandButton1.Left = 20
CommandButton1.Top = 40 + ((Amount - 1) * 40)
CommandButton1.TabIndex = Amount * 3 + 1
Me.Height = 220
Me.ScrollBars = fmScrollBarsVertical
Me.ScrollWidth = Me.InsideWidth * 9
For Each cnt In Me.Controls
If cnt.Top + cnt.Height > Me.ScrollHeight Then
Me.ScrollHeight = cnt.Top + cnt.Height + 5
End If
Next
End Sub
When the UserForm is filled in (graphs are chosen by clicking on the options), the user will press CommandButton1. An event should then be run to show the correct graph, but for the simplicity I am first testing if a MsgBox will show up. Unfortunately the MsgBox does not show up.
Private Sub CommandButton1_Click()
'Will fix this with a loop
If A1 = True Then
MsgBox ("TestA1")
End If
If B1 = True then
MsgBox ("TestB1")
End If
If A2 = True then
MsgBox ("TestA2")
End If
Unload Me
End Sub
I am stuck on this part. The checkboxes do show up on the UserForm and they are clickable, but the commandbutton only shuts down the sub (Unload Me). I would like to see the MsgBox show up when I select the corresponding option and click the commandbutton. Any help on getting this to work is appreciated!
You are referencing 'A1' in the sub, but that variable does not exitst at compile time, because you add them dynamically. What you need to do is loop the controls, to check the names. Best practice is to put the checkboxes in a frame, to be able to group them.
Add a frame to the userform and name it 'checkboxframe'
And then instead of:
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
you would need to do:
With Me.checkboxframe
For i = 1 To Amount
Set lblBox = .Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = .Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = .Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
End With
And to add the checkboxes to the frame, use something like:
For Each ctr In UserForm1.frame("checkboxframe").Controls
If TypeName(ctr) = "CheckBox" Then
If ctr.Value = True Then
'do something usefull here
msgbox ctr.name
End If
End If
Next ctr
The reason nothing appears is because there is no object "A1" manually defined as a variable.
To get the value of the box you Dynamically named "A1" you would have to refer to it as such:
If Me.Controls.Item("A1").Value = True then
Hope this helps!
For some reason when I run this it only ever calculates to 0
Where am I going wrong? :(
The user has 3 input boxes to place values. From there those values should be calculating. It only ever equals a value of 0. I get no errors
Option Strict On
Public Class Form1
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
Dim dblPacA, dblPacB, dblPacC, dblAnswerA, dblAnswerB, dblAnswerC, dblGrandTotal As Double
Dim dblAnswerA1, dblAnswerB1, dblAnswerC1 As Double
'Packages Retail
Dim dblPACA_FACTOR As Double = 99
Dim dblPACB_FACTOR As Double = 199
Dim dblPACC_FACTOR As Double = 299
'Rate of each range
Dim dblTENNINE_FACTOR As Double = 0.8
Dim dblTWONINE_FACTOR As Double = 0.7
Dim dblFIVENINE_FACTOR As Double = 0.6
Dim dblONETEN_FACTOR As Double = 0.5
Try
'important calculate
dblAnswerA1 = dblPacA * dblPACA_FACTOR
dblAnswerB1 = dblPacB * dblPACB_FACTOR
dblAnswerC1 = dblPacC * dblPACC_FACTOR
dblPacA = CDbl(txtPacA.Text)
dblPacB = CDbl(txtPacB.Text)
dblPacC = CDbl(txtPacC.Text)
dblGrandTotal = dblAnswerA + dblAnswerB + dblAnswerC
lblGrandTotal.Text = "Gran Total:" & (dblGrandTotal.ToString("c"))
'lblAnswer.Text = dblAnswer.ToString
'lblAnswer.Text = "PackageA:" & dblAnswerA _
' & "PackageB:" & dblAnswerB & "PackageC:" _
'& dblAnswerC & "GrandTotal:" & dblGrandTotal
Catch
End Try
If dblPacA >= 0 Then
If dblPacA < 10 Then
dblAnswerA = dblAnswerA1
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 10 And dblPacA < 20 Then
dblAnswerA = dblAnswerA1 * dblTENNINE_FACTOR
lblAnswerA.Text = "PackageA:" & (dblAnswerA.ToString("c"))
ElseIf dblPacA >= 20 And dblPacA < 50 Then
dblAnswerA = dblAnswerA1 * dblTWONINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 50 And dblPacA < 100 Then
dblAnswerA = dblAnswerA1 * dblFIVENINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblAnswerA >= 100 Then
dblAnswerA = dblAnswerA1 * dblONETEN_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
End If
Else
MessageBox.Show("txtPacA must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacB >= 0 Then
If dblPacB >= 10 And dblPacB <= 19 Then
dblAnswerB = dblAnswerB1 * dblTENNINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 20 And dblPacB <= 49 Then
dblAnswerB = dblAnswerB1 * dblTWONINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 50 And dblPacB <= 99 Then
dblAnswerB = dblAnswerB1 * dblFIVENINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
Else
dblAnswerB = dblAnswerB * dblONETEN_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
End If
Else
MessageBox.Show("txtPacB must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacC >= 0 Then
If dblPacC >= 10 And dblPacC <= 19 Then
dblAnswerC = dblAnswerC1 * dblTENNINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 20 And dblPacA <= 49 Then
dblAnswerC = dblAnswerC1 * dblTWONINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 50 And dblPacC <= 99 Then
dblAnswerC = dblAnswerC1 * dblFIVENINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
Else
dblAnswerC = dblAnswerC1 * dblONETEN_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
End If
Else
MessageBox.Show("txtPacC must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
End Class
dblAnswerA1 = dblPacA * dblPACA_FACTOR
where is dblPacA 's value set? its not. its 0