Masking textbox to accept only 1 digit after decimal - textbox

I am using VB6 and I want to allow user to enter only one digit after decimal,
please help.

Put the code below in change event of textbox. I think "." point is used for decimal seperator. if "," coma is used, then change the point in the code with coma.
Private Sub TextBox1_Change()
Dim strA As String
Dim intP As Integer
strA = TextBox1.Text
intP = InStr(1, strA, ".", vbTextCompare)
If intP > 0 Then TextBox1.Text = Left(strA, intP + 1)
End Sub

Related

regex for matching a string into words but leaving multiple spaces

Here's what I expect. I have a string with numbers that need to be changed into letters (a kind of cipher) and spaces to move into different letter, and there is a tripple spaces that represent a space in output. For example, a string "394 29 44 44 141 6" will be decrypted into "Hell No".
function string.decrypt(self)
local output = ""
for i in self:gmatch("%S+") do
for j, k in pairs(CODE) do
output = output .. (i == j and k or "")
end
end
return output
end
Even though it decrypts the numbers correctly I doesn't work with spacebars. So the string I used above decrypts into "HellNo", instead of expected "Hell No". How can I fix this?
You can use
CODE = {["394"] = "H", ["29"] = "e", ["44"] = "l", ["141"] = "N", ["6"] = "o"}
function replace(match)
local ret = nil
for i, v in pairs(CODE) do
if i == match then
ret = v
end
end
return ret
end
function decrypt(s)
return s:gsub("(%d+)%s?", replace):gsub(" ", " ")
end
print (decrypt("394 29 44 44 141 6"))
Output will contain Hell No. See the Lua demo online.
Here, (%d+)%s? in s:gsub("(%d+)%s?", replace) matches and captures one or more digits and just matches an optional whitespace (with %s?) and the captured value is passed to the replace function, where it is mapped to the char value in CODE. Then, all double spaces are replaced with a single space with gsub(" ", " ").

Making a print function repeat on a new line everytime it prints

So I want this final print function to print its function on a new line every time it prints. I've tried various "\n" placements to make it work but to no avail. Any tips?
from datetime import date
currentYear = date.today().year
print('Hi. What is your name?')
name = input()
while True:
try:
print('How old are you, ' + name + '?')
age = int(input())
if age >= 0:
break
else:
print('That is not a valid number.')
except ValueError:
print('That is not a valid number')
ageinHundred = 100 - int(age)
y = currentYear + int(ageinHundred)
t = 'You will be 100 years old in the year ' + str(int((y)))
print(t)
print('Give me another number')
num = input()
f = (int(num) * t)
print(f)
I want the final print function (print(f)) to print f multiple times on a new line each time. Not one after the other like the above code does now.
Thanks!
Change the last couple of lines to:
# Put t inside a list so it does list multiplication instead
# of string multiplication
f = int(num) * [t]
# Then join the individual f-lists with newlines and print
print("\n".join(f))
For the f = line, inspect f to get a better idea of what's going on there.
For the join part, join takes a list of strings, inserts the given string (in this case "\n"; a newline), and "joins" it all together. Get used to using join. It is a very helpful function.
Try this:
from datetime import date
currentYear = date.today().year
print('Hi. What is your name?')
name = input()
while True:
try:
print('How old are you, ' + name + '?')
age = int(input())
if age >= 0:
break
else:
print('That is not a valid number.')
except ValueError:
print('That is not a valid number')
ageinHundred = 100 - int(age)
y = currentYear + int(ageinHundred)
t = 'You will be 100 years old in the year ' + str(int((y)))
print(t)
print('Give me another number')
num = input()
for i in range(0,int(num)):
print(t)

Lua find operand in a string

I have a Lua string like "382+323" or "32x291" or "94-23", how can I check and return the position of the operands?
I found String.find(s, "[+x-]") did not work. Any ideas?
th> str = '5+3'
th> string.find(str, '[+-x]')
1 1
th> string.find(str, '[+x-]')
2 2
[+-x] is a pattern match for 1 character in the range between "+" and "x".
When you want to use dash as character and not as the meta character you should start or end the character group with it.
print("Type an arithmetic expression, such as 382 x 3 / 15")
expr = io.read()
i = -1
while i do
-- Find the next operator, starting from the position of the previous one.
-- The signals + and - are special characters,
-- so you have to use the % char to escape each one.
-- [The find function returns the indices of s where this occurrence starts and ends][1].
-- Here we are obtaining just the start index.
i = expr:find("[%+x%-/]", i+1)
if i then
print("Operator", expr:sub(i, i), "at position", i)
end
end

VBA parse string Place values to new columns

I have a sheets with the rows of data like.
NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=18.8200(18.4400,19.2100)
I would like to just grab the Values and place them in their own cells like
207.3980 207.3980 18.8200
206.1990 206.1990 18.4400
208.5970 208.5970 19.2100
I continue to recieve "ByRef Argument Mismatch" errors. I believe relating to how I am defining the reference cell.
Sub Parse_Replace()
Dim i As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Dim Col As Range
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
For i = rLastCell.Column To 1 Step -1
Col = ColLett(rLastCell.Column)
Columns(i).Cells(4) = SplitString(Col3, ",", 4)
Columns(i).Cells(5) = SplitString(Col3, ",", 5)
Columns(i).Cells(6) = SplitString(Col3, ",", 6)
Next i
End Sub
Function ColLett(Col As Integer) As String
If Col > 26 Then
ColLett = ColLett((Col - (Col Mod 26)) / 26) + Chr(Col Mod 26 + 64)
Else
ColLett = Chr(Col + 64)
End If
End Function
Function SplitString(pValue As String, pChar As String, pIndex As Integer) As Variant
Dim YString As Variant
YString = Replace(Replace(Replace(Replace(pValue, " ", ""), "=", ""), "(", ","), ")", ",")
SplitString = Split(YString, pChar)(pIndex - 1)
End Function
Process
Establish number of Columns with Data
Loop through each column
Convert column index to Column with ColLett
Set cell value with SplitString
Loop
Thank You
EDIT : replaced SplitString value with inteded.
You declare Col to be a range here:
Dim Col As Range
You then try to set Col to a string here:
Col = ColLett(rLastCell.Column)
When you set a range you have to set it to a range. Furthermore, you have to use the SET keyword to do so:
Set Col = <a range>
When you set that Col you set it only to the rLastCell.Column repeatedly in each loop of your For. If you just need that column letter for the last column, then do it before entering your for loop.
All of that is pointless anyway. At no point do you use the Column letter that you went through the trouble retrieving in your function. And really, for what you are doing you don't need the Column Letter. Column Letters are for humans; the column number is what is important in VBA any how.

