Solved

Multiple MDIChild question. (hard)

Posted on 2003-11-05
6
235 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, DigitalPersona Fingerprint reader and MySQL 6 272
Downloading email attachments 2 74
Reconfigure Delphi Install? 2 51
Firemonkey allowing RTL on android 6 33
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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