?
Solved

Static DBRichEdit vs Dynamic DBRichEdit.

Posted on 2010-01-11
24
Medium Priority
?
393 Views
Last Modified: 2012-05-08
Dear Experts,

In my application I use a pagecontrol with on every tabsheet a dbrichedit-component
set to align = alclient. The procedures that I have put in the code-section works great
with a static dbrichedit. But now I have deleted all tabsheets with their dbrichedit's
and made a procedure that creates the tabsheets with the dbrichedit dynamicly.

Now the procedures in the code-section have to call for the dynamicly dbrichedit!
Who can help me with these procedures?

Greetings, Peter Kiers

procedure TMainForm.PlayBtnClick(Sender: TObject);
begin
  if not BeenPaused then
    SpVoice1.Speak(DBRichEdit1.Text, SVSFlagsAsync)  <==call the active dbrichedit
  else
  begin
    SpVoice1.Resume;
    BeenPaused := False
  end
end;

procedure TMainForm.SetTextHilite(FirstChar, Len: Integer);
begin
  DBRichEdit1.SelStart := FirstChar;           <=======
  DBRichEdit1.SelLength := Len;                <=======
end;

procedure TMainForm.SetTextStyle(FirstChar, Len: Integer; Styles: TFontStyles);
begin
 with DBRichEdit1 do      <===========
  begin
    Lines.BeginUpdate;
    try
      SelStart := FirstChar;
      SelLength := Len;
      SelLength := 0;
    finally
      Lines.EndUpdate
    end
  end
end;

procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  V: Variant;
  e: Boolean;
begin
  if Assigned(TreeView1.Selected) then
  begin
    V := Integer(TreeView1.Selected.Data);
    if V > 0 then ADOTable1.Locate('ID', V, []);
    e := TreeView1.Selected.ImageIndex = Item_Image_Index;
  end else e := False;
//  DbRichEdit1.Enabled := e;            <==call for all the dbrichedits not just 1 and 2
//  DbRichEdit2.Enabled := e;            <===
end;

Open in new window

0
Comment
Question by:peterkiers
[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
  • 12
  • 7
  • 4
  • +1
24 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 26281854
I think I need to see your function that creates the PageControls and DBRichEdits as well to be able to help you...
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26281890
Here it is:

procedure TMainForm.NewTab;
procedure CreateColumn(ColumnName: String);
  var
    q: TAdoQuery;
  begin
    q := TAdoQuery.Create(nil);
    try
      q.Connection := ADOConnection1;
      q.SQL.Add('Alter table Tree Add Column '+ColumnName+' OLEOBJECT');
      try
        q.ExecSQL;
      except
        on E: Exception do
          showmessage('Error: '+E.Message);
      end;
    finally
      q.Free;
    end;
  end;
var
  aForm: TDBRichEdit;
  tabSheet: TTabSheet;
  Node: TTreeNode;
  V: Variant;
begin
  tabSheet := TTabSheet.Create(PageControl1);
  tabSheet.PageControl := PageControl1;
  aForm := TDBRichEdit.Create(tabSheet);
  aForm.Parent := tabSheet;
  aForm.Name := 'DBRichedit' + IntToStr(tabSheet.tabindex+1);
  aForm.Align := alClient;
  aForm.Visible := True;
  aForm.DataSource := DataSource1;
  if (ADOTable1.FindField('Data' + IntToStr(tabSheet.tabindex+1)) = nil) then
  begin
    Node := TreeView1.Selected;
    V := integer(Node.Data);
    ADOTable1.Close;
    CreateColumn('Data' + IntToStr(tabSheet.tabindex+1));
    ADOTable1.Open;
    TreeView1.Select(Node);
    ADOTable1.Locate('ID', V, []);
  end;
  aForm.DataField := 'Data' + IntToStr(tabSheet.tabindex+1);
  tabSheet.Name := 'TabSheet' + IntToStr(tabSheet.tabindex+1);
  tabSheet.caption := 'New Tab' + '(' + IntToStr(tabSheet.tabindex+1) + ')';
  PageControl1.ActivePage := tabSheet;
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26281919
you would need to create a function to find the active DBRichEdit


function TMainForm.ActiveDBRichEdit: TDBRichEdit;
var tab: TTabSheet;
  I: Integer;
begin
  Result := nil;
  tab := nil;
  if ActiveControl is TDBRichEdit then 
    Result := TDBRichEdit(ActiveControl)
  else
  begin
    if ActiveControl is TPageControl
      tab := PageControl1.ActivePage
    else if ActiveControl is TTabSheet then 
      tab := TTabSheet(ActiveControl);
  end;
  if Assigned(tab) then 
    for I := 0 to tab.ControlCount-1 do 
      if tab.controls[I] is TDBRichEdit then 
      begin
        Result := TDBRichEdit(tab.Controls[I]);
        Break;
      end;
end;

Open in new window

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.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26281927
you would use the function like this

procedure TMainForm.SetTextStyle(FirstChar, Len: Integer; Styles: TFontStyles);
var re: TDBRichEdit;
begin
  re := ActiveDBRichEdit;
  if Assigned(re) then
   with re do // DBRichEdit1 do      <===========
   begin
      // the rest
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26281975
Oke, what about selecting all of them:

procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  V: Variant;
  e: Boolean;
begin
  if Assigned(TreeView1.Selected) then
  begin
    V := Integer(TreeView1.Selected.Data);
    if V > 0 then ADOTable1.Locate('ID', V, []);
    e := TreeView1.Selected.ImageIndex = Item_Image_Index;
  end else e := False;
//  DbRichEdit1.Enabled := e;            <==call for all the dbrichedits not just 1 and 2
//  DbRichEdit2.Enabled := e;            <===
end;

P.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26282012
Error: "THEN expected but identifier 'tab' found":

function TMainForm.ActiveDBRichEdit: TDBRichEdit;
var tab: TTabSheet;
  I: Integer;
begin
  Result := nil;
  tab := nil;
  if ActiveControl is TDBRichEdit then
    Result := TDBRichEdit(ActiveControl)
  else
  begin
    if ActiveControl is TPageControl
      tab := PageControl1.ActivePage    <============
    else if ActiveControl is TTabSheet then
      tab := TTabSheet(ActiveControl);
  end;
  if Assigned(tab) then
    for I := 0 to tab.ControlCount-1 do
      if tab.controls[I] is TDBRichEdit then
      begin
        Result := TDBRichEdit(tab.Controls[I]);
        Break;
      end;
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282052
add the then ... i typed this in the editor, not in delphi
i'm not fullproof syntax checker obviously :)

if ActiveControl is TPageControl then
      tab := PageControl1.ActivePage    <============
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282073
well .. you would have find all the richedits
i wouldn't use the name to find them
you may get the idea, later on in life, to give the richedit a more significant name

for me i would use a callback to find all richedits
but this is rather difficult to understand
it is easier if you have to do 3 or more different functionalities with all richedits
so if you are interested in such a callback, state it

otherwise, something like this:

procedure TMainForm.SetStateAllDBRe(SetEnabled: Boolean);
var I: Integer;
begin
  for I := 0 to ComponentCount-1 do
    if Components[I] is TDBRichEdit then
      TDBRichEdit(Components[I]).Enabled := SetEnabled;
end;

and use it like
  SetStateAllDBRe(True);
to enable all dbrichedits
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 400 total points
ID: 26282113
callback way, first call for iteration, second : proc to do stuff on

type
  TIterDBRe = procedure (RichEdit: TDBRichEdit; Value1: String; Value2: Integer);

procedure TMainForm.IterateDBRe(proc: TIterDBRe; Value1: String; Value2: Integer););
var I: Integer;
begin
  for I := 0 to ComponentCount-1 do
    if Components[I] is TDBRichEdit then
      proc(TDBRichEdit(Components[I]), Value1, Value2);
