I ended up with using Exergist's great program as the framework. Therefore a lot of code is borrowed from that. I created dynamic buttons to avoid having to write out 52 buttons. Below code with adaptions. See link posted in previous thread for original post located in Inline Functions section.
// C# inline function for retrieving user input & preprocessing
Marker: GotoInline
Inline C# Function: Display UI for getting input & process result, wait until execution finishes
// Check if user provided relevant input
Begin Text Compare : [~~UserInput] Has Been Set
Begin Text Compare : [~~isCancel] Equals 'No'
// Output retrieved information
Write [Blue] 'Letter selected is: {TXT:~~UserInput}' to log
Quick Input, '{TXT:~~UserInput}'
Jump to Marker: GotoInline
Else
// Output info to event log
Write [Red] 'Exited Alphabet Form' to log
End Condition
End Condition
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class VAInline
{
[STAThread]
public void main()
{
// Enable (operating system) visual styles for the application
Application.EnableVisualStyles();
// Create an instance of Form1 (while passing the dynamic VA for use by the form) and an associated message loop
Application.Run(new Form1(VA));
}
}
public class Form1 : Form
{
// Initialize string variable for storing user input
public string result = null;
// Create and display Form1
public Form1(dynamic VA)
{
VA.SetText("~~isCancel", "No");
// Set up the form
this.Text = "This is a Windows Form"; // Set the caption bar text of the form
this.FormBorderStyle = FormBorderStyle.FixedDialog; // Define the border style of the form to a dialog box
this.MaximizeBox = false; // Set the MaximizeBox to false to remove the maximize box
this.MinimizeBox = false; // Set the MinimizeBox to false to remove the minimize box
this.StartPosition = FormStartPosition.CenterScreen; // Set the start position of the form to the center of the screen
this.AutoSize = true; // Set form sizing to be automatic
this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // Set the form's autosizing to grow or shrink based on the text
this.MinimumSize = new Size(this.Width, this.Height); // Set the form window to be no smaller than design time size
this.MaximumSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Set the form window to be no larger than screen size
this.Padding = new Padding(10); // Set the padding around the form's border
int FontSize = 10; // Set the font size used for buttons
this.TopMost = true;
int ControlSpacing = 5; // Set the spacing between form controls
this.FormClosing += new FormClosingEventHandler((sender, e) => Form1_FormClosing(sender, e, VA)); // Subscribe to event for the form closing and pass in dynamic VA
TableLayoutPanel Table = new TableLayoutPanel();
Table.Location = new Point(10, 40);
Table.Size = new Size(820,100);
Table.AutoSize = true;
Table.Name = "Alphabet Selector";
Table.ColumnCount = 9;
Table.RowCount = 6;
Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddRows;
this.Controls.Add(Table);
// Declare and assign number of buttons = 52
System.Windows.Forms.Button[] btnArray = new System.Windows.Forms.Button[52];
//Create (52) Buttons:
for (int i = 0; i < 52; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
}
/* *********************************************
Alternative code for button text
char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z' etc}; btnArray[n].Text = Alphabet[n].ToString();
****************************************** */
int n = 0;
int j = 0;
while(n < 52)
{
if (n>25)
{
btnArray[n].Text = ((char)(j + 65)).ToString();
j++;
}
else
{
btnArray[n].Text = ((char)(n + 65)).ToString().ToLower();
}
btnArray[n].AutoSize = true;
btnArray[n].AutoSizeMode = AutoSizeMode.GrowAndShrink;
btnArray[n].Font = new Font(btnArray[n].Font.FontFamily, FontSize);
Table.Controls.Add(btnArray[n]);
btnArray[n].Click += new EventHandler((sender, e) => ClickButton(sender, e, VA));
n++;
}
//Set up Exit/Cancel Button
Button Exit = new Button();
Exit.Text = "Exit";
Exit.Size = new Size(26, 27);
Exit.Font = new Font(Exit.Font.FontFamily, 6);
Table.Controls.Add(Exit);
Exit.Click += new EventHandler((sender, e) => Exit_Click(sender, e, VA));
this.CancelButton = Exit;
// Call function for resizing the form
Functions.ResizeForm(this, ControlSpacing);
// Call function for bringing form into focus
Functions.ShowForm(this);
}
// Result of (Click Button) event, get the text of button
public void ClickButton(Object sender, System.EventArgs e, dynamic VA)
{
Button btn = (Button) sender;
VA.SetText("~~UserInput", btn.Text);
//debug
//MessageBox.Show("You clicked character [" + btn.Text + "]");
result = "Yes";
this.Close(); // Close the form
}
//Function Exit button is clicked
private void Exit_Click(object sender, EventArgs e, dynamic VA)
{
result = null;
this.Close(); // Close the form
}
// Function run when the form is closing
private void Form1_FormClosing(object sender, FormClosingEventArgs e, dynamic VA)
{
// Do stuff
if (result != null) // Check if result string contains data
VA.SetText("~~isCancel", "No");
else
VA.SetText("~~isCancel", "Yes");
}
}
public class Functions
{
// Function for resizing the form and positioning all the controls based on their width
public static void ResizeForm(Form f, int ControlSpacing)
{
// Determine the form control with the maximum width, store this width, and set position of this control
int MaxWidth = 0; // Initialize integer variable for storing the MaxWidth
Control MaxWidthControl = null; // Initialize variable for capturing the widest form control
foreach (Control c in f.Controls) // Loop through each control on the form
{
if (c.Size.Width > MaxWidth) // Check if the current control's width is greater than MaxWidth
{
MaxWidthControl = c; // Set MaxWidthControl to current control
MaxWidth = c.Size.Width; // Redefine MaxWidth using current control's width
}
}
MaxWidthControl.Location = new Point(f.Padding.Left, 0); // Set the location of the MaxWidthControl on the form
// Set location of all other controls within the form
Control PreviousControl = null; // Initialize variable for capturing the previous form control
foreach (Control c in f.Controls) // Loop through each control on the form
{
c.Location = new Point(MaxWidth / 2 + f.Padding.Left - c.Size.Width / 2, (PreviousControl == null ? ControlSpacing : PreviousControl.Height + PreviousControl.Top + ControlSpacing)); // Set location of control c
PreviousControl = c; // Store current control as PreviousControl for future processing
}
}
// Function for brining form into focus
public static void ShowForm(Form f)
{
// Retrieve the window handle for the form and give the form focus
IntPtr myHandle = f.Handle;
SetForegroundWindow(myHandle.ToInt32());
}
// External function for bringing window into focus using its handle
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
}
It does exactly the same thing as the previous example without using inline functions, but with buttons!