c# - 如何使用列表框项调用消息框
问题描述
我正在开发一个应用程序,让用户填写左侧的四个字段并单击“添加汽车”按钮,型号、颜色和年份将显示为一个列表框项目,一旦他们选择该列表框项目,一个消息框会出现给用户他们之前在价格字段中写的价格。除了单击列表框项时出现消息框外,我已经完成了所有工作。我不确定为什么它不起作用。
在这里,我已经包含了这个项目的代码。我使用了 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]);
}
}
}
}
}
解决方案
推荐阅读
- xamarin - 导航页面标题
- c# - 如何在实体框架中的计算列上创建非聚集索引?
- php - 创建元素组合
- python - 如何在 Python 中求解非线性方程组?
- c# - 如何在一个信号rest api“include_player_ids”参数中包含字符串列表或字符串数组
- c# - 组合框 selectedvalue 并覆盖 ToString() - MVVM WPF
- c# - 合并(联合)来自多个来源的数据
- ios - iOS Objective-C VoiceOver Updating Available Accessible Elements
- php - php 脚本托管在虚拟服务器上,被删除、重命名或损坏
- sql - 映射主键和外键