SelectIndexChanged不起作用(C#desktop)

时间:2017-07-06 13:28:32

标签: c# user-interface listbox

** autopostback - 修复不起作用! **

我的GUI看起来像:

enter image description here

我希望在点击时显示猫的详细信息。

我使用了这段代码:

private void listBox_Cats_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // Get the currently selected item in the ListBox.
            Cat cat = listBox_Cats.SelectedItem as Cat;
            if (cat != null)
            {
                tb_ID.Text = cat.ID.ToString();
                tb_Name.Text = cat.Name.ToString();
                tb_DateArrived.Text = cat.DateArrived.ToString();
                tb_IsAdopted.Text = cat.IsAdopted.ToString();
                tb_adoptedBy.Text = cat.AdoptedBy.ToString();
            }
        }

然而,当跑步时,似乎没有任何效果。我究竟做错了什么?

修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;

using IntegrationTool;

namespace IntegrationTool
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=CatShelter;Integrated Security=true");
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[cats]", conn);
            SqlDataReader reader = cmd.ExecuteReader();

            List<Cat> cats = new List<Cat>();


            while (reader.Read())
            {
                Cat c = new Cat();
                c.ID = (int)reader["CatID"];
                c.Name = (string)reader["Name"];
                c.DateArrived = (DateTime)reader["DateArrived"];
                c.IsAdopted = (string)reader["IsAdopted"];
                c.AdoptedBy = (string)reader["AdoptedBy"];

                cats.Add(c);
            }

            foreach (Cat c in cats)
            {
                Console.WriteLine(c.Name);
            }

            reader.Close();
            conn.Close();

            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }

            listBox_Cats.DataSource = cats;
            listBox_Cats.DisplayMember = "Name";
            listBox_Cats.ValueMember = "ID";

        }

        private void listBox_Cats_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // Get the currently selected item in the ListBox.
            Cat cat = listBox_Cats.SelectedItem as Cat;
            if (cat != null)
            {
                Console.WriteLine(cat.Name);
                tb_ID.Text = cat.ID.ToString();
                tb_Name.Text = cat.Name.ToString();
                tb_DateArrived.Text = cat.DateArrived.ToString();
                tb_IsAdopted.Text = cat.IsAdopted.ToString();
                tb_adoptedBy.Text = cat.AdoptedBy.ToString();
            }
        }
   }
}

namespace IntegrationTool

{

  partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.listBox_Cats = new System.Windows.Forms.ListBox();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.button2 = new System.Windows.Forms.Button();
        this.cb_VetCheck = new System.Windows.Forms.CheckBox();
        this.label5 = new System.Windows.Forms.Label();
        this.tb_Comment = new System.Windows.Forms.TextBox();
        this.tb_ID = new System.Windows.Forms.TextBox();
        this.tb_DateArrived = new System.Windows.Forms.TextBox();
        this.tb_IsAdopted = new System.Windows.Forms.TextBox();
        this.tb_adoptedBy = new System.Windows.Forms.TextBox();
        this.groupBox3 = new System.Windows.Forms.GroupBox();
        this.label6 = new System.Windows.Forms.Label();
        this.tb_Name = new System.Windows.Forms.TextBox();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.groupBox3.SuspendLayout();
        this.SuspendLayout();
        // 
        // listBox_Cats
        // 
        this.listBox_Cats.FormattingEnabled = true;
        this.listBox_Cats.Location = new System.Drawing.Point(16, 35);
        this.listBox_Cats.Name = "listBox_Cats";
        this.listBox_Cats.Size = new System.Drawing.Size(137, 316);
        this.listBox_Cats.TabIndex = 0;
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.tb_Name);
        this.groupBox1.Controls.Add(this.label6);
        this.groupBox1.Controls.Add(this.tb_adoptedBy);
        this.groupBox1.Controls.Add(this.tb_IsAdopted);
        this.groupBox1.Controls.Add(this.tb_DateArrived);
        this.groupBox1.Controls.Add(this.tb_ID);
        this.groupBox1.Controls.Add(this.label4);
        this.groupBox1.Controls.Add(this.label3);
        this.groupBox1.Controls.Add(this.label2);
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.Location = new System.Drawing.Point(252, 31);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(409, 190);
        this.groupBox1.TabIndex = 1;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "Details";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(6, 35);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(24, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "ID: ";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(6, 95);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(69, 13);
        this.label2.TabIndex = 1;
        this.label2.Text = "Date Arrived:";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(6, 123);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(60, 13);
        this.label3.TabIndex = 2;
        this.label3.Text = "Is adopted:";
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(6, 157);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(65, 13);
        this.label4.TabIndex = 3;
        this.label4.Text = "Adopted By:";
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.tb_Comment);
        this.groupBox2.Controls.Add(this.label5);
        this.groupBox2.Controls.Add(this.cb_VetCheck);
        this.groupBox2.Location = new System.Drawing.Point(252, 243);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(409, 157);
        this.groupBox2.TabIndex = 2;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "Validate";
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(553, 424);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(107, 31);
        this.button2.TabIndex = 4;
        this.button2.Text = "Save";
        this.button2.UseVisualStyleBackColor = true;
        // 
        // cb_VetCheck
        // 
        this.cb_VetCheck.AutoSize = true;
        this.cb_VetCheck.Location = new System.Drawing.Point(9, 30);
        this.cb_VetCheck.Name = "cb_VetCheck";
        this.cb_VetCheck.Size = new System.Drawing.Size(76, 17);
        this.cb_VetCheck.TabIndex = 0;
        this.cb_VetCheck.Text = "Vet Check";
        this.cb_VetCheck.UseVisualStyleBackColor = true;
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(6, 75);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(51, 13);
        this.label5.TabIndex = 1;
        this.label5.Text = "Comment";
        // 
        // tb_Comment
        // 
        this.tb_Comment.Location = new System.Drawing.Point(9, 91);
        this.tb_Comment.Name = "tb_Comment";
        this.tb_Comment.Size = new System.Drawing.Size(288, 20);
        this.tb_Comment.TabIndex = 2;
        // 
        // tb_ID
        // 
        this.tb_ID.Location = new System.Drawing.Point(81, 35);
        this.tb_ID.Name = "tb_ID";
        this.tb_ID.ReadOnly = true;
        this.tb_ID.Size = new System.Drawing.Size(66, 20);
        this.tb_ID.TabIndex = 4;
        // 
        // tb_DateArrived
        // 
        this.tb_DateArrived.Location = new System.Drawing.Point(81, 92);
        this.tb_DateArrived.Name = "tb_DateArrived";
        this.tb_DateArrived.ReadOnly = true;
        this.tb_DateArrived.Size = new System.Drawing.Size(66, 20);
        this.tb_DateArrived.TabIndex = 5;
        // 
        // tb_IsAdopted
        // 
        this.tb_IsAdopted.Location = new System.Drawing.Point(81, 120);
        this.tb_IsAdopted.Name = "tb_IsAdopted";
        this.tb_IsAdopted.ReadOnly = true;
        this.tb_IsAdopted.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
        this.tb_IsAdopted.Size = new System.Drawing.Size(66, 20);
        this.tb_IsAdopted.TabIndex = 6;
        // 
        // tb_adoptedBy
        // 
        this.tb_adoptedBy.Location = new System.Drawing.Point(81, 154);
        this.tb_adoptedBy.Name = "tb_adoptedBy";
        this.tb_adoptedBy.ReadOnly = true;
        this.tb_adoptedBy.Size = new System.Drawing.Size(66, 20);
        this.tb_adoptedBy.TabIndex = 7;
        // 
        // groupBox3
        // 
        this.groupBox3.Controls.Add(this.listBox_Cats);
        this.groupBox3.Location = new System.Drawing.Point(12, 31);
        this.groupBox3.Name = "groupBox3";
        this.groupBox3.Size = new System.Drawing.Size(175, 369);
        this.groupBox3.TabIndex = 5;
        this.groupBox3.TabStop = false;
        this.groupBox3.Text = "Cats";
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(7, 61);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(41, 13);
        this.label6.TabIndex = 8;
        this.label6.Text = "Name: ";
        // 
        // tb_Name
        // 
        this.tb_Name.Location = new System.Drawing.Point(81, 61);
        this.tb_Name.Name = "tb_Name";
        this.tb_Name.ReadOnly = true;
        this.tb_Name.Size = new System.Drawing.Size(66, 20);
        this.tb_Name.TabIndex = 9;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(721, 480);
        this.Controls.Add(this.groupBox3);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        this.groupBox3.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ListBox listBox_Cats;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox tb_adoptedBy;
    private System.Windows.Forms.TextBox tb_IsAdopted;
    private System.Windows.Forms.TextBox tb_DateArrived;
    private System.Windows.Forms.TextBox tb_ID;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.TextBox tb_Comment;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.CheckBox cb_VetCheck;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.TextBox tb_Name;
    private System.Windows.Forms.Label label6;
}

}

2 个答案:

答案 0 :(得分:1)

您的listBox_Cats_SelectedIndexChanged未绑定到listBox的SelectedIndexChanged事件。

您的设计器文件中应该包含以下内容:

this.listBox_Cats.SelectedIndexChanged += new System.EventHandler(this.listBox_Cats_SelectedIndexChanged);

您可以手动添加它(如果它不在那里) - InitializeComponent

答案 1 :(得分:0)

您的列表框(listBox_Cats)将Cat作为复杂类型,因此请尝试通过以下代码获取所选项目并查看其是否有效。

Cat value = (Cat) this.listBox_Cats.SelectedItem;