Filling a textbox field from C# variable

Here is the final, working code for those interested. If the correct barcode is scanned at the MES Menu main form, then Start Production Activity will launch, the fields will populate, and then close so the employee is clocked in.

The code is triggered by the hotkey “J” which is the first character of the QR code. So the barcode would read “J-JobNumber-AssemblyNumber-OperationSequence-Z”. J triggers the code, and Z tells the code to stop reading the barcode. I had to make a custom menu item in Menu Maintenance for the StartProd form (that’s what SSSM9999 is).

// Launch Start Activity from hot key
DateTime _lastKeystroke = new DateTime(0);
List<char> _barcode = new List<char>(10);	
LaunchFormOptions lfo = new LaunchFormOptions();

public void MESMenu_KeyPress(object sender, KeyPressEventArgs e)
{
	// Check timing (keystrokes within 100 ms)
	TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
	if (elapsed.TotalMilliseconds > 100)
	_barcode.Clear();

	// Record keystroke and timestamp
	_barcode.Add(e.KeyChar);
	_lastKeystroke = DateTime.Now;

	// Process barcode
	if (e.KeyChar == (char)90 && _barcode.Count > 0)
	{
		string msg = new String(_barcode.ToArray());
		lfo.IsModal = true;
		lfo.ContextValue = msg;
		_barcode.Clear();
		ProcessCaller.LaunchForm(this.oTrans, "SSSM9999", lfo);
		this.oTrans.RefreshLaborData();
	}
}

A customization on the StartProd form pulls the barcode in and then assigns each section of the barcode to its proper field. Sections are separated by the character “-”.

// Pull barcode in from MESMenu
private void StartProdForm_Load(object sender, EventArgs e)
{
	if(StartProdForm.LaunchFormOptions != null)
	{
		object barcodeObj = StartProdForm.LaunchFormOptions.ContextValue;
		// Convert barcode (object to string)
		string barcode = barcodeObj.ToString();
		// Split barcode into parts
		var items = barcode.Split('-');
		string job = items[1];
		string asm = items[2];
		string opr = items[3];
		// Transfer data to fields
		var view = ((EpiDataView)(this.oTrans.EpiDataViews["Start"]));
		if(view.Row>=0)
		{
			// Fill Job Number
			view.dataView[view.Row].BeginEdit();
			view.dataView[view.Row]["JobNum"]=job;
			view.dataView[view.Row].EndEdit();
			// Fill Assembly Sequence
			view.dataView[view.Row].BeginEdit();
			view.dataView[view.Row]["AssemblySeq"]=asm;
			view.dataView[view.Row].EndEdit();
			// Fill Operation Sequence
			view.dataView[view.Row].BeginEdit();
			view.dataView[view.Row]["OprSeq"]=opr;
			view.dataView[view.Row].EndEdit();
			// Close form
			if (oTrans.Update())
			{
				StartProdForm.Close();
			}
		}
	}
}

I hope this helps somebody!

7 Likes