end;

to enable/disable the richedit
procedure TMainForm.StateDBRe(RichEdit: TDBRichEdit; Value1: String; Value2: Integer);
begin
  RichEdit.Enabled := Value2 = 1;
end;

to use:
uses Math; // need this for IfThen function

procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  V: Variant;
  e: boolean;
begin
  if Assigned(TreeView1.Selected) then
  begin
    V := Integer(TreeView1.Selected.Data);
    if V > 0 then ADOTable1.Locate('ID', V, []);
    e := TreeView1.Selected.ImageIndex = Item_Image_Index;
  end else e := False;
  IterateDBRe(StateDBRe, '', IfThen(e, 1, 0));
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26282150
I have change this procedure:

procedure TMainForm.PlayBtnClick(Sender: TObject);
var re: TDBRichEdit;
begin
  re := ActiveDBRichEdit;
  if Assigned(re) then
  if not BeenPaused then
    SpVoice1.Speak(re.Text, SVSFlagsAsync)
  else
  begin
    SpVoice1.Resume;
    BeenPaused := False
  end
end;

But the play-btn doesn't work, did i do it wrong?

P.
0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 1600 total points
ID: 26282215
My suggestion

procedure TMainForm.SetTextHilite(FirstChar, Len: Integer);
var
i: Integer;
begin
  for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        TDBRichEdit(PageControl1.ActivePage.Components[i]).SelStart := FirstChar;
        TDBRichEdit(PageControl1.ActivePage.Components[i]).SelLength := Len;  
        break;
      end;
end;

procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  V: Variant;
  e: boolean;
  i,x: Integer;
begin
  if Assigned(TreeView1.Selected) then
  begin
    V := Integer(TreeView1.Selected.Data);
    if V > 0 then ADOTable1.Locate('ID', V, []);
    e := TreeView1.Selected.ImageIndex = Item_Image_Index;
  end else e := False;
