[RESOLVED] Changing font size on one control changes all other controls fonts
Hello.
I'm developing a VB6 Pro, SP6 program on a XP Pro, SP3 computer.
I'm having a problem changing the font of a text box control on the form. When i change it, either inside the program or on the properties page and execute the program, all of the other controls on the form somehow get the same font change.
Does anybody know how to keep this from happening? I thought each control on a form had it's own properties.
Re: Changing font size on one control changes all other controls fonts
Hello dilettante.
Thanks for your help.
Here is my code. You can see at the end i'm changing the font for the text box to 24. When i run this everything on the page uses a font size of 24 including the flexgrid.
If i move the text box font changes up before the flex grid coding, then it displays in the smaller size font.
I tested this with labels and other controls and it does it for all controls on the form.
For Each previousOrdersFileName In previousOrdersFiles
If LCase(Mid(previousOrdersFileName.Name, 1, 6)) = "orders" Then
If (Mid(previousOrdersFileName.Name, 8, 3) >= "001" And Mid(previousOrdersFileName.Name, 8, 3) <= "030") Or LCase(Mid(previousOrdersFileName.Name, 8, 3)) = "bol" Then
previousOrdersRecordSet.AddNew
Set previousOrdersFile = previousOrdersFileSystemObject.GetFile(previousOrdersFileName)
Re: Changing font size on one control changes all other controls fonts
I did not test many combinations. I just tested adding an additional label and text box and of course with the flex grid there also. It changes any text box font size and label font size as well as the flex grid font size.
Re: Changing font size on one control changes all other controls fonts
Do you know how to use Debug? If so you might be able to find out where it is happening by adding a Watch for Me.Text1.FontSize or whatever control you want to follow and select 'Break When Value Changes'. If you need help then take a look at my VB6 Debug Tutorial.
Re: Changing font size on one control changes all other controls fonts
OK. I moved the font size changes to before the flex grid changes and made the textbox font size = 24.
I then set a watch for the text box and stepped through all of the statements. When Me.Text1.FontSize = 24 occurred, i could see it in the watch window. When it got to this MSHFlexGrid1.FontSize = 10, it also changed the text box size in the watch window.
So, it's changing it after that statement occurs even though that statement is for the flex grid.
I see what is happening, but i still don't know how to fix it.
Re: Changing font size on one control changes all other controls fonts
I opened the zip file and started the project. What do I do about the c:\dataflow\init\master.ini file it shows me in the Please Enter Parameter File InputBox? Also a note of caution for you. Your convertEDMSControlForm is not stored in your project folder but rather in the VBStudio folder.
Re: Changing font size on one control changes all other controls fonts
I should have said that the convertEDMSControlForm form that your VBP project file is referring to is in the Program Files\Microsoft Visual Studio\VB98 folder and so you may have two of them. One in your project folder and one there.
Re: Changing font size on one control changes all other controls fonts
Well it would in that when you made changes to that form you were changing the one in the Program Files\Microsoft Visual Studio\VB98 folder and the one you sent me was probably from your project folder.
I also sorry to say that your project environment is complex and I don't have the time to set it up, so while I may still answer questions I won't be running the project.
Re: Changing font size on one control changes all other controls fonts
I'm not sure i understand. That form has nothing to do with the main form which is where the problem is. The missing form only gets called from one of the menu options.
The problem is in the display of the main form which gets called form Sub Main in module1.
I don't know what to do about this, except start the project all over.
Re: Changing font size on one control changes all other controls fonts
Just poking my nose in.........
I have a theory, not too sure but it sounds logical. Others with more knowledge may have a better explanation
Code:
Set fnt = frm.Font
fnt.Name = LoadResString(20)
fnt.Size = CInt(LoadResString(21))
'set the controls' captions using the caption
'property for menu items and the Tag property
'for all other controls
For Each ctl In frm.Controls
Set ctl.Font = fnt
Sets (ie 'Points') the font object of each control to the same object (frm.Font). Thus changes to a Font property on any control will affect all controls. If I comment out the 'Set ctl.Font' statement the change to Text1.Font does not propagate to other controls.
Also, the 'On Error Resume Next' statement (which I'd remove) in LoadResStrings is 'hiding' at least 3 errors:
1. frm.Caption = LoadResString(CInt(frm.Tag)) fails Type Mismatch because frm.Tag is Null
2. fnt.Name = LoadResString(20) fails as Resource with identifier '20' is not found
3. fnt.Size = CInt(LoadResString(21)) fails as Resource with identifier '21' is not found
I'd get those sorted out first and remove the 'Set fnt =' statement. Then you'll probably have to set each Font property in each control to the default value you want rather than 'clone' the complete object.
eg
Code:
fntName = LoadResString(20)
fntSize = CInt(LoadResString(21))
.
.
.
For Each ctl In frm.Controls
ctl.Font.Name = fntName
ctl.Font.Size = fntSize
.
etc
Alternatively, you could just exclude Text1 from the setting of the Font by checking for ctl.Name = "Text1"
However, you're going to run into trouble where the Control does not have a Font property, eg
mnuFileNew
mnuFile
Image1
dlgCommonDialog
(The same applies to those Controls not having a ToolTipText property.)
so you may need to introduce error handling just within the For / Next loop to ignore the specific error. eg
Code:
On Error GoTo errh
For Each ctl In frm.Controls
.
. etc
.
Next
On Error GoTo 0
Exit Sub
errh:
'
' Ignore 'Object doesn't support this property or method' Error
'
If Err.Number = 438 Then
Resume Next
Else
'
' Report other errors
'
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Or use an 'If TypeOf ctl Is ... Then' construct to avoid the exceptions.
Last edited by Doogle; Mar 11th, 2011 at 04:48 AM.
Re: Changing font size on one control changes all other controls fonts
Hello Doogle.
Thanks for your in depth analysis and information.
It sounds like you got this to happen using my program. Is that right?
Your insight caused me to start debugging through the LoadResStrings subroutine. I noticed that the text box control falls into the last else, but that else did not really do anything.
Also, all of the control settings are being done after the LoadResStrings subroutine. So, i'm not sure of how that LoadResStrings subroutine could have anything to do with it.
I'm also not sure of how displaying errors in the LoadResStrings subroutine helps any.
Finally, that LoadResStrings subroutine was put there by the Wizard and i'm not sure i want to start changing something that the wizard put there.
Re: Changing font size on one control changes all other controls fonts
Please don't take this as a criticism, but one thing wrong with using code generators is that you can get code that you don't understand but still need to maintain. IMO it's much better to write the code yourself from scratch, even if you have to struggle to do so, because in the end you will have a much better idea of what's going on, and maintenance/modification will be much easier.
Re: Changing font size on one control changes all other controls fonts
Originally Posted by Tgirgenti
It sounds like you got this to happen using my program. Is that right?
Yes, that's right. I stepped through the code using Debug.
As Martin says, code wizards are not always 'perfect' and, IMHO, should be used more as a guide, or perhaps, 'framework' which you can adjust to fit your needs. As I think I have ponted out, the code has some shortcomings which are causing you problems.
Having 'played' with your code, I am convinced that my suggestions will resolve the current problem(s)
Re: Changing font size on one control changes all other controls fonts
It's a thing of beauty Doogle.
I'm sorry for even questioning your suggestion. That absolutely fixed the problem. I had to do a little debugging to figure out why the errors were occurring, especially with the menu control type, but eventually got it to work withour errors.
I can't believe that Microsoft creates a piece of code like that from a wizard that is supposed to be helping you create a workable template.
Is there some way to mark this post as resolved?
Thanks for all of your diligent work in debugging this.
Re: Changing font size on one control changes all other controls fonts
Now that we've helped you, you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that by selecting the Mark Thread Resolved item from the Thread Tools menu. Otherwise please insert "[Resolved]" at the start of the Subject and select the green checkmark from the Post Icons. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help. (You could wait until you have 20 posts)
Re: [RESOLVED] Changing font size on one control changes all other controls fonts
I have a situation that is similar to Tgirgenti's issue, but no resource strings being used in this scenario.
I have two labels that are on the form hidden and display after the user hits an update button... basically an "Update Successful" label.
I set all the fonts on my form to what I want, but as soon as I change the label for the "Update Successful" control, all of the controls on the form change!
This label control is already defined from the Design screen as a bold font.
I can step through my code and all is well until I hit this!
Here is the routine I use to set the control fonts; It can iterate through the controls on the form and set the font properties. I get the current state of the font before I apply the oFont object to the control because the oFont objects FontBold=false.
Code:
Public Sub SetFormFont(frm As Form, Optional ForceRefresh As Boolean = False, Optional ForeColor As Long = 0)
Dim ctl As Control
Dim fntSize As Integer
Dim fntName As String
Dim fntCharset As Integer
Dim fntBold As Boolean
Dim fntItalic As Boolean
Dim oFont As StdFont
Dim bIsBold As Boolean
Dim bIsUnderlined As Boolean
GetFontInfo fntName, fntSize, fntCharset, fntBold, fntItalic, ForceRefresh
Set oFont = New StdFont
With oFont
.name = fntName
.Size = IIf(fntSize = 0, 8, fntSize)
.Charset = fntCharset
.bold = fntBold
.italic = fntItalic
End With
Set frm.Font = oFont
On Error Resume Next
For Each ctl In frm.Controls
bIsBold = ctl.Font.bold ' see if the controls font is already set to BOLD
' Some controls cannot take font settings so we skip those and continue on...
' I hate using the GOTO statement but it works here the way a break statement would.
If err = 438 Then
GoTo Break
ElseIf err > 0 Then
MsgBox err.Description
End If
bIsUnderlined = ctl.Font.Underline
If ctl.Font.Underline = False Then
Set ctl.Font = oFont
If ForeColor > 0 Then ctl.ForeColor = ForeColor
If bIsBold Then
ctl.Font.bold = True
Else
ctl.Font.bold = False
End If
End If
Break:
err.Clear
Next
End Sub
No error messages are displayed.
Any one have any ideas?