Recent

Author Topic: Right click and parsing parameters  (Read 1050 times)

nimmer

  • Newbie
  • Posts: 6
Right click and parsing parameters
« on: June 04, 2020, 09:09:48 am »
Hi,

I'm writing a small program launcher (win64) in where I have a button for each program I want in my launcher. The settings for the buttons are stored in an ini-file. For now I have a settings box, in where I can change the settings for all the buttons (eg. Button-text, Program to launch and parameters if any).
But I would like to change my settings box to only contain one pair of parameters (Btn(x).Text, Btn(x)Command.Text, Btn(x)Parameter,Text) so that if I rightclick my Button2 it reads that its Button2 thats rightclicked, and then opens the settingsbox with only Button2.Text,Button2Command and Button2Parameter.

How do I do this?

Best regards

nimmer
« Last Edit: June 04, 2020, 09:17:45 am by nimmer »

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Right click and parsing parameters
« Reply #1 on: June 04, 2020, 09:38:36 am »
@nimmer
Instead of trying to answer a question asked about a black box, would it be possible for you to show the contents of your .ini file ?

The problem is that multiple solutions can be given but they might be given without giving any thought about your current implementation, which then could mean extra work for you to be able to try such a solution.

For instance do you currently use a section for each button individually ? or do you just use a list with name-value pairs ?


nimmer

  • Newbie
  • Posts: 6
Re: Right click and parsing parameters
« Reply #2 on: June 04, 2020, 09:44:14 am »
Here's my ini-file:

[
Quote
Button1]
Text=Group Policy Management
Command=c:\windows\system32\mmc.exe
Parameter=C:\Windows\system32\gpmc.msc

[Button2]
Text=Wordpad
Command=Write.exe
Parameter=

[Button3]
Text=Group Policy Management
Command=c:\windows\system32\mmc.exe
Parameter=C:\Windows\system32\gpmc.msc

[Button4]
Text=AD
Command=c:\windows\system32\mmc.exe
Parameter=C:\windows\system32\dsa.msc

[Button5]
Text=PowerPoint
Command=powerpnt.exe
Parameter=

[Button6]
Text=Microsoft Word
Command=winword.exe
Parameter=

Don't mind that there are duplicate entries, it's just for test as of now.

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Right click and parsing parameters
« Reply #3 on: June 04, 2020, 10:18:07 am »
Ok, thank you for the information nimmer.

I (still) do not have a good idea on how you want to edit things but from what i was able to understand, i would make the following assumptions/suggestions (please ask in case something is unclear or you are not sure how to implement it)

As you press the right mouse-button on a launcher-button you are able to tell them apart as the sender differs inside your click-handler.

I am assuming that each of your individual launcher-buttons have a distinctive name or other mechanism that links the button with the values in the inifile.

Let's assume you use the name of the (launch-button) component for that, so that he first launch button is named "Button1", the second "Button2" etc.

Inside your click-handler you are able to retrieve the name of the control, and use that name.

You can read a section of a inifile with Ini.ReadSectionValues('Button1', TStrings) where Button1 is the name of the corresponding button in the inifile ad TStrings a list of strings that will contain the name-value pair from the .ini file that belong to that specifc button-section.

You are now able to present those name-value pairs in any which way you wish to, either a memobox or individual edit fields or whatever works for you.

You change the values, then update the inifile by using Ini.WriteString(Section, Ident, Value), where section is the name of the button, Ident the name of the variable (Text, Comand, Parameter) and value the actual contents that is paired to that specific ident (e.g., AD, c:\windows\system32\mmc.exe, C:\windows\system32\dsa.msc)

Perhaps that is able to get you going ? If not just ask, or if you need more specific help post code  ;)

nimmer

  • Newbie
  • Posts: 6
Re: Right click and parsing parameters
« Reply #4 on: June 04, 2020, 12:39:50 pm »
Hi,

I know how to read and write from the ini-files. I read the values into TEdit components in my settings dialogbox onload, and then executes whatever the ini-string says like this:

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.Button1Click(Sender: TObject);
  2. Var
  3.    AppToExecute, Parameter: String;
  4. begin
  5.   AppToExecute := SettingsForm.Btn1Command.Text;
  6.   Parameter := SettingsForm.Btn1Parameter.Text;
  7.   ShellExecute(Handle,'open',Pchar(AppToExecute),PChar(Parameter),nil,SW_SHOWNORMAL);            

