Solved

Multiple MDIChild question. (hard)

Posted on 2003-11-05
6
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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