Recent

Author Topic: [SOLVED] TEditButton usage  (Read 4881 times)

Rayvenhaus

  • Jr. Member
  • **
  • Posts: 70
[SOLVED] TEditButton usage
« on: October 24, 2016, 02:14:38 am »
I've searched the Internet over as well as this forum but I can't seem to find anything that answers me question though I am sure the answer will be so simple I'll feel appropriately chagrined.

I've created a setup form for my program. On it, I have several TEditButtons that I want to use to allow the user to select a directory and file name, when the click on the button and have that populated back into the edit part of the EditButton.

Do I need to create a form with an OpenDialog, etc to do this? If so, how can I tell which file was chosen by the but the file dialog form?

I'm not asking anyone to write the code for me, just give me a nudge in the right direction .

Thanks!!!
« Last Edit: October 24, 2016, 02:27:59 pm by Rayvenhaus »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TEditButton
« Reply #1 on: October 24, 2016, 02:24:42 am »
Do I need to create a form with an OpenDialog, etc to do this? If so, how can I tell which file was chosen by the but the file dialog form?
Yes in principle, when using a TEditButton you must program that manually.

You can drop a TOpendialog component on your form (it is a non visual component that automatically opens up a selection dialog when being executed using the execute method).

Because being non-visual you could just as well create it dynamically if you like (although there is an extensive list of properties/options which is perhaps better managed using the object inspector in case unfamiliar with the component).

Quote
On it, I have several TEditButtons that I want to use to allow the user to select a directory and file name, when the click on the button and have that populated back into the edit part of the EditButton.
Why not use TFileNameEdit and/or TDirectoryEdit instead ?
« Last Edit: October 24, 2016, 02:39:33 am by molly »

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: TEditButton
« Reply #2 on: October 24, 2016, 06:13:25 am »
Here is another way....
Very interesting if you have a form that is "fsStayOnTop" or "fsSystemStayOnTop"... or if you want your own style...

Code: Pascal  [Select][+][-]
  1. Unit uSelectFolder;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  USES
  6.   Classes,  SysUtils, Forms,   Controls,
  7.   StdCtrls, Graphics, LCLType, ShellCtrls;
  8.  
  9.  TYPE
  10.   SelectFolder = Class
  11.  
  12.    Class Procedure OnKeyDownDIR (Sender: TObject; Var Key: Word;
  13.                                  Shift: TShiftState);
  14.     PRIVATE
  15.      Class Var strResult: String;
  16.      Class Var stvDIR   : TShellTreeView;
  17.  
  18.     PUBLIC
  19.      Class Function Show: String;
  20.   End;
  21.  
  22. Implementation
  23.  
  24.  
  25. Class Function SelectFolder.Show: String;
  26.   Var
  27.    wndDIR: TForm;
  28.  Begin
  29.   Result:= '';
  30.  
  31.   wndDIR:= TForm.Create(Nil);
  32.    Try
  33.     wndDIR.BorderStyle          := bsSizeable;
  34.     wndDIR.BorderIcons          := [biSystemMenu, biMaximize];
  35.     wndDIR.Caption              := Application.Title;
  36.     wndDIR.Constraints.MinHeight:= 300;
  37.     wndDIR.Constraints.MinWidth := 400;
  38.     wndDIR.Height               := 500;
  39.     wndDIR.Width                := 700;
  40.     wndDIR.AutoScroll           := False;
  41.     wndDIR.Position             := poDesktopCenter;
  42.     wndDIR.FormStyle            := fsNormal;
  43.     wndDIR.KeyPreview           := True;
  44.     wndDIR.DoubleBuffered       := True;
  45.      wndDIR.OnKeyDown           := @OnKeyDownDIR;
  46.  
  47.     stvDIR                 := TShellTreeView.Create(wndDIR);
  48.     stvDIR.DoubleBuffered  := True;
  49.     stvDIR.BorderStyle     := bsSingle;
  50.     stvDIR.Font.Size       := 20;
  51.     stvDIR.Font.Quality    := fqAntialiased;
  52.     stvDIR.Font.Style      := [fsBold];
  53.     stvDIR.Align           := alClient;
  54.     stvDIR.AutoExpand      := True;
  55.     stvDIR.BackgroundColor := clBtnFace;
  56.     stvDIR.Root            := ExtractFileDrive(ParamStr(0));
  57.     stvDIR.Root            := '';
  58.     stvDIR.ObjectTypes     := [otFolders];
  59.     stvDIR.ReadOnly        := True;
  60.     stvDIR.ShowButtons     := True;
  61.     stvDIR.ShowRoot        := True;
  62.     stvDIR.HideSelection   := True;
  63.     stvDIR.ScrollBars      := ssAutoBoth;
  64.     stvDIR.TreeLinePenStyle:= psSolid;
  65.     stvDIR.TreeLineColor   := clLime;
  66.     stvDIR.ExpandSignColor := clLime;
  67.     stvDIR.Parent          := wndDIR;
  68.  
  69.     wndDIR.ShowModal;
  70.  
  71.     Result:= strResult;
  72.    Finally
  73.     wndDIR.Release;
  74.     wndDIR:= Nil;
  75.    End;
  76.  End;
  77.  
  78.  
  79. Class Procedure SelectFolder.OnKeyDownDIR(Sender: TObject; Var Key: Word;
  80.                                           Shift: TShiftState);
  81.  Begin
  82.   If Key = VK_RETURN
  83.   Then
  84.    Begin
  85.     strResult:= stvDIR.Path;
  86.     TForm(Sender).Close;
  87.    End;
  88.  
  89.   If Key = VK_ESCAPE
  90.   Then TForm(Sender).Close;
  91.  End;
  92.  
  93. End.
  94.  
  95. // =================
  96. // can be used like this:
  97.  
  98. Implementation
  99.  USES uSelectFolder;
  100.  {$R *.LFM}
  101.  
  102. Procedure TForm1.Button1Click(Sender: TObject);
  103.  Begin
  104.   Edit1.Text:= SelectFolder.Show;
  105.  End;  
  106.  