My settingsform contains Tedit-components for all the buttons, but I would like to just have one set of Tedit components and then when rightclicking a button it should load the settings for that specific button.

/nimmer

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Right click and parsing parameters
« Reply #5 on: June 04, 2020, 12:54:33 pm »
I know how to read and write from the ini-files. I read the values into TEdit components in my settings dialogbox onload, and then executes whatever the ini-string says like this:
Ok, noted. Don't take my example too serious then (you are able to read through that, i hope  :) ).

Quote
My settingsform contains Tedit-components for all the buttons, but I would like to just have one set of Tedit components and then when rightclicking a button it should load the settings for that specific button.
You can remove all the duplicate visual components from your settings-form and use the Sender parameter from the MouseupUp Event to determine which button was right-clicked.

If you've named your buttons in correspondence with the section names in the ini file, you are able to link them.

An example (i made it a tad different in comparison to your approach)

On clicking a launch-button (note that i was expecting to use TProcess, not shell-execute but as you already know that works in a similar way).
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LaunchButtonClick(Sender: TObject);
  2. var
  3.   ThisCommand : string;
  4.   NameOfButton : string;
  5. begin
  6.   // Launch a program
  7.  
  8.  // Obtain name of button that was clicked
  9.  NameOfButton := (Sender as TButton).Name;
  10.  // Obtain command that needs to be executed
  11.  ThisCommand := LauncherSettings.ReadString(NameOfButton, 'Command', '');
  12.   if ThisCommand <> '' then
  13.   begin
  14.     ShowMessage('Launching Command = ' + ThisCommand)
  15.     // Todo: Check if file exists, if not report to user else prepare TProcess
  16.     // Todo: Check if parameter present, if so then add to parameterlist of TProcess
  17.     // Todo: Launch chosen executable by starting TProcess.
  18.   end
  19.   else ShowMessage('Launching Command Is Empty');
  20. end;
  21.  

The right mouse-click
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LaunchButtonMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  2. var
  3.   NameOfButton : string;
  4.   ThisText : string;
  5.   ThisCommand : string;
  6.   ThisParameter : string;
  7. begin
  8.   if Button = mbRight then
  9.   begin
  10.     // Obtain name of button that was clicked
  11.     NameOfButton := (Sender as TButton).Name;
  12.     // Obtain values from ini-file and store into local variables (for clarity)
  13.     ThisText     := LauncherSettings.ReadString(NameOfButton, 'Text', '');
  14.     ThisCommand  := LauncherSettings.ReadString(NameOfButton, 'Command', '');
  15.     ThisParameter:= LauncherSettings.ReadString(NameOfButton, 'Parameter', '');
  16.     // Store values into corresponding components on 2nd form (clarity, can be done immediate)
  17.     Form2.EditButton1.Text   := ThisText;
  18.     Form2.FileNameEdit1.Text := ThisCommand;
  19.     Form2.EditButton2.Text   := ThisParameter;
  20.     // Show Form2, and wait for it to return a positive response (button OK)
  21.     if Form2.ShowModal = mrOk then
  22.     begin
  23.       // Obtain values from Form2 components and store into local variables (for clarity)
  24.       ThisText      := Form2.EditButton1.Text;
  25.       ThisCommand   := Form2.FileNameEdit1.Text;
  26.       ThisParameter := Form2.EditButton2.Text;
  27.       // Write Local Variables to ini-file (clarity, can be done immediate)
  28.       LauncherSettings.WriteString(NameOfButton, 'Text', ThisText);
  29.       LauncherSettings.WriteString(NameOfButton, 'Command', ThisCommand);
  30.       LauncherSettings.WriteString(NameOfButton, 'Parameter', ThisParameter);
  31.       // Update Form1 button Text
  32.       (Sender as TButton).Caption:= ThisText;
  33.     end
  34.     else ShowMessage('Cancelled settings');
  35.   end;
  36. end;
  37.  

Note that i simply placed some components on my 'settings' form (Form2) and that those doesn't match yours but you can take an identical approach (though clean my mes up a little  ;) ).

nimmer

  • Newbie
  • Posts: 6
Re: Right click and parsing parameters
« Reply #6 on: June 04, 2020, 03:16:20 pm »
Thank you, I will take a look at it when I come home from work.

/nimmer

 

TinyPortal © 2005-2018