【发布时间】:2019-12-16 18:29:55
【问题描述】:
我正在制作一个 Windows 窗体应用程序,它采用各种游戏条目(标题、类型、价格),然后将它们存储在一个最多包含四个条目的数组中。我还有一个删除按钮来覆盖条目
我目前有 3 个错误我不确定是单独询问每个错误还是同时询问所有错误,但我会同时尝试所有错误
1.当使用删除按钮覆盖数组时,它会保持原始价格值,而不是更改为字符串并被列为已售出
2.当我使用游戏 2 的删除按钮删除第二个数组时,它会覆盖第一个数组,但是当被覆盖时,第三个和第四个数组可以正常工作
3.当用户在 tb 中输入小数以外的小数时,价格是小数,整个程序崩溃我该如何输入一个 try catch 语句来解决这个问题
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;
using Microsoft.Win32;
using System.IO;
namespace gameForm
{
public partial class gameEntryForm : Form
{
public gameEntryForm()
{
InitializeComponent();
}
struct Game
{
public string Title;
public string Genre;
public decimal Price;
}
static Game[] aNewGame = new Game[4]; //max size of the array is 4
static int newGameEntryIndex = 1;
//bool full = true;
private void gameEntryForm_Load(object sender, EventArgs e)
{
aNewGame[0].Title = "golf tour"; //this is a game already stored in the database
aNewGame[0].Genre = "sports";
aNewGame[0].Price = 1.99m;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(tbGenre.Text))
{
MessageBox.Show("please enter a Game genre.");
}
else if (String.IsNullOrEmpty(tbTitle.Text))
{
MessageBox.Show("please enter a Game title");
}
else if (String.IsNullOrEmpty(tbPrice.Text))
{
MessageBox.Show("please enter a Game price");
}
else
{
if (newGameEntryIndex >= 4)
{
MessageBox.Show("the game store is full");
}
else
{
aNewGame[newGameEntryIndex] = new Game();
aNewGame[newGameEntryIndex].Title = tbTitle.Text;
aNewGame[newGameEntryIndex].Genre = tbGenre.Text;
aNewGame[newGameEntryIndex].Price = Convert.ToDecimal(tbPrice.Text);
newGameEntryIndex++;
MessageBox.Show("entry saved");
//clears the text boxes
tbTitle.Clear();
tbGenre.Clear();
tbPrice.Clear();
}
}
}
private void btnShow_Click(object sender, EventArgs e)
{
rtbShow.Text = "Game Details \n\nGame 1 \n" + aNewGame[0].Title + "\n" + aNewGame[0].Genre + "\n" + aNewGame[0].Price + "\n\n" + "Game 2 \n" + aNewGame[1].Title + "\n" + aNewGame[1].Genre + "\n" + aNewGame[1].Price + "\n\n" + "Game 3 \n" + aNewGame[2].Title + "\n" + aNewGame[2].Genre + "\n" + aNewGame[2].Price + "\n\n" + "Game 4 \n" + aNewGame[3].Title + "\n" + aNewGame[3].Genre + "\n" + aNewGame[3].Price; ;
}
//clears the rich text box
private void btnClear_Click(object sender, EventArgs e)
{
rtbShow.Clear();
}
private void btnQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (!(cmbDelete.SelectedIndex == 1))
{
aNewGame[cmbDelete.SelectedIndex - 0].Title = "Sold";
aNewGame[cmbDelete.SelectedIndex - 0].Genre = "Sold";
aNewGame[cmbDelete.SelectedIndex - 0].Price.ToString("Sold");
}
else if (!(cmbDelete.SelectedIndex == 2))
{
aNewGame[cmbDelete.SelectedIndex - 1].Title = "Sold";
aNewGame[cmbDelete.SelectedIndex - 1].Genre = "Sold";
aNewGame[cmbDelete.SelectedIndex - 1].Price.ToString("sold");
}
else if (!(cmbDelete.SelectedIndex == 3))
{
aNewGame[cmbDelete.SelectedIndex - 2].Title = "Sold";
aNewGame[cmbDelete.SelectedIndex - 2].Genre = "Sold";
aNewGame[cmbDelete.SelectedIndex - 2].Price.ToString("sold");
}
else if (!(cmbDelete.SelectedIndex == 4))
{
aNewGame[cmbDelete.SelectedIndex - 3].Title = "Sold";
aNewGame[cmbDelete.SelectedIndex - 3].Genre = "Sold";
aNewGame[cmbDelete.SelectedIndex - 3].Price.ToString("sold");
}
}
}
}
【问题讨论】:
-
ComboBox 中有多少条目?为什么要从 SelectedIndex 中减去一个常数? SelectedIndex 应该已经正确,以便将“已售”文本设置在 Game 数组中的正确位置
-
“也许是一个 try catch 语句来解决这个问题” - 不。你掩盖了真正的问题。以首先不需要尝试/捕获的方式处理此问题。您当然能够从异常中获得一些有用的信息,并且希望它应该足够明显。
标签: c# arrays forms user-interface try-catch