PascalABC Color Dialog

Can you give an example of use? Outside of GrafABC. It is about Unit creation.

procedure Form1.colorButton_Click(sender: Object; e: EventArgs); begin // what code I need to put here? // I try C# syntax but no result. // I need to change the color of a drawing and in the same time the back color of button. end;

Use markdown syntax to insert code, like this:

```
code
```

Also, elaborate on

Which C# code you wanted to use and how you tried translating it to pascal? Send code of both.

Well, this is the Form and the controls.

And the code:

Unit Unit1;

interface

uses System, System.Drawing, System.Windows.Forms;

type
  Form1 = class(Form)
    procedure pictureBox1_Paint(sender: Object; e: PaintEventArgs);
    
    procedure trackBar1_Scroll(sender: Object; e: EventArgs);
    procedure trackBar2_Scroll(sender: Object; e: EventArgs); 
    procedure numericUpDown1_ValueChanged(sender: Object; e: EventArgs);
    procedure numericUpDown2_ValueChanged(sender: Object; e: EventArgs);
    
    procedure trackBar3_Scroll(sender: Object; e: EventArgs);
    procedure trackBar4_Scroll(sender: Object; e: EventArgs);
    
    procedure numericUpDown3_ValueChanged(sender: Object; e: EventArgs);
    procedure numericUpDown4_ValueChanged(sender: Object; e: EventArgs);
    
    procedure trackBar5_Scroll(sender: Object; e: EventArgs);
    procedure numericUpDown5_ValueChanged(sender: Object; e: EventArgs);
    
    procedure button2_Click(sender: Object; e: EventArgs);
    
    procedure colorButton_Click(sender: Object; e: EventArgs);
    
    procedure resStartX_Click(sender: Object; e: EventArgs);
    procedure resStartY_Click(sender: Object; e: EventArgs);
    procedure resEndX_Click(sender: Object; e: EventArgs);
    procedure resEndY_Click(sender: Object; e: EventArgs);
    procedure resWidth_Click(sender: Object; e: EventArgs);
    procedure resAll_Click(sender: Object; e: EventArgs);
    procedure button1_Click(sender: Object; e: EventArgs);
  {$region FormDesigner}
  private
    {$resource Unit1.Form1.resources}
    button1: Button;
    button2: Button;
    label1: &Label;
    label2: &Label;
    label3: &Label;
    label4: &Label;
    trackBar1: TrackBar;
    trackBar2: TrackBar;
    numericUpDown1: NumericUpDown;
    numericUpDown2: NumericUpDown;
    numericUpDown3: NumericUpDown;
    trackBar3: TrackBar;
    numericUpDown4: NumericUpDown;
    trackBar4: TrackBar;
    numericUpDown5: NumericUpDown;
    trackBar5: TrackBar;
    label5: &Label;
    colorDialog1: ColorDialog;
    colorButton: Button;
    label6: &Label;
    resStartY: Button;
    resEndX: Button;
    resEndY: Button;
    resWidth: Button;
    resStartX: Button;
    resAll: Button;
    label7: &Label;
    pictureBox1: PictureBox;
    {$include Unit1.Form1.inc}
  {$endregion FormDesigner}
  public
    constructor;
    begin
      InitializeComponent;
    end;
  end;

implementation

var
  startx := 50;
  starty := 50;
  endx := 255;
  endy := 255;
  thickness := 5;
  color := Color.Blue;
  
  
procedure Form1.pictureBox1_Paint(sender: Object; e: PaintEventArgs);
begin

  e.Graphics.DrawLine(new Pen(color, thickness), startx, starty, endx, endy);

end;

procedure Form1.trackBar1_Scroll(sender: Object; e: EventArgs);
begin
  numericUpDown1.Value := trackBar1.Value;
  startx := trackBar1.Value;
  pictureBox1.Invalidate();
end;

procedure Form1.trackBar2_Scroll(sender: Object; e: EventArgs);
begin
  numericUpDown2.Value := trackBar2.Value;
  starty := trackBar2.Value;
  pictureBox1.Invalidate();
end;

procedure Form1.numericUpDown1_ValueChanged(sender: Object; e: EventArgs);
begin

end;

procedure Form1.numericUpDown2_ValueChanged(sender: Object; e: EventArgs);
begin

end;


procedure Form1.trackBar3_Scroll(sender: Object; e: EventArgs);
begin
  numericUpDown3.Value := trackBar3.Value;
  endx := trackBar3.Value;
  pictureBox1.Invalidate();
end;

procedure Form1.trackBar4_Scroll(sender: Object; e: EventArgs);
begin
  numericUpDown4.Value := trackBar4.Value;
  endy := trackBar4.Value;
  pictureBox1.Invalidate();
end;

procedure Form1.numericUpDown3_ValueChanged(sender: Object; e: EventArgs);
begin
  
end;

procedure Form1.numericUpDown4_ValueChanged(sender: Object; e: EventArgs);
begin
  
end;

procedure Form1.trackBar5_Scroll(sender: Object; e: EventArgs);
begin
  numericUpDown5.Value := trackBar5.Value;
  thickness := trackBar5.Value;
  pictureBox1.Invalidate();
end;

procedure Form1.numericUpDown5_ValueChanged(sender: Object; e: EventArgs);
begin
  
end;




procedure Form1.button2_Click(sender: Object; e: EventArgs);
begin
  //On OK button click Close app
  Close();
