Solved

dbgrid focus error

Posted on 2008-10-07
10
691 Views
Last Modified: 2010-04-21
hi all,

i have this strange error which seems to be a delphi bug. I have a mdi application. Main form has a treeview and a button. mdichild form has a dbgrid. When i run the application i click on the button in main form to open mdi child form. then i click dbgrid in mdi child form. then i click the treeview on the main form. when i want to go back to dbgrid on mdi child form this error comes. the dbgrid wont get focus. so i need to minimize mdi child form or click another component on the form, then i can click on dbgrid. Please see attach code to be clear. I have reported this problem to Quality Central (#67627), and for sure i wont get the answer anytime soon.

I also had this problem when i use delphi 7. But i can solve it simply by adding "PostMessage(Handle, WM_ACTIVATE, WA_INACTIVE, 0);" on FormActive event. But this wont work in delphi 2007.

Please experts help me solve this problem.

Thanks in advance and sorry for my poor english.

Reynaldi
--------------- Main Form .dfm ---------------------------
 
 
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 419
  ClientWidth = 672
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  FormStyle = fsMDIForm
  OldCreateOrder = False
  Position = poDesktopCenter
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 0
    Top = 29
    Width = 121
    Height = 390
    Align = alLeft
    Indent = 19
    TabOrder = 0
    Items.NodeData = {
      01050000001B0000000000000000000000FFFFFFFFFFFFFFFF00000000000000
      000131001B0000000000000000000000FFFFFFFFFFFFFFFF0000000000000000
      0132001B0000000000000000000000FFFFFFFFFFFFFFFF000000000000000001
      33001B0000000000000000000000FFFFFFFFFFFFFFFF00000000000000000134
      001B0000000000000000000000FFFFFFFFFFFFFFFF0000000000000000013500}
  end
  object ToolBar1: TToolBar
    Left = 0
    Top = 0
    Width = 672
    Height = 29
    ButtonHeight = 21
    ButtonWidth = 48
    Caption = 'ToolBar1'
    ShowCaptions = True
    TabOrder = 1
    object ToolButton1: TToolButton
      Left = 0
      Top = 0
      Caption = 'Button 1'
      ImageIndex = 0
      OnClick = ToolButton1Click
    end
  end
end
 
 
 
 
 
--------------- Main Form .Pas ---------------------------
 
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin;
 
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses
  unit2;
 
{$R *.dfm}
 
procedure TForm1.ToolButton1Click(Sender: TObject);
var
  frm: TForm2;
begin
  frm := TForm2.Create(self);
end;
 
end.
 
 
 
 
 
 
 
--------------------------- mdi child .dfm --------------------------------
 
 
 
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 216
  ClientWidth = 243
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  FormStyle = fsMDIChild
  OldCreateOrder = False
  Visible = True
  OnClose = FormClose
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 8
    Top = 40
    Width = 225
    Height = 168
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object Edit1: TEdit
    Left = 8
    Top = 8
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'Edit1'
  end
  object ds1: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 168
    Top = 8
    object ds1field1: TStringField
      FieldName = 'field1'
    end
  end
  object DataSource1: TDataSource
    DataSet = ds1
    Left = 200
    Top = 8
  end
end
 
 
 
 
 
-------------------------- mdi child .pas ---------------------------------------
 
 
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBClient, StdCtrls, Grids, DBGrids;
 
type
  TForm2 = class(TForm)
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    ds1: TClientDataSet;
    ds1field1: TStringField;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
implementation
 
{$R *.dfm}
 
 
 
procedure TForm2.FormCreate(Sender: TObject);
begin
  ds1.CreateDataSet;
  ds1.Open;
  ds1.InsertRecord(['1']);
  ds1.InsertRecord(['2']);
  ds1.InsertRecord(['3']);
  ds1.InsertRecord(['4']);
  ds1.InsertRecord(['5']);
end;
 
 
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action := cafree;
end;
 
 
 
 
end.

Open in new window

0
Comment
Question by:reynaldio
10 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 22663148
"this error comes"
>>What is the error?
0
 
LVL 2

Author Comment

by:reynaldio
ID: 22663205
hi rfwoolf,

sorry for not being clear. the problem is that dbgrid wont get focus. i can click any other control on the form, but not dbgrid. this problem only occurs with mdi application.

thanks
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22666877
set the parent of the child control to the mdi form


procedure TForm1.ToolButton1Click(Sender: TObject);
var
  frm: TForm2;
begin
  frm := TForm2.Create(self);
  frm.parent := Self;
end;

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Author Comment

by:reynaldio
ID: 22668091
hi all,

Geert_Gruwez -> i implemented your code but i get this error "cannot focus a disabled or invinsible windows". What is this?

Thanks
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22676547
any other code you have added besides the code you posted
for the problematic form ?
0
 
LVL 2

Author Comment

by:reynaldio
ID: 22679399
hi,

no other codes. and i only use standard component. and it only happen with mdi application.
i also had this problem in delphi 7, But i can solve it simply by adding "PostMessage(Handle, WM_ACTIVATE, WA_INACTIVE, 0);" on FormActive event. But this wont work in delphi 2007.

thanks.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22684784
if you don't set the parent of the form to the mdiform
then it's not an mdichild form

mdichild means it's a child of an mdiform or the parent must be set

the mdichild behave erratic otherwise

i created a new mdi application with the delphi wizard
no problems (after i added the frm.parent := self)
0
 
LVL 2

Author Comment

by:reynaldio
ID: 22700441
This is how i create mdichild

frm := TMdiChild.Create(Self); --> self is mdi parent
frm.parent := self; --> this raise an error

but if i remove, "frm.parent", the mdi works fine except for the problem i mention above.

Thanks,

Reynaldi
0
 
LVL 11

Accepted Solution

by:
calinutz earned 500 total points
ID: 25488888
0
 
LVL 2

Author Closing Comment

by:reynaldio
ID: 31503993
Thanks alot, that saved my day
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 79
delphi exception 7 64
error 1.1 400 Bad request idhttp delphi 18 87
find a node in VST 2 68
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question