Hi,
I'm trying to master databinding but I feel like I'm doing it all
wrong. I have a combo which allows me to select a test. I also have
the ability to add a test which should result in its addition to the
combobox.
Here is my code with the three important functions. The first is when
the user creates a new Test. The second updates the combo and is also
called to init it when the program starts. The last function is
triggered when the combobox value is changed. The code works, but it
feels like I'm overcomplicating things...
Thanks for your help,
Aine
// Add a new Test to the Test Round
private void buttonCreateTest_Click(object sender, EventArgs e)
{
// don't add a test that already exists
if (!
system.CurrentTestRound.TestExists(textBoxComplete Test.Text))
{
system.CurrentTest =
system.CurrentTestRound.AddTest(textBoxCompleteTes t.Text);
UpdateTestGroupBox();
}
}
void UpdateTestGroupBox()
{
List<TestboundTests = new List<Test>
(system.CurrentTestRound.Tests);
// databind the test control to the test data for this
test round
comboBoxTest.DataSource = boundTests;
comboBoxTest.ValueMember = "testName";
CurrencyManager currencyManagerTest;
currencyManagerTest =
(CurrencyManager)this.BindingContext[boundTests, null];
// if there are tests for this test round
if (system.CurrentTest.TestName != "")
{
comboBoxTest.SelectedIndex =
comboBoxTest.FindStringExact(system.CurrentTest.Te stName);
currencyManagerTest.PositionChanged += new
EventHandler (currencyManagerTest_PositionChanged);
comboBoxTest.Enabled = true;
}
else
{
comboBoxTest.Enabled = false;
comboBoxTest.Text = "";
}
// Update variables
UpdateTestDepentantVariables();
}
// triggered when the test
public void currencyManagerTest_PositionChanged(object sender,
System.EventArgs e)
{
system.CurrentTest = ((Test)((CurrencyManager)
(sender)).Current);
// Update variables
UpdateTestDepentantVariables();
}