Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple MDIChild question. (hard)

Posted on 2003-11-05
6
Medium Priority
?
241 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 280 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: 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.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

609 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