code issue

drama22
drama22 used Ask the Experts™
on
i have this code to insert some node inside Tvirtuailstringtree it works but some time strange behavior happened

this Tvirtuailstringtree  are inside a Tpanel called pnlmc

here is the code


function AddMCVDTMIC(AVST: TCustomVirtualStringTree; ANode: PVirtualNode; AObject: TUMICX): PVirtualNode;
    var
      Data: PUserONMIC;
    begin
      Result := AVST.AddChild(ANode);
      Data := AVST.GetNodeData(Result);
      AVST.ValidateNode(Result, false);
      Data^.FObject := AObject;
    end;

procedure Tform1.Talkin;
var
Node: PVirtualNode;
Data: PUserData;
UMICX : TUMICX;
begin
Node := lookingTreeView(LineToid);
Data := VDT1.GetNodeData(Node);
if (Node <> nil) then
begin
VDT1.IsVisible[Node] := False;
vstmca.BeginUpdate;
try
vstmca.Clear;
UMICX := TUMICX.Create;
try Data.FObject.istalking := '12'; except end;
umicx.Username :=  Data.FObject.Username;
umicx.userUid := Data.FObject.userUid;
umicx.istalking := Data.FObject.istalking;
umicx.color := Data.FObject.color;
umicx.image := Data.FObject.image;

try AddMCVDTMIC(vstmca, nil, umicx);  except end;

finally
  vstmca.EndUpdate;
end;
end;

//

if (recorder.Active = True) and (LineToid = IntToStr(UniqueID)) then
begin
try button1.Visible := false; except end;
try stop1.Visible := True; except end;
end
else if (recorder.Active = false) then // enrecorder check
begin
try button1.Enabled := false; except end;
try button1.Caption := 'streamin'; except end;
end;


try pnlmc.Height := Data.FObject.iHeigh; except end;
try panlmtr.Height := Data.FObject.iHeigh; except end;
try vr.Height := Data.FObject.iHeigh; except end;
try vrl.Height := Data.FObject.iHeigh; except end;
try vrl.Top := 0; except end;
try vr.Top := 0; except end;
try pnlmc.Visible := True; except end;
Try validate.Enabled := True; except end;

end;

Open in new window


the issue is some times when this code requested its insert the node normal and every thing okay but when its comes to try pnlmc.Visible := True; except end; the pnlmc didn't show and stay invisible and some times it works and shows normal what could be the issue can i write the code better to avoid problems ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
That happens when one try-except block exits with exception prior reaching pnlmc.visible:= true.

No need to put button.visible in try-except.
My advice would be rearranging your scattered try-except blocks keeping all code under one or multiple try-except for validations enclosed with only one try-finally which contains the visual properties.

Author

Commented:
I would love to see your english suggested in coding manner the code already posted showing how to write it better will help
Top Expert 2011

Commented:
Am using my phone and definitely it's not suitable to write code, but I will try.

function AddMCVDTMIC(AVST: TCustomVirtualStringTree; ANode: PVirtualNode; AObject: TUMICX): PVirtualNode;
    var
      Data: PUserONMIC;
    begin
      Result := AVST.AddChild(ANode);
      Data := AVST.GetNodeData(Result);
      AVST.ValidateNode(Result, false);
      Data^.FObject := AObject;
    end;

procedure Tform1.Talkin;
var
Node: PVirtualNode;
Data: PUserData;
UMICX : TUMICX;
begin
Node := lookingTreeView(LineToid);
Data := VDT1.GetNodeData(Node);
//Try
if (Node <> nil) then
begin
  VDT1.IsVisible[Node] := False;
  vstmca.BeginUpdate;
Try
  Try
    vstmca.Clear;
    UMICX := TUMICX.Create;
    Data.FObject.istalking := '12';
    umicx.Username := Data.FObject.Username;
    umicx.userUid := Data.FObject.userUid;
    umicx.istalking := Data.FObject.istalking;
    umicx.color := Data.FObject.color;
    umicx.image := Data.FObject.image;

    AddMCVDTMIC(vstmca, nil, umicx);
  except
    // what happens in the interface when there is an error (the panel and the buttons)?
end;

finally
  vstmca.EndUpdate;
end;
end;

//

if (recorder.Active) and (LineToid = IntToStr(UniqueID)) then
begin
  button1.Visible := false;
  stop1.Visible := True;
end
else if not(recorder.Active) then // enrecorder check
begin
  button1.Enabled := false;
  button1.Caption := 'streamin';
end;

// If there should be a situation where the following should not be executed it can be validated here (like when data or node are nil).

  pnlmc.Height := Data.FObject.iHeigh;
  panlmtr.Height := Data.FObject.iHeigh;
  vr.Height := Data.FObject.iHeigh;
  vrl.Height := Data.FObject.iHeigh;
//Finally
//activate this try-finally to avoid neglecting the following lines when try-except from above escapes the routine.

  vrl.Top := 0;
  vr.Top := 0;
  pnlmc.Visible := True;
  validate.Enabled := True;
//End;
end;

Author

Commented:
i did exactly as what you suggest still  issue happend panel did not show some times here is the updated code

procedure Tform1.Talkin;
var
Node: PVirtualNode;
Data: PUserData;
UMICX : TUMICX;
begin
Node := lookingTreeView(LineToid);
Data := VDT1.GetNodeData(Node);
try
if (Node <> nil) then
begin
VDT1.IsVisible[Node] := False;
vstmca.BeginUpdate;
try
try
vstmca.Clear;
UMICX := TUMICX.Create;
try Data.FObject.istalking := '12'; except end;
umicx.Username :=  Data.FObject.Username;
umicx.userUid := Data.FObject.userUid;
umicx.istalking := Data.FObject.istalking;
umicx.color := Data.FObject.color;
umicx.image := Data.FObject.image;
AddMCVDTMIC(vstmca, nil, umicx);  except end;


finally
  vstmca.EndUpdate;
end;
end;

if (recorder.Active = True) and (LineToid = IntToStr(UniqueID)) then
begin
  button1.Visible := false;
  stop1.Visible := True;
end
else if (recorder.Active = false) then // enrecorder check
begin
  button1.Enabled := false;
  button1.Caption := 'streamin';
end;


  pnlmc.Height := Data.FObject.iHeigh;
  panlmtr.Height := Data.FObject.iHeigh;
  vr.Height := Data.FObject.iHeigh;
  vrl.Height := Data.FObject.iHeigh;


finally

vrl.Top := 0;
vr.Top := 0;
pnlmc.Visible := True;
 validate.Enabled := True;

end;

end;

Open in new window


i want guarantee  that pnlmc panel to be visible
Top Expert 2011

Commented:
Alright that will require some more info.

Where in your code you hide that panel?
Make sure you don't have oversight somewhere, for instance when you set pnlmc.Height:= Data.FObject.iHeigh is iheigh having a proper value (i.e > 0)?
Also check, is the procedure talkin gets called atall?
And wherelse do you hide/show the panel?
You need to show more code.

Author

Commented:
I set pnlmc visible to false thats how I hide it and set it to true to show and set it to false again to hide it again I height is not set to zero basically its node that already exist in another list so its guarantee that all data set correctly

Author

Commented:
any idea whats up
Top Expert 2011
Commented:
Please if it is not a problem would you show as much code as possible?

There could be a problem some where else like at the routine where you hide the panel, or right before you call talkin procedure. Will not be possible until complete code is shown.

Also, is it possible that lookingTreeView(LineToid) or VDT1.GetNodeData(Node) error out and making the procedure to break execution?
Just in case, move the TRY at line 9 to be right after the main begin of talkin procedure.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial