Search code examples
delphipopupmenu

Popup menu doesn't refresh imediatly in Delphi


I have a popupmenu which open on a right click event when clicking on a StringGrid. In this menu i have an option that colors the column. Let's say i have an option that colors the selected column in blue. When i click on Blue the column color is set to blue and i have a bool value that says that a column has been colored.

My problem is that i want to disable the option to color the column in blue if the column is any other than the colorod column. I have an array in which i stored all the data from the grid and to whom i'm adding a #colored tag at the end of the header name in my array.

Right now i'm doing this:

in my coloring option of the popup menu. We can color only one column with the color blue.

if Pos('#',sourceData[0,grid.col]) <> 0 then
   SourceData[0,grid.col] := COPY(SourceData[0,grid.col],0, Pos('#', SourceData[0,grid.col])-1);

columnColored := true;

There some other code that colors the column but it isn't important here.

Then in string grid mousedownevent:

if Button = mbRight then
  begin
    if columnColored then
      begin
        if Pos('#colored',SourceData[0,grid.col]) <> 0 then
            pmColorBlue.enabled := true
        else
            pmColorBlue.enabled := false;
      end;
  end;

THE PROBLEM

This is working the problem is that it have a latence. When i right click on the column that's been colored i want to be able to reclick on it and set default oclor back again. But if the column is different than the one that is colored the option should remain disabled.

The thing is that when i click on the colored column then right after i click on another not colored column. The Blue option in my oppup menu is still available, if i reclick again on the same column the blue option is disabled. So i basicly have to click twice on a column to disable the color opton from my opup menu.

How do i fix it so when i click on the colored column the option is enabled, but when i click on another the option should be disabled. Now to disable the option i have to click twice right button cause hte first time the option is still available. And it's the same probleme if i click firstly on a non colored column and then on my colored column. First the option is disabled, then i click on my colored column the option still remains disabled, i reclick on my colored column and the option is enabled.

I don't know why it does this. Can you pls help me?


Solution

  • Move the code that is on your StringGridMouseDown event to the OnPopup event of the popup menu. The events are probably fired in the (for your case) wrong order.