Solved

Multiple MDIChild question. (hard)

Posted on 2003-11-05
6
236 Views
Last Modified: 2010-04-05
Hi. I have a problem in this code. When this function execute and if Im not in the active window the program again opens the new window with the same name and append the lines. I wanna that if window is already opened then append lines to that window. And if its not opened then create it and append lines. But howwww ???? :///

Take a look. I tryed a lot of combinations and .....


begin
 for I := 0 to MDIChildCount - 1 do
  begin

  if MDIChildren[I].Caption <> NickName then
    CreateMDIChild(NickName);
    break
    end;
       for K := 0 to MDIChildCount - 1 do
       begin
        if MDIChildren[K].Caption = NickName then
        begin
        with (MDIChildren[K] as TMDIChild).RichEdit1 do
   Lines.Append(('<')+(Nickname)+('> ')+(Content));
   break;
        end;
        end;
end;


Pleas help :///


Thanks
0
Comment
Question by:Polde
  • 3
  • 3
6 Comments
 

Expert Comment

by:opreacatalin
ID: 9686028
Your solutions is below:

procedure TMainForm.FindChild(NickName,Content:string);
var i:integer;
    flag:boolean;
begin
  if MDIChildCount<0 then
    exit;
  flag:=false;
  for I := 0 to MDIChildCount - 1 do
  begin
    if UpperCase(MDIChildren[I].Caption)<>UpperCase(NickName) then
    begin
      flag:=false;
    end
    else
    begin
      flag:=true;
      with (MDIChildren[i] as TMDIChild).Memo1 do
      try
        Lines.Add('<'+NickName+'> '+Content);
      finally
        SetFocus;
      end;
    end;
  end;
  if not(flag) then
  begin
    CreateMDIChild(NickName);
    with (MDIChildren[MDIChildCount-1] as TMDIChild).Memo1 do
    try
      Lines.Add('<'+NickName+'> '+Content);
    finally
      SetFocus;
    end;
  end;
end;

Success !
0
 

Author Comment

by:Polde
ID: 9686196
the program stops on this line :
with (MDIChildren[MDIChildCount-1] as TMDIChild).TrichEdit1 do

---------------------------
Debugger Exception Notification
---------------------------
Project MDIAPP.exe raised exception class EInvalidCast with message 'Invalid class typecast'. Process stopped. Use Step or Run to continue.
---------------------------
OK   Help  
---------------------------
0
 

Expert Comment

by:opreacatalin
ID: 9697888
I will put more code here...
[...]
  private
    { Private declarations }
    procedure CreateMDIChild(const Name: string);
    procedure FindChild(NickName,Content:string);
  public
[...]
procedure TMainForm.ToolButton12Click(Sender: TObject);
begin
  FindChild(InputBox('Window Search', 'Window Title?', 'default'), InputBox('Content','Text','???'));
end;
[...]
procedure TMainForm.FindChild(NickName,Content:string);
var i:integer;
    flag:boolean;
begin
  if MDIChildCount<0 then
    exit;
  flag:=false;
  for I := 0 to MDIChildCount - 1 do
  begin
    if UpperCase(MDIChildren[I].Caption)<>UpperCase(NickName) then
    begin
      flag:=false;
    end
    else
    begin
      flag:=true;
      with (MDIChildren[i] as TMDIChild).Memo1 do
      try
        Lines.Add('<'+NickName+'> '+Content);
      finally
        MDIChildren[i].SetFocus;
        MDIChildren[i].BringToFront;
      end;
      break;
    end;
  end;
  if not(flag) then
  begin
    CreateMDIChild(NickName);
    FindChild(NickName,Content:string);
  end;
end;

Sorry for earlier error, but it doesn't belong to my code (on 2 computers with Delphi 6 is functioning normaly). Your error is from syntax: "MDIChildren[MDIChildCount-1] as TMDIChild", your MDIChildren variabile differs isn't compatible with TMDIChild class. Verify this, and the code I'v last posted should work fine and ok.

I wish you happy coding :).
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Polde
ID: 9699526
Hi this code works prfectly !!! :))). But the only problem is that the first line is not shown. Only second. When MDICreate new window and he must also append the first line :/. And one more question. Can you comment this code ? Because Im begginer and I dont know what this flags and setfocus and bringtofront do. Can you comment a leathel this code :). Damn I find the soolution here and I stuck in other place :))).


Have a nice time .)
0
 

Accepted Solution

by:
opreacatalin earned 70 total points
ID: 9700378
OK, let's comment this code:

procedure TMainForm.FindChild(NickName,Content:string);
var i:integer;
    flag:boolean; //Type boolean, 2 values possible : true or false
begin
  //If no. of MDIWindows is smaller than 0 (no MDI Childs created) then exit from procedure
 if MDIChildCount<0 then
    exit;
  //Init of variabile flag, used to indicate if we have find the "nickname" or not
  flag:=false;
  for I := 0 to MDIChildCount - 1 do
  begin
    //Comparation between NickName and the Caption of every MDI Windows is done with "case insensitive"
    if UpperCase(MDIChildren[I].Caption)<>UpperCase(NickName) then
    begin
     //Is different so we set the flag to false
      flag:=false;
    end
    else
    begin
      //it's equal, so we set the flag true and we add content to the Memo or Richedit
      flag:=true;
      with (MDIChildren[i] as TMDIChild).Memo1 do
      try
        Lines.Add('<'+NickName+'> '+Content);
      finally
        //SetFocus - the edit window will be the current MDIChild
        MDIChildren[i].SetFocus;
        //BringToFront should Bring the current MDIChild to front (above other windows)
        MDIChildren[i].BringToFront;
      end;
      //Then we exit from "for ...", we have find what we looking for
      break;
    end;
  end;
  //If the window doesn't find (flag is false) then we create the MDI Window
  if not(flag) then
  begin
    CreateMDIChild(NickName);
    //We recursively use the procedure it self after creatig MDI Window, to optimize the process
    FindChild(NickName,Content:string);
  end;
end;

I think it's ok now. Good luck.
0
 

Author Comment

by:Polde
ID: 9700594
Damn. I wish that on this world would be more peoples like you. Thank you very much !!!! :). And something else :). Can you add a code that when MDI is created also lines are added :).


THANKS !!!!



Have a nice time .)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 95
Delphi Form ownership 4 110
Strange behavior when a form is closed 6 61
oracle global variables 4 76
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

839 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