Hello VbForums community
I'm saving my textboxes left , top, width and height properties in an ini file.
From time to time everything gets collapsed.
The textboxes are shrinked and become 0 Left.
My textboxes are inside a picturebox.
My original textboxes:
Public Function WriteToAppINI(strSection As String, strKeyName As String, strValue As String, strFile As String) As Long
Dim intStatus As Long
On Error GoTo PROC_ERR
intStatus = WritePrivateProfileString(strSection, strKeyName, strValue, strFile)
WriteToAppINI = (intStatus <> 0)
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & " " & Err.Description, , "WriteToAppINI"
Resume PROC_EXIT
End Function
Code:
Public Function ReadFromAppINI(SectionHeader As String, VarName As String, ByVal Default As String, strFile As String) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
ReadFromAppINI = Left(RetStr, GetPrivateProfileString(SectionHeader, ByVal VarName$, Default, RetStr, Len(RetStr), strFile))
End Function
Code:
For Each ctl In Valdation_frm.Controls
If TypeOf ctl Is TextBox Then
Dim RetVal As Long
RetVal = WriteToAppINI(Me.Name & "Positions", ctl.Name & " width", CStr(ctl.Width), DataFolder & "\AppSetting.ini")
RetVal = WriteToAppINI(Me.Name & "Positions", ctl.Name & " height", CStr(ctl.Height), DataFolder & "\AppSetting.ini")
RetVal = WriteToAppINI(Me.Name & "Positions", ctl.Name & " left", CStr(ctl.Left), DataFolder & "\AppSetting.ini")
RetVal = WriteToAppINI(Me.Name & "Positions", ctl.Name & " top", CStr(ctl.Top), DataFolder & "\AppSetting.ini")
End if
Next
Code:
For Each ctl In Valdation_frm.Controls
If TypeOf ctl Is TextBox Then
Dim RetVal As Long
ctl.Width = ReadFromAppINI(Me.Name & "Positions", ctl.Name & " Width", ctl.Width, DataFolder & "\AppSetting.ini")
ctl.Height = ReadFromAppINI(Me.Name & "Positions", ctl.Name & " height", ctl.Height, DataFolder & "\AppSetting.ini")
ctl.Left = ReadFromAppINI(Me.Name & "Positions", ctl.Name & " Left", ctl.Left, DataFolder & "\AppSetting.ini")
ctl.Top = ReadFromAppINI(Me.Name & "Positions", ctl.Name & " Top", ctl.Top, DataFolder & "\AppSetting.ini")
End if
Next
Are you able to make it happen or does it happen rarely and you cannot know when is it going to happen?
I would put some stop points in the code to start investigating what could be happening:
Code:
Public Function WriteToAppINI(strSection As String, strKeyName As String, strValue As String, strFile As String) As Long
Dim intStatus As Long
On Error GoTo PROC_ERR
If Val(strValue) = 0 Then Stop
intStatus = WritePrivateProfileString(strSection, strKeyName, strValue, strFile)
WriteToAppINI = (intStatus <> 0)
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & " " & Err.Description, , "WriteToAppINI"
Resume PROC_EXIT
End Function
Code:
Public Function ReadFromAppINI(SectionHeader As String, VarName As String, ByVal Default As String, strFile As String) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
ReadFromAppINI = Left(RetStr, GetPrivateProfileString(SectionHeader, ByVal VarName$, Default, RetStr, Len(RetStr), strFile))
If Val(ReadFromAppINI) = 0 Then Stop
End Function
Only the Left of the controls is set to 0, not the Top or other properties?
PGBSoft
thank you for the interest and thank you for the sample
I tested the sample and everything is perfect.
Is there any change in your codes?
thank you
Hello VbForums community
I'm saving my textboxes left , top, width and height properties in an ini file.
From time to time everything gets collapsed.
The textboxes are shrinked and become 0 Left.
My textboxes are inside a picturebox.
why do you save the Settings ?
you have a Form_Load and Resize
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
I would suggest that when you encounter this problem take a look at the ini file with notepad and see if you have a 0 written to the file. If you do then look at the point where you write to the ini file, if the value is no 0 in the ini file then check the part that reads and sets the positions.
What ever is going on it pretty much has to be either your read, your write or your position assignments.
If you are still having the problem, this is just an idea.
Can't you dynamically load the textboxes just like in the attached sample here?
Changes to consider in this sample from the previous sample in #5:
1. TextBoxes (array based) are dynamically loaded at runtime instead of design-time.
2. The index of textboxes are saved in the ini file instead of their Names.
3. TextBoxes are refreshed after setting their positions from the ini file.
4. TextBoxes can be positioned at runtime.
1. You can move the textboxes at runtime.
2. Close the form.
3. Reopen
Last edited by PGBSoft; Aug 27th, 2018 at 07:17 AM.
Thanks PGBSoft
I have been playing with your sample example for a hundred times wthout producing the error.
It is nearly thesame thing I'm doing in my project
CStr() is locale-aware so you should not use it with data that is persisted or exchanged with another computer. Never. Use the Invariant Locale formatting instead.
Converting back you are relying on compiler-inserted implicit coercion, which I believe is locale-blind and always uses the Invariant Locale.
Instead you can convert numeric values to String using the old Str$() function and convert back from String using the old Val() function. These are both locale-blind.