[RESOLVED] UserControl with MSFlexGrid needs Font change
I'm busy (trying!) to create a UserControl that uses a MSFlexGrid with a button or three for adding rows/cols and removing rows/cols at runtime. I've got most of the events and properties figured out (i hope!)
My problem is I can't get the font figured out. How do I set the Font property of the MSFlexgrid in my UserControl? I don't want to change the Font of the buttons in the UserControl only the MSFlexGrid.
I've got the Font property into the UserControl and can choose the Font for the UserControl at design time but can't get the MSFlexGrid to show the chosen Font. It reverts back to the font I used for the MSFlexGrid while designing the UserControl.
Has done the following (the MSFlexGrid in the UserControl are named MSFG):
Set Font = PropBag.ReadProperty("Font", Ambient.Font)
And the Property Get and Set functions
Code:
Public Property Get Font() As Font
Set Font = UserControl.Font
End Property
Public Property Set Font(ByVal fntNewFont As Font)
Set UserControl.Font = fntNewFont
Set MSFG.Font = UserControl.Font
UserControl.PropertyChanged "Font"
End Property
Any and all help appreciated. This is driving me nuts!
When you use WriteProperty and supply the optional default value, nothing is written if the value to be stored is same as the default property. Your .WriteProperty statement has no effect because you are writing Font which is your public Font property which is also the same as your default property of UserControl.Font. Simply remove that optional parameter.
Last edited by LaVolpe; Mar 8th, 2011 at 05:27 PM.
Insomnia is just a byproduct of, "It can't be done"
Just one minor problem. The MSFlexGrid in the UserControl defaults to a grid of 3x3. Attached is a screenshot of the grid with some data in it. Notice that the captions of columns A and B and rows 1 and 2 is not in the font as set for the FlexGrid/UserControl (in this case Tahoma Bold-Italic). The rest of the grid is in the correct font
It has to do with the default grid size. If I change the default to 2x2 grid column A and row 1 is not set but the rest are.
..It has to do with the default grid size. If I change the default to 2x2 grid column A and row 1 is not set but the rest are.
Any ideas?
Interesting. Have you tried to default it to 1x1 and then add them via code instead? In the properties window for that control, set the Fixed Rows/Cols values each to 0 and the Rows/Cols value each to 1. Then during your control's Initialize event, add this. I don't know if it will resolve the problem or not.
Tried it but doesn't help. The number of cols and rows set in the Initialize event still does not show the correct font. Looks like the font gets set after the initialize event having the same effect as if it was set in the properties before.
Would probably have to change the font cell by cell. In which event would one have to try that. It would have to be the first event that runs after the ReadProperties event.
Is there a way to find out in which order the events are fired other than putting a Debug.Print statement in each event?
Here's a pdf document with just about everything you wanted to know about ActiveX controls/dlls. Chapter 17 is primarily ocx, but scan the table of contents for other chapters of interest.
Insomnia is just a byproduct of, "It can't be done"
... It would have to be the first event that runs after the ReadProperties event.
Keep this in mind and reading the ocx chapters in that link should help too.
ReadProperites is NEVER called the very first time a usercontrol is placed on a form, InitProperites is called then. After that first time, InitProperties is never called again and ReadProperties is called thereafter. Initialize event is always called. I would expect when Initialize is called that the FlexGrid is already created.
By the way, I could not replicate your problem by using the flex grid in a form, however, I may have time to give it a quick test tonight using code similar to yours to see if I can replicate the problem.
Insomnia is just a byproduct of, "It can't be done"
Here's what I've done. It works perfectly and doesn't exhibit the behavior you are describing.
Code:
Public Property Set Font(newFont As StdFont)
Set MSFlexGrid1.Font = newFont
PropertyChanged "Font"
End Property
Public Property Let Font(newFont As stdFont)
Set Me.Font = newFont
End Property
Public Property Get Font() As StdFont
Set Font = MSFlexGrid1.Font
End Property
Private Sub UserControl_Initialize()
End Sub
Private Sub UserControl_InitProperties()
Set MSFlexGrid1.Font = Ambient.Font
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set MSFlexGrid1.Font = PropBag.ReadProperty("Font", Ambient.Font)
End Sub
Private Sub UserControl_Show()
' should use a flag and set it after this is called
' if this gets called again and flag is set, then don't re-do the headers
MSFlexGrid1.TextMatrix(0, 1) = "A"
MSFlexGrid1.TextMatrix(0, 2) = "B"
MSFlexGrid1.TextMatrix(0, 3) = "C"
MSFlexGrid1.TextMatrix(1, 0) = "1"
MSFlexGrid1.TextMatrix(2, 0) = "2"
MSFlexGrid1.TextMatrix(3, 0) = "3"
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Font", MSFlexGrid1.Font, Ambient.Font
End Sub
Note that I don't mess with the UserControl.Font above. Simply because IMO not needed. The usercontrol.font probably should be a separate property if it is used differently than the flexgrid font. You can set the UC font if desired, regardless.
Insomnia is just a byproduct of, "It can't be done"
I must be missing something somewhere. Mine still doesn't work as it should. I've attached the project. Not commented well and lots of rubbish still in there especially Debug.Print statements.
I'll leave it to you to experiment and see if you can find the reason, but moving the following line from the bottom of ReadProperties to the top seems to solve the problem
Code:
Set MSFG.Font = PropBag.ReadProperty("Font", Ambient.Font)
Insomnia is just a byproduct of, "It can't be done"
Narrowed it down to the ClipAll function. When it's after the ClipAll ReadProperty it doesn't work but before the ClipAll ReadProperty it works but I can't see/think why?
If you rem out your call to FixCaptions in your ClipAll function, problem does not appear
Now, un-reming that and going into the FixCaptions function, rem-ing out setting font to Bold, then problem does not occur. Like I've said before, I'm no longer familiar with the flex grid and don't know what the workaround would be or why that call is affecting the display, but it appears to be. Enjoy
Insomnia is just a byproduct of, "It can't be done"