How to use a loop through multiple similar textboxes?

So I know this is pretty sloppy, but I have a lot going on for a simple triangulation program (2d) and I'm very new.
The user inputs value in textboxes- "aft1.text,ain1.text,aft2.text,ain2.text,bft1.text,bin1.text,bft2.text,bin1.text." This goes on for as many points that need triangulated. I want to be able to run a loop through these similar textboxes, and run a function on them.
So for each textbox that starts with abft + i and ain + i run the inches function to create a1.
Do the same to create b1.
Then another loop to plot a + i and b + i on the chart per the xcoord/ycoord functions created.
Dim a1 As Double = inches(aft1.Text, ain1.Text)
Dim b1 As Double = inches(bft1.Text, bin1.Text)
If a1 <> 0 And b1 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a1, b1)), ((ycoord(a1, b1))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "1"
End If
Dim a2 As Double = inches(aft2.Text, ain2.Text)
Dim b2 As Double = inches(bft2.Text, bin2.Text)
If a2 <> 0 And b2 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a2, b2)), ((ycoord(a2, b2))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "2"
End If
Dim a3 As Double = inches(aft3.Text, ain3.Text)
Dim b3 As Double = inches(bft3.Text, bin3.Text)
If a3 <> 0 And b3 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a3, b3)), ((ycoord(a3, b3))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "3"
End If
If I understand you correctly, this code can be easily generalized by creating a Sub and calling it with your inches calculation and Label text,:oord are reachable (in scope) Functions:
Triangulate(inches(aft1.Text, ain1.Text), inches(bft1.Text, bin1.Text), 1)
Triangulate(inches(aft2.Text, ain2.Text), inches(bft2.Text, bin2.Text), 2)
Triangulate(inches(aft3.Text, ain3.Text), inches(bft3.Text, bin3.Text), 3)
EDIT-2
Although I have tested the following up to a point, since I do not have your chart properties nor the inches, xcoord and ycoord functions I could not test it completely, so try it out and let me know how it goes.
Sub TriangulateAll()
Try
Dim aft As New SortedList(Of String, TextBox)
Dim ain As New SortedList(Of String, TextBox)
Dim bft As New SortedList(Of String, TextBox)
Dim bin As New SortedList(Of String, TextBox)
For Each ctl As Control In Controls
If TypeOf (ctl) Is TextBox Then
Select Case ctl.Name.Substring(0, 3)
Case "aft"
aft.Add(ctl.Name, ctl)
Case "ain"
ain.Add(ctl.Name, ctl)
Case "bft"
bft.Add(ctl.Name, ctl)
Case "bin"
bin.Add(ctl.Name, ctl)
End Select
End If
Next
Dim a As New List(Of Double)
Dim b As New List(Of Double)
For Each kvp_aft As KeyValuePair(Of String, TextBox) In aft
For Each kvp_ain As KeyValuePair(Of String, TextBox) In ain
a.Add(inches(kvp_aft.Value.Text, kvp_ain.Value.Text))
Next
Next
For Each kvp_bft As KeyValuePair(Of String, TextBox) In bft
For Each kvp_bin As KeyValuePair(Of String, TextBox) In bin
b.Add(inches(kvp_bft.Value.Text, kvp_bin.Value.Text))
Next
Next
For i As Int16 = 0 To aft.Count - 1
Triangulate(a(i), b(i), i.ToString())
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
EDIT
Replace the 3 calls to Triangulate with the following code to completely generalize the Triangulation routine. Note that this routine expects there to be an equal number of each of the TextBox controls: aft, ain, bft, bin. I haven't tested this but it should work.
Dim aft As List(Of TextBox) = Nothing
Dim ain As List(Of TextBox) = Nothing
Dim bft As List(Of TextBox) = Nothing
Dim bin As List(Of TextBox) = Nothing
For Each ctl As Control In Controls
If TypeOf (ctl) Is TextBox Then
Select Case ctl.Name.Substring(0, 2)
Case "aft"
aft.Add(ctl)
Case "ain"
ain.Add(ctl)
Case "bft"
bft.Add(ctl)
Case "bin"
bin.Add(ctl)
End Select
End If
Next
aft.Sort()
ain.Sort()
bft.Sort()
bin.Sort()
For i As Int16 = 0 To aft.Count - 1
Dim a As Double = inches(aft.Item(i).Text, ain.Item(i).Text)
Dim b As Double = inches(bft.Item(i).Text, bin.Item(i).Text)
Triangulate(a, b, i.ToString())
Next
Sub Triangulate(a As Double, b As Double, LabelValue As String)
If a <> 0 And b <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a, b)), ((ycoord(a, b))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = LabelValue
End If
End Sub

Resources