Solved

Multiple MDIChild question. (hard)

Posted on 2003-11-05
6
229 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now