Current high school student learning vb6 and having this major problem when trying to set the left and top values of these image boxes. I keep getting the error:
Object Variable or With Block Variable Not Set
and debug seems to lead it to the left and top value variables.
Option Explicit
Dim GapY As Integer
Dim GapX As Integer
Dim x As Integer
Dim y As Integer
Dim Tile() As Image
Dim NumOfTiles
Dim h, i As Integer 'Counter
Private Sub cmdRender_Click()
x = 480
y = 480
GapX = Val(InputBox("Enter How Many tile you want horizontally:"))
GapY = Val(InputBox("Enter How Many tile you want vertically"))
NumOfTiles = (GapY * GapX)
ReDim Tile(NumOfTiles)
For i = 1 To GapY
For h = 1 To GapX
Tile(h).Height = 615
Tile(h).Width = 615
Tile(h).Left = x
Tile(h).Top = y
'Tile(h).Stretch = True
x = x + 600
Next
y = y + 600
x = 480
Next
End Sub
You have not assigned any image to your tile array, i.e. you have an array with NumOfTiles empty positions.
I don't know where you are getting your images from. You might create images with new Image or read images from files or take them from image boxes on a form etc.
Probably you want to work with PictureBox controls. if you have such controls on a form, you would have to type the array as such
Dim Tile() As PictureBox
ReDim Tile(NumOfTiles) As PictureBox
And then assign them from your form (assuming that you have placed some on a form):
Set Tile(1) = pictureBox1
Set Tile(2) = pictureBox2
...
or in a loop
For i = 1 To NumOfTiles
Set Tile(i) = Me("pictureBox" & i)
Tile(i).Left = ...
Next
or create them on the form with something like this (I did not try it out)
Set Tile(i) = Me.Controls.Add("VB.PictureBox", "pictureBox" & i)
Related
I have rectangle that's add another rectangle depend on how many stamps/receipt to be print.
I add another rectangle using for loop but if I manage to add more than 3 rectangles the other rectangles will not diplay because it's keeping align straight or if I use if else if e.pagebounds.widht < (the last width of my rectangle) it goes down and it's fine but how about the next... next ... next for the row 3 and up how can I get that this is my code so far
Dim rec As Rectangle
Dim x1, y1 As Integer
Dim nextline As Integer
x1 = 40
y1 = 40
Dim b As Integer = 0
Dim containerrectangle As Rectangle
containerrectangle = New Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, e.PageBounds.Height)
For i = 0 To 6
If e.PageBounds.Width - 100 < x1 + b Then
nextline = 40
'reset the x-axis of the rectangle
y1 = 250
b = 40
rec = New Rectangle(b, y1 + 50, 250, 250)
e.Graphics.DrawRectangle(Pens.Sienna, rec)
y1 += 250
b += 10
Else
rec = New Rectangle(x1 + b, 40, 250, 250)
e.Graphics.DrawRectangle(Pens.Sienna, rec)
x1 += 250
b += 10
End If
Next
please see image https://imgur.com/a/ssv6YzX
Have a look into this code - I'd say it's more straight forward:
Dim RectTemplate As New Rectangle(0, 0, 250, 250)
Dim GapPx As Int32 = 12 ' set gap between rectangles
Dim StaPt As New Point(GapPx, GapPx * 2) ' set starting point
Dim N As Int32 = 15 ' set (or get) the totalnumber of rectangles
Dim nL As Int32 = Math.Floor((e.PageBounds.Width - GapPx) / (RectTemplate.Width + GapPx)) ' number of rectangles per row
Dim nR As Int32 = Math.Ceiling(N / nL) ' number of rows of rectangles
For ir = 0 To nR - 1 ' process all rows
For il = 0 To nL - 1 ' process all rectangles in a row
Dim rect As New Rectangle(StaPt.X + il * (RectTemplate.Width + GapPx), StaPt.Y + ir * (RectTemplate.Height + GapPx), RectTemplate.Width, RectTemplate.Height)
e.graphics.DrawRectangle(Pens.Sienna, rect)
Next
Next
Using SNMP version 3, I am creating a user.
Right now, I have it set up where I clone a user and that works just fine. However, I need to change the new user's authKey. How can I do this? I know the oid for authKeyChange, however, I don't know how to generate the new key. How do I generate that key? Can it be done using SNMPSharpNet?
If there is an easier way to do this while I'm creating the user, I can do that as well. ANY way to change the authKey (and privKey, but one step at a time) is much appreciated. I'm using VB.net if it means anything.
So I've figured out how to do this. It's a bit of a complex process. I followed this document, which is rfc2574. Do a ctrl+F for "keyChange ::=" and you'll find the paragraph walking you through the algorithm to generate the keyChange value. The following code has worked reliably to generate the keyChange value. All you have to do from this point is push the keyChange value to the usmAuthKeyChange OID. If you are changing the privacy password, you push the keyChange value to the usmPrivKeyChange OID. I'm ashamed to say that due to the time crunch, I did not have time to make this work completely, so when using SHA, I had to code an entirely new method that did almost the exact same thing. Again, I'm ashamed to post it, but I know how much I was banging my head against a wall, and if someone comes here later and sees this, I would like them to know what to do without going through the struggle.
Here is all of the code you need using VB.Net and the SNMPSharpNet library:
Private Function GenerateKeyChange(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte()
Dim authProto As AuthenticationDigests = param.Authentication
Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto)
Dim L As Integer = hash.DigestLength
Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId)
Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId)
Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L))
Dim temp() As Byte = oldKey
Dim delta(L - 1) As Byte
Dim iterations As Integer = ((newKey.Length - 1) / L) - 1
Dim k As Integer = 0
If newKey.Length > L Then
For k = 0 To iterations
'Append random to temp
Dim merged1(temp.Length + random.Length - 1) As Byte
temp.CopyTo(merged1, 0)
random.CopyTo(merged1, random.Length)
'Store hash of temp in itself
temp = hash.ComputeHash(merged1, 0, merged1.Length)
'Generate the first 16 values of delta
For i = 0 To L - 1
delta(k * L + i) = temp(i) Xor newKey(k * L + i)
Next
Next
End If
'Append random to temp
Dim merged(temp.Length + random.Length - 1) As Byte
temp.CopyTo(merged, 0)
random.CopyTo(merged, temp.Length)
'Store hash of temp in itself
temp = hash.ComputeHash(merged, 0, merged.Length)
'Generate the first 16 values of delta
For i = 0 To (newKey.Length - iterations * L) - 1
delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i)
Next
Dim keyChange(delta.Length + random.Length - 1) As Byte
random.CopyTo(keyChange, 0)
delta.CopyTo(keyChange, random.Length)
Return keyChange
End Function
Private Function GenerateKeyChangeShaSpecial(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte()
Dim authProto As AuthenticationDigests = param.Authentication
Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto)
Dim L As Integer = 16
Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId)
Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId)
Array.Resize(oldKey, L)
Array.Resize(newKey, L)
Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L))
Dim temp() As Byte = oldKey
Dim delta(L - 1) As Byte
Dim iterations As Integer = ((newKey.Length - 1) / L) - 1
Dim k As Integer = 0
If newKey.Length > L Then
For k = 0 To iterations
'Append random to temp
Dim merged1(temp.Length + random.Length - 1) As Byte
temp.CopyTo(merged1, 0)
random.CopyTo(merged1, random.Length)
'Store hash of temp in itself
temp = hash.ComputeHash(merged1, 0, merged1.Length)
Array.Resize(temp, L)
'Generate the first 16 values of delta
For i = 0 To L - 1
delta(k * L + i) = temp(i) Xor newKey(k * L + i)
Next
Next
End If
'Append random to temp
Dim merged(temp.Length + random.Length - 1) As Byte
temp.CopyTo(merged, 0)
random.CopyTo(merged, temp.Length)
'Store hash of temp in itself
temp = hash.ComputeHash(merged, 0, merged.Length)
Array.Resize(temp, L)
'Generate the first 16 values of delta
For i = 0 To (newKey.Length - iterations * L) - 1
delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i)
Next
Dim keyChange(delta.Length + random.Length - 1) As Byte
random.CopyTo(keyChange, 0)
delta.CopyTo(keyChange, random.Length)
Return keyChange
End Function
Private Function GenerateRandomString(ByVal length As Integer) As String
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To length
Dim idx As Integer = r.Next(0, 51)
sb.Append(s.Substring(idx, 1))
Next
Return sb.ToString()
End Function
Again, I am oh so well aware this code is hideous, but it works, and that is all I needed in the meantime. I understand this is technical debt and not the way I should code, but it's here and I hope you can get some use out of it.
If this doesn't work, don't forget to go to frc2574 and look at the algorithm.
I have this code where I want to look up some cells in another sheet, and if they match the criteria then add a neighboring cell to the total which is returned to the cell function is called from.
Function collectUtfall(A1 As String, Ax As String)
Dim rng As Range
Dim total As Long: total = 0
Set rng = Sheets("Utfall").Range("M2:O272")
Dim cell As Range
For Each cell In rng
If StrComp(cell.Offset(0, 1).Text, Ax, vbTextCompare) = 0 Then
total = total + ActiveCell.Value
Else
End If
Next
collectUtfall = total
End Function
Problem is that I get a "Circle reference" error when executing. Is there a problem in using ActiveCell.Value in that way?
If I just try with a value, say 10, it works just fine:
total = total + 10
So the problem has to be with the ActiveCell.Value?
Function collectUtfall(A1 As String, Ax As String)
Dim rng As Range
Dim total As Long
set total = 0
Set rng = Sheets("Utfall").Range("M2:O272")
Dim cell As Range
For Each cell In rng
If StrComp(cell.Offset(0, 1).Text, Ax, vbTextCompare) = 0 Then
total = total + Cell.Value
Else
End If
Next Cell
collectUtfall = total
End Function
I have an application in which i'm drawing a line/square on a picturebox. I also need the user to click on a particular point on the picturebox(after drawing the square/line) so as to get the location of the second point. But the mouse down event does not work for the second click. My code is as shown:
Dim m_Drawing As Boolean
'm_Drawing = False
Dim m_Startx As Single
Dim m_Starty As Single
Dim m_endx As Single
Dim m_endy As Single
Dim square_click As Boolean
'square_click = False
Dim line_click As Boolean
'line_click = False
Dim bclick As Boolean
'blick = True
Dim startx As Single
Dim starty As Single
Dim endx As Single
Dim endy As Single
Dim laserx_mm As Single
Dim lasery_mm As Single
Dim rectx_mm As Single
Dim recty_mm As Single
Dim xpos As Single
Dim ypos As Single
Dim uxpos As Single
Dim uypos As Single
Dim dist As Single
Dim dist1 As Single
Private Sub Command1_Click()
square_click = True
End Sub
Private Sub Command2_Click()
line_click = True
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim diffx As Single
Dim diffy As Single
Picture1.Cls
If m_Startx = 0 And m_Starty = 0 Then
m_Startx = X
m_Starty = Y
'End If
startx = X
starty = Y
rectx_mm = X
recty_mm = Y
'move to start position
ElseIf m_Startx <> 0 And m_Starty <> 0 Then
laserx_mm = X
lasery_mm = Y
diffx = rectx_mm - laserx_mm
diffy = recty_mm - lasery_mm
dist = xpos + (diffx / 4.74 / 1000)
dist1 = ypos - (diffy / 4.68 / 1000)
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
endx = X
endy = Y
m_endx = X
m_endy = Y
If square_click = True Then
Picture1.Line (m_Startx, m_Starty)-(endx, endy), vbWhite, B
ElseIf line_click = True Then
Picture1.Line (m_Startx, m_Starty)-(endx, endy), vbWhite
End If
End Sub
The Code: ElseIf m_Startx <> 0 And m_Starty <> 0
does not get executed unless and until i put a breakpoint there. I'm not sure why this is happening. Please help me out! Hope i was clear enough! Thanks.
I threw a Debug.Print "Here I am" call inside your ElseIf m_Startx <> 0 And m_Starty <> 0...Works like a charm on the 2nd click. Perhaps you may want to go with a darker color or a thicker line? The white line is fairly hard to see.
What's the best way to test two Singles for equality in VB6?
I want to test two Single values for equality to 7 significant figures.
This MSDN article recommends using something like
If Abs(a - b) <= Abs(a / 10 ^ 7) Then
valuesEqual = True
End If
However, that can fail for certain values, e.g.
Public Sub Main()
Dim a As Single
Dim b As Single
a = 0.50000005
b = 0.50000014
Debug.Print "a = " & a
Debug.Print "b = " & b
Debug.Print "a = b: " & (a = b)
Debug.Print "SinglesAreEqual(a, b): " & SinglesAreEqual(a, b)
// Output:
// a = 0.5000001
// b = 0.5000001
// b = b: False
// SinglesAreEqual(a, b): False
End Sub
Private Function SinglesAreEqual(a As Single, b As Single) As Boolean
If Abs(a - b) <= Abs(a / 10 ^ 7) Then
SinglesAreEqual = True
Else
SinglesAreEqual = False
End If
End Function
The simplest way I've found of getting the result I need is to convert the values to strings, but seems horribly ugly:
Private Function SinglesAreEqual(a As Single, b As Single) As Boolean
SinglesAreEqual = (Str$(a) = Str$(b))
End Function
Are there any better ways?
I maintain a CAD/CAM application and I have to deal with floating point numbers all the time. I have a function that I call fComp that I pass a floating point value when I need to test for equality. fComp call a rounding function set to a certain level of precision. For our system I round to 6 decimal places. Yours may need higher or get away with lower it depends on the application.
The fComp Function exists so I have one spot to change the rounding factor used in these calculations. This proved handy a couple of years back when we started manufacturing higher precision machines.
Public Function pRound(ByVal Value As Double, ByVal Power As Double) As Double
Dim TempValue As Double
Dim tSign As Double
TempValue = Value
tSign = TempValue
TempValue = Abs(TempValue)
TempValue = TempValue * 10 ^ (Power * -1)
TempValue = Fix(TempValue + 0.5)
TempValue = TempValue / 10 ^ (Power * -1)
pRound = TempValue * Sign(tSign)
End Function
To round to the 6th decimal place you go
RoundedNumber = pRound(MyValue, -6)
Negative is to the right of the decimal place positive to the left.
Instead if rounding and testing for equality, you can take the difference of two numbers and compare that with a factor
If Abs(a - b) < 0.000001 Then
You can adjust the 0.000001 to whatever resolution you need
I don't believe you can use the single data type to that many significant figures. You would need to use double instead:
Dim a As Single
Dim s As String
s = "0.50000005"
a = 0.50000005
Debug.Print s & " " & a
The above outputs:
0.50000005
0.5000001