end;

procedure Form1.colorButton_Click(sender: Object; e: EventArgs);
begin
//some code here but not C# syntax
//if(colorDialog1.ShowDialog := Dialog.OK)
//begin
// colorButton.BackColor := colorDialog1.Dialog.Color;
// color := colorDialog1.Dialog.Color;
//end;
end;

procedure Form1.resStartX_Click(sender: Object; e: EventArgs);
begin
  startx := 50;
  numericUpDown1.Value := startx;
  trackBar1.Value := startx;
  pictureBox1.Invalidate();
end;

procedure Form1.resStartY_Click(sender: Object; e: EventArgs);
begin
  starty := 50;
  numericUpDown2.Value := starty;
  trackBar2.Value := starty;
  pictureBox1.Invalidate();
end;

procedure Form1.resEndX_Click(sender: Object; e: EventArgs);
begin
  endx := 255;
  numericUpDown3.Value := endx;
  trackBar3.Value := endx;
  pictureBox1.Invalidate();
end;

procedure Form1.resEndY_Click(sender: Object; e: EventArgs);
begin
  endy := 255;
  numericUpDown4.Value := endy;
  trackBar4.Value := endy;
  pictureBox1.Invalidate();
end;

procedure Form1.resWidth_Click(sender: Object; e: EventArgs);
begin
  thickness := 5;
  numericUpDown5.Value := thickness;
  trackBar5.Value := thickness;
  pictureBox1.Invalidate();
end;

procedure Form1.resAll_Click(sender: Object; e: EventArgs);
begin
  //reset all controls values
  startx := 50;
  numericUpDown1.Value := startx;
  trackBar1.Value := startx;
  
  starty := 50;
  numericUpDown2.Value := starty;
  trackBar2.Value := starty;
  
  endx := 255;
  numericUpDown3.Value := endx;
  trackBar3.Value := endx;
  
  endy := 255;
  numericUpDown4.Value := endy;
  trackBar4.Value := endy;
  
  thickness := 5;
  numericUpDown5.Value := thickness;
  trackBar5.Value := thickness;
  //color := Color.Black; does not work
  pictureBox1.Invalidate();
end;

procedure Form1.button1_Click(sender: Object; e: EventArgs);
begin
  //Cancel
  Close();
end;

end.

:= is assignment, while in C# you probably had ==, which is equality operator.
And where is then keyword?

Other then that - colorButton_Click code looks fine.
Maybe colorDialog1.Color instead of colorDialog1.Dialog.Color, not sure… Try both.

Ah right… Look at C# code you are trying to translate. And preferably - send it here as well.
There is probably something like DialogRes, can’t look up rn.

1 лайк
private void colorButton_Click(object sender, EventArgs e)
        {
            ColorDialog colDlg = new ColorDialog();
            if (colDlg.ShowDialog() == DialogResult.OK)
            {
                colorButton.BackColor = colDlg.Color;
                label_Preview.ForeColor = colDlg.Color;
            }
        }

@Sun_Serega, thank you for your support. Another question is how to set SmoothingMode for drawing to Antialiasing?

Here. Though, it was probably easier and faster to google, then waiting when my net would work again, so i can answer…

Well, I forgot to use

uses System.Drawing.Drawing2D 

The IDE don’t suggest to load a ref. By the way, how to comment / uncomment a block of selected lines in PascalABC IDE?

Ctrl + /

1 лайк

Thanks, I searched all the menus and I didn’t find this command. A button on the toolbar would have been nice.

Another question: When I run my exe file the Command Window appear behind the Form. There is an option to disable it?

Compile the .exe file using the Compile button (not Run button!)

1 лайк

It is working! Thanks @Admin. But I can’t figure out the issue with Color Dialog.

procedure Form1.colorButton_Click(sender: Object; e: EventArgs);
begin
    ColorDialog colorDialog1 := new ColorDialog();
    if (colorDialog1.ShowDialog() = DialogResult.OK) then
    begin
    colorButton.BackColor := colorDialog1.Color;
    color := colorDialog1.Color;            
    end; 
end;

error: Unit1.pas(167) : Found ‘colorDialog1’ but expected ‘;’

Because that’s not how you define variables in pascal.

If you have this C# code:

var i = 5;
int i = 5;

In pascal it would look like this:

var i := 5;
var i: integer := 5;
1 лайк

Ok, I made the change:

var
  startx: integer := 50;
  starty: integer := 50;
  endx: integer := 255;
  endy: integer := 255;
  thickness: integer := 5;
  color: Color := Color.Black;
  colorDialog1: ColorDialog;
var colorDialog1 := new ColorDialog();
1 лайк

Well, this it is worked, but now I have another issue:

if colorDialog1.ShowDialog = DialogResult.OK then

error: Static field ‘OK’ cannot be accessed with an instance reference.

I looked in the documentation, but there is no explanation. I looked in the example programs, in TextEditor.pas but there DialogResult is a Windows.System.Forms.DialogResult enum. In my case it is only property and does not recognize OK enum.

if colorDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK then

Сlass Form has a property “DialogResult”, so priority is given to a property, not a type

1 лайк

You can also create a synonym for the DialogResult:

type
  DResult = DialogResult;
if colorDialog1.ShowDialog = DResult.OK then
1 лайк

Then what is the purpose of: uses System.Windows.Forms ?