I have two forms, "FrmRunEntry" and "FrmPartNumEntry". When I enter a value on the FrmRunEntry form, it displays the FrmPartNumEntry from and a combobox. After selecting a value in the combobox, I want to press the ENTER key and carry the selected value from the combobox back to a textbox on the FrmRunEntry form. But I cant get it to work. My combobox and form Keydown events never get triggered. My program just sits on the combobox and does nothing after I press ENTER. I've search the forum extensively and have tried the following solutions without success:
How to pass value from one form into another's combobox
How to get selected items of Combobox from one form to another form in C#
I've also tried a few other solutions that didn't work. I'm a new C# programmer and I admit I don't have a deep understanding of how C# events work. I'm hoping someone can assist in solving this problem and help me understand what I'm doing wrong. Here's the code I'm using:
FORM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HydroProgram
{
public partial class FrmRunEntry : Form
{
public string selectedPartNumber = "";
public FrmRunEntry()
{
InitializeComponent();
this.ActiveControl = TxtHydro;
TxtHydro.Focus();
}
private void FrmRunEntry_Load(object sender, EventArgs e)
{
//Text Boxes
TxtHydro.CharacterCasing = CharacterCasing.Upper;
if (!string.IsNullOrEmpty(selectedPartNumber))
{
TxtPartNum.Text = selectedPartNumber;
}
}
private void TxtHydro_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Hide();
FrmPartNumEntry f = new FrmPartNumEntry();
f.ShowDialog();
}
}
}
}
FORM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HydroProgram
{
public partial class FrmPartNumEntry : Form
{
public FrmPartNumEntry()
{
InitializeComponent();
this.ActiveControl = CboPartNum;
}
private void FrmPartNumEntry_Load(object sender, EventArgs e)
{
//Combo Box
CboPartNum.Location = new Point(668, 240);
CboPartNum.Size = new Size(255, 23);
CboPartNum.Focus();
CboPartNum.SelectedIndex = 1;
}
private void CboPartNum_KeyDown(object sender, KeyEventArgs e) <-- NOT BEING TRIGGERED
{
processRequest(e);
}
private void FrmPartNumEntry_KeyDown(object sender, KeyEventArgs e) <-- NOT BEING TRIGGERED
{
processRequest(e);
}
private void processRequest(KeyEventArgs e) <-- NEVER REACHED
{
if (e.KeyCode == Keys.Enter && this.ActiveControl == CboPartNum)
{
this.Hide();
FrmRunEntry f = new FrmRunEntry();
f.selectedPartNumber = Convert.ToString(CboPartNum.SelectedItem);
f.ShowDialog();
}
}
}
}