Coded Form

This guidance is based on our RemObjects C# page entitled Getting Started with Forms using RemObjects C# for .Net (without a Designer).

When using a form designer, the form code is split between partial classes in different files. For simple form-based applications you may prefer to code it all for yourself. This page should help you to get started. It shows you how to create a form and add to it a label, text box, button, numeric up down (spinner), and data grid view. The three coded events show the syntax without performing useful functions.

To try this example, start a new Oxygene for .Net console application named OxygeneCodedForm and paste in the code below to replace the default code. Add System.Windows.Forms and System.Drawing to the references. If you do not want the console window to appear, select menu item Projects > OxygeneCodedForm Properties and change the output to Windows Application:

Changing the output type

Changing the output type

The created window, after some user input, appears as follows.

Coded Form

Coded Form

Oxygene code

namespace OxygeneCodedForm;

interface

uses 
  System.Windows.Forms, System.Drawing; //Add these to the references.

type
  CodedForm = static class 
  private 
    f : Form ;
    lbl: Label;
    btn: Button;
    tb: TextBox;
    cbo: ComboBox;
    nud: NumericUpDown;
    dgv: DataGridView;
    Col1, Col2, Col3: DataGridViewTextBoxColumn;
    presses: Integer := 0;
  public
    method btn_Click(sender: Object; e: EventArgs);
    method cbo_SelectedIndexChanged(sender: Object; e :EventArgs);
    method nud_ValueChanged(sender: Object; e :EventArgs);
    method Main(args: array of String);
  end;
    
implementation

method CodedForm.btn_Click(sender: Object; e :EventArgs);
begin
  inc(presses);
  tb.Text :="Click no " + presses.ToString();
end;

method CodedForm.cbo_SelectedIndexChanged(sender: Object; e :EventArgs);
begin
  var selected: String := String(cbo.SelectedItem);
  lbl.Text := selected;
end;

method CodedForm.nud_ValueChanged(sender: Object; e :EventArgs);
begin
  nud.Width := Integer (nud.Value);
end;

method CodedForm.Main(args: array of String);
begin
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  //Form
  f := new Form();
  f.Text := "Simple Coded Form";
  f.SetBounds(400, 200, 330, 210);
  //Label
  lbl := new Label();
  lbl.Location := new Point(100, 43);
  lbl.Text := "";
  f.Controls.Add(lbl);
  //Button
  btn := new Button();
  btn.SetBounds(12, 12, 75, 25);
  btn.Text := "Execute";
  btn.Click += new EventHandler(btn_Click);
  f.Controls.Add(btn);
  //Text box
  tb := new TextBox();
  tb.SetBounds(12, 43, 75, 20);
  tb.Text := "Initial";
  f.Controls.Add(tb);
  //Combo box
  cbo := new ComboBox();
  cbo.SetBounds(100, 12, 100, 100);
  var places: array of String  := ["First", "Second", "Third"];
  cbo.Items.AddRange(places);
  cbo.SelectedIndex := 0;
  cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
  f.Controls.Add(cbo);
  //NumericUpDown (Spinner)
  nud := new NumericUpDown();
  nud.SetBounds(12, 80, 50, 20);
  nud.Minimum := 40;
  nud.Maximum := 60;
  nud.Value := 50;
  nud.ValueChanged += nud_ValueChanged;      
  f.Controls.Add(nud);
  //DataGridView
  dgv := new DataGridView();
  dgv.Location := new Point(100, 80);
  Col1 := new DataGridViewTextBoxColumn();
  Col1.HeaderText := "Gold";
  Col1.Width := 50;
  Col2 := new DataGridViewTextBoxColumn();
  Col2.HeaderText := "Silver";
  Col2.Width := 50;
  Col3 := new DataGridViewTextBoxColumn();
  Col3.HeaderText := "Bronze";
  Col3.Width := 50;
  dgv.Columns.Add(Col1);
  dgv.Columns.Add(Col2);
  dgv.Columns.Add(Col3); 
  dgv.Rows.Add(places);
  dgv.Rows.Add(1000", "£500", "£300");
  dgv.AllowUserToAddRows := false;
  dgv.Height := dgv.Rows.GetRowsHeight(DataGridViewElementStates.None) + dgv.ColumnHeadersHeight + 2;
  dgv.Width := dgv.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dgv.RowHeadersWidth + 2;
  f.Controls.Add(dgv);

  Application.Run(f);
end;

end.

Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal