首页 > 解决方案 > 如何使用列表框项调用消息框

问题描述

Windows 窗体应用程序

我正在开发一个应用程序,让用户填写左侧的四个字段并单击“添加汽车”按钮,型号、颜色和年份将显示为一个列表框项目,一旦他们选择该列表框项目,一个消息框会出现给用户他们之前在价格字段中写的价格。除了单击列表框项时出现消息框外,我已经完成了所有工作。我不确定为什么它不起作用。

在这里,我已经包含了这个项目的代码。我使用了 Visual Studio 2017,项目类型是 Visual C# Windows Form App 项目,如果有帮助的话:

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 MIS_312_Extra_Challenge_Project
{
    public partial class Form1 : Form
    {
        string model, color, details = "", AllDetails = "", models = "";
        int year;
        double price;

    public Form1()
    {
        InitializeComponent();
    }

    private void btn_addCar_Click(object sender, EventArgs e)
    {
        model = txtModel.Text;
        color = txtColor.Text;
        year = Convert.ToInt32(txtYear.Text);
        price = Convert.ToDouble(txtPrice.Text);

        details = model + " " + color + " " + year + Environment.NewLine;
        AllDetails = AllDetails + price + " " + Environment.NewLine;
        models = models + model + " " + Environment.NewLine;

        list_carDetails.Items.Add(details);
        txtModel.Clear();
        txtColor.Clear();
        txtYear.Clear();
        txtPrice.Clear();
        txtModel.Clear();

    }

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

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void list_carDetails_SelectedIndexChanged_1(object sender, EventArgs e)
    {

    }
    private void list_carDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] all = AllDetails.Split();
        string[] m = models.Split();
        string[] modelName = { };
        int i;
        foreach (var car in list_carDetails.SelectedItems)
        {
            string details = car.ToString();
            modelName = details.Split();
        }

        for(i=0;i<m.Length;i++)
        {
            if (string.Compare(modelName[0],m[i])== 0)
            {
                MessageBox.Show("The price of the " + modelName[0] + " is $" + all[i]);
            }
        }
    }
}
}

标签: c#visual-studiolistbox

解决方案


推荐阅读