« Last Edit: December 30, 2016, 05:09:46 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4467
  • I like bugs.
Re: TEditButton
« Reply #3 on: October 24, 2016, 12:23:37 pm »
Why not use TFileNameEdit and/or TDirectoryEdit instead ?
Exactly. They inherit from EditButton and already implement all the features Rayvenhaus asked for.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Rayvenhaus

  • Jr. Member
  • **
  • Posts: 70
Re: TEditButton
« Reply #4 on: October 24, 2016, 02:27:24 pm »
Do I need to create a form with an OpenDialog, etc to do this? If so, how can I tell which file was chosen by the but the file dialog form?
Yes in principle, when using a TEditButton you must program that manually.

You can drop a TOpendialog component on your form (it is a non visual component that automatically opens up a selection dialog when being executed using the execute method).

Because being non-visual you could just as well create it dynamically if you like (although there is an extensive list of properties/options which is perhaps better managed using the object inspector in case unfamiliar with the component).

Quote
On it, I have several TEditButtons that I want to use to allow the user to select a directory and file name, when the click on the button and have that populated back into the edit part of the EditButton.
Why not use TFileNameEdit and/or TDirectoryEdit instead ?
Well....... Uhm...... Cause? I didn't see that component when I was first creating this. matter of fact, my first iteration was a simple editbox and a button. Then I discovered the EditButton. However, that lead me to trying to write the code for it and I was lost (As is usual).

However, as usual molly, you've provided me with the perfect answer. The TFileNameEdit functions just as I want.  Thanks so much for the help.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] TEditButton usage
« Reply #5 on: October 24, 2016, 10:04:20 pm »
@Rayvenhaus
In case you have the opportunity (time-wise), i would like to suggest to invest some time in 'fun' projects.

With 'fun' i meant creating a new application and just putting random components on it from the component palette to see what all these components are all about. There does not have to be a clear direction for such 'fun' project other then gaining some information. That way it can't hurt in case you mess something up as well  ;)

Some components are easy to grasp using the object inspector while others (for example the database and/or internet components) take a fair amount of knowledge before being able to use them properly. Point is, to getting familiar with them.

The example directory delivered with lazarus can sometimes give you hints on how to use a certain component in case you get stuck (although honestly said, i wasn't able to find much about TFilenameEdit), or sometimes a wiki articles or forum post can help you out with understanding.

Knowing your tool(set) is half the battle. (even though it could not have hurt in case you would have used a edit + button + opendialog components to accomplish the task on your own, as that is also valuable experience. if only to know what you do not have to do next time because you know about TFileNameEdit  :D ).
« Last Edit: October 24, 2016, 10:15:56 pm by molly »

 

TinyPortal © 2005-2018