-1

我试图从从数据库创建的下拉列表中选择一个名称。我似乎无法分配鬃毛。

当我单击选择按钮时,它不会将 comboBox1 中的人员姓名传递给我下一批代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;

namespace GlidingLogsGUI2
{
public partial class InstructorSelectionForm : Form
{
    List<Person> instructors = new List<Person>();
    Person selectedPerson;

    public InstructorSelectionForm()
    {
        InitializeComponent();
        this.TopMost = true;

        List<Person> instructors = new List<Person>();
        OleDbCommand com = new OleDbCommand("SELECT * FROM Personnel WHERE [Current?] = TRUE AND [Position] = ?", Program.DB_CONNECTION);
        com.Parameters.Add(new OleDbParameter("", PositionIDs.INSTRUCTOR));

        OleDbDataReader dr = com.ExecuteReader();

        while (dr.Read())
        {
            try
            {
                instructors.Add(new Person(dr));
            }
            catch
            {
            }
        }

        foreach (Person a in instructors)
        comboBox1.Items.Add(a.FirstName + " " + a.Surname);
    }

    private void Selectbutton_Click_1(object sender, EventArgs e)
    {
        if (selectedPerson == null)
        {
            MessageBox.Show("A person must be selected first");
            return;
        }

        new F5363(selectedPerson);
    }

    private void CancelButton_Click_1(object sender, EventArgs e)
    {
        this.Close();

    }

    private void InstructorSelectForm_Load(object sender, EventArgs e)
    {

    }


    }
}
4

1 回答 1

3

您正在声明 selectedPerson,但从不在任何地方设置它。我怀疑你想要做的是删除 selectedPerson 的当前声明并将点击事件更改为类似的东西;

private void Selectbutton_Click_1(object sender, EventArgs e)
{  
    Person selectedPerson = comboBox1.SelectedItem as Person;
    if (selectedPerson == null)
    {
...
于 2013-05-19T16:39:49.873 回答