for i:= 0 to Pagecontrol1.PageCount -1 do
    if i <> PageControl1.ActivePageIndex then
      for x := PageControl1.Pages[i].ComponentCount - 1 downto 0 do
        if PageControl1.Pages[i].Components[x] is TDBRichEdit then
           TDBRichEdit(PageControl1.Pages[i].Components[x]).enabled :=e;  
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26282267
I go for Ferruccio's suggestion. What do i have to change in this procedure:

procedure TMainForm.SetTextStyle(FirstChar, Len: Integer; Styles: TFontStyles);
begin
 with DBRichEdit1 do      <===========
  begin
    Lines.BeginUpdate;
    try
      SelStart := FirstChar;
      SelLength := Len;
      SelLength := 0;
    finally
      Lines.EndUpdate
    end
  end
end;

P.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26282291
Like in SetTextHilite

var
i: Integer;
begin
  for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        With TDBRichEdit(PageControl1.ActivePage.Components[i]) do
        begin
           Lines.BeginUpdate;
              try
                 SelStart := FirstChar;
                 SelLength := Len;
                 SelLength := 0;
              finally
                 Lines.EndUpdate
             end;
       end;
         break;
     end;
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282306
Peter ,
 the begin ... end is important too
otherwise ... you get other programmatic logic

procedure TMainForm.PlayBtnClick(Sender: TObject);
var re: TDBRichEdit;
begin
  re := ActiveDBRichEdit;
  if Assigned(re) then
  begin
    if not BeenPaused then
      SpVoice1.Speak(re.Text, SVSFlagsAsync)
    else
    begin
      SpVoice1.Resume;
      BeenPaused := False
    end;
  end;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26282313
just one last reqeust before i give the points to you.

Can you help me with this procedure too, because i have a little
difficulti do it my own:

procedure TMainForm.PlayBtnClick(Sender: TObject);
begin
  if not BeenPaused then
    SpVoice1.Speak(DBRichEdit1.Text, SVSFlagsAsync)  <==call the active dbrichedit
  else
  begin
    SpVoice1.Resume;
    BeenPaused := False
  end
end;

P.


0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282373
Peter,

what doesn't work in ActiveDBRichEdit
(except for the 4 letter typo: "then")
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26282374
var
i: Integer;
begin
  for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        With TDBRichEdit(PageControl1.ActivePage.Components[i]) do

procedure TMainForm.PlayBtnClick(Sender: TObject);
var
i :
Integer
begin
  if not BeenPaused then
  begin
    for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        SpVoice1.Speak(TDBRichEdit(PageControl1.ActivePage.Components[i]).Text, SVSFlagsAsync);
        break;
      end;
  end
  else
  begin
    SpVoice1.Resume;
    BeenPaused := False
  end
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282381
here is the complete function again
function TMainForm.ActiveDBRichEdit: TDBRichEdit;
var tab: TTabSheet;
  I: Integer;
begin
  Result := nil;
  tab := nil;
  if ActiveControl is TDBRichEdit then 
    Result := TDBRichEdit(ActiveControl)
  else
  begin
    if ActiveControl is TPageControl then
      tab := PageControl1.ActivePage
    else if ActiveControl is TTabSheet then 
      tab := TTabSheet(ActiveControl);
  end;
  if Assigned(tab) then 
    for I := 0 to tab.ControlCount-1 do 
      if tab.controls[I] is TDBRichEdit then 
      begin
        Result := TDBRichEdit(tab.Controls[I]);
        Break;
      end;
end;

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282386
Peter, Ferruccio68,

This question changed halfwayinto a second part
So at least 2 solutions ...

And Peter, Ferruccio68's solution is to run the Play button for every RichEdit
Not just the active one ...


0
 
LVL 1

Author Comment

by:peterkiers
ID: 26282388
Geert, I have tried all your solution but it didn't work.
The solutions that Ferrucio gave me works great.

Greetings,

Peter Kiers
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282397
Peter,
You asked 2 questions ...

First for the Active DBRichEdit, then added a part for all the DBRichEdits

Are you sure you want to press play for all the richedits ?
This would be like every one in a choir in church singing a different song at the same time
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26282403
Geert, about the changes of the question I agree, but about the play button not
(even if there's a part of pasted code by mistake that could point in wrong way. I'm coding directly on the ExEx editor)

This Procedure means only the activepage Richedit, that is what he's asking

procedure TMainForm.PlayBtnClick(Sender: TObject);
var
i :
Integer
begin
  if not BeenPaused then
  begin
    for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        SpVoice1.Speak(TDBRichEdit(PageControl1.ActivePage.Components[i]).Text, SVSFlagsAsync);
        break;
      end;
  end
  else
  begin
    SpVoice1.Resume;
    BeenPaused := False
  end
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282412
ow sorry,
i missed the break,
it'll play the text on the first tabsheet which has a DBRichEdit

not the one which is visible ...
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26282421
lol, i didn't see ActivePage either
must be the age, i'm nearing a too old age i guess

sorry about that

Ferruccio --> editor, yeah me too, except it's the firefox editor
0

Featured Post

Technology Partners: 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!

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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