Getting Started with Forms using RemObjects C# for .Net (without a Designer)

(You can compare this version with the Oxygene equivalent).

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 .Net console application named CodedForm and paste in the code below to replace the default code. If you do not want the console window to appear, select menu item Projects > CodedForm Properties and change the output to Windows Application:

Output Type

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

Output

The C# Code

using System.Windows.Forms; using System.Drawing; namespace CodedForm { static class Program { private Form f; private Label lbl; private Button btn; private TextBox tb; private ComboBox cbo; private NumericUpDown nud; private DataGridView dgv; private DataGridViewTextBoxColumn Col1; private DataGridViewTextBoxColumn Col2; private DataGridViewTextBoxColumn Col3; private Integer presses = 0; public void btn_Click(Object sender, EventArgs e) { inc(presses); tb.Text ="Click no " + presses.ToString(); } public void cbo_SelectedIndexChanged(Object sender, EventArgs e) { string selected = (string)cbo.SelectedItem; lbl.Text = selected; } public void nud_ValueChanged(object sender, EventArgs e) { nud.Width = (Integer) nud.Value; } [STAThread] public void Main(string[] args) { 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); string[] places = new 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); } } }