bazzer747 25 Posted March 5, 2021 Hi In a window I have a 'floating' panel (calledpanEdit)that follows the items in a dbgrid. On some occasions I don't want to see that panel. Elsewhere I have an edit field and on it's OnClick event I have: panEdit.Visible:= False; which works fine, making the panEdit panel invisible. However, that's not so good in design terms as I have to enter this edit field to make the panel invisible. So I have put another panel on the form, made it effectively invisible by not showing borders and colouring it the same as the background. On it's OnClick event I've put the same code to make panEdit invisible. But it doesn't! I follow the code in debug mode and when I click on this panel it goes to the OnClick event, runs the panEdit.Visible:= False code, but does nothing. Am I missing something here? There's an OnClick event for a panel and it runs the code but doesn't run the code, if you see what I mean. I've put a ShowMessage('Hello there') in the OnClick event and that shows, so it is running that code, but ignoring, the panEdit.Visible:= False; code. Share this post Link to post
bazzer747 25 Posted March 5, 2021 Darn! Found the problem. I have code in the dbgrids OnDrawColumnCell event which sets the panel visibility to True, and this is getting run after I click the other panel. Not sure why it should do this but I traced it in the OnDrawColumnCell event. 1 Share this post Link to post
Remy Lebeau 1436 Posted March 5, 2021 5 hours ago, bazzer747 said: I have code in the dbgrids OnDrawColumnCell event which sets the panel visibility to True Absolutely DO NOT EVER update UI state in a drawing event! 5 hours ago, bazzer747 said: Not sure why it should do this It shouldn't be doing that at all. Get rid of it. Share this post Link to post
bazzer747 25 Posted March 5, 2021 Please explain why you say that. I change colour and font in the drawing event, why not make a panel visible or invisible, if that is what is needed. Share this post Link to post
Remy Lebeau 1436 Posted March 5, 2021 1 hour ago, bazzer747 said: Please explain why you say that. Because drawing events are meant JUST for drawing, using the current UI state to decide what to draw. They are not meant for managing/updating the UI state. 1 hour ago, bazzer747 said: I change colour and font in the drawing event As long as you are changing the color/font of the Canvas that you are drawing onto, then that is perfectly fine. That is not UI state, that is just drawing state. On the other hand, changing the color/font of a UI control from inside a drawing event would be wrong, and can lead to endless repaint loops that eat up CPU cycles and slow down the UI message loop. 1 hour ago, bazzer747 said: why not make a panel visible or invisible, if that is what is needed. Because that is NOT needed OR appropriate during a drawing event. That is for UI logic to handle instead, for instance in reaction to some action. Changing the visibility of a UI control will trigger a UI repaint to draw the updated UI display with/without the control in it. The drawing should account for the control's current state, not update its state. Share this post Link to post
Guest Posted March 5, 2021 (edited) 2 hours ago, bazzer747 said: Please explain why you say that. I change colour and font in the drawing event, why not make a panel visible or invisible, if that is what is needed. hi @bazzer747 maybe, you can use a component not-visual for this task on DBGrid or similar... look, if you need change your Panel.VISIBILITY when your DataSource from your DBGrid change the State, for example, then, just add a new DataSource in your Form and do use of events this component. You see? it would be your "2º DataSource" for task like this. DataSource is a non-visual component, then, it not have a "repaint" calls, for example. By consequence, none UI events! hug Edited March 5, 2021 by Guest Share this post Link to post
bazzer747 25 Posted March 6, 2021 Hi Many thanks for this advice. I think over the years I've just picked up bits of code and manipulated it to do what I want it to do and fallen into this 'separation' trap. With most of my projects not accessing tens of thousands of records there have been no time penalties in some code being run lots of times when there is no need so that hasn't been an issue for me. But I'll certainly start to look to do what I want outside of the drawing event. Share this post Link to post