C# 尝试在windows窗体中捕获数组
我正在制作一个windows窗体应用程序,它可以获取各种游戏条目(标题、类型、价格),然后将它们存储在最多四个条目的数组中。我还有一个删除按钮来覆盖条目 我目前有3个错误,我不确定我是单独询问每个错误还是同时询问所有错误,但我会同时尝试所有错误 1.当使用“删除”按钮写入数组时,它将保持原始价格值,而不是更改为字符串并列为已售出 2.当我使用删除按钮(即游戏2)删除第二个数组时,它会覆盖第一个数组,但在写入时会覆盖第三个和第四个数组C# 尝试在windows窗体中捕获数组,c#,arrays,forms,user-interface,try-catch,C#,Arrays,Forms,User Interface,Try Catch,我正在制作一个windows窗体应用程序,它可以获取各种游戏条目(标题、类型、价格),然后将它们存储在最多四个条目的数组中。我还有一个删除按钮来覆盖条目 我目前有3个错误,我不确定我是单独询问每个错误还是同时询问所有错误,但我会同时尝试所有错误 1.当使用“删除”按钮写入数组时,它将保持原始价格值,而不是更改为字符串并列为已售出 2.当我使用删除按钮(即游戏2)删除第二个数组时,它会覆盖第一个数组,但在写入时会覆盖第三个和第四个数组 3.当用户在tb for price中输入的不是小数点,而是小
3.当用户在tb for price中输入的不是小数点,而是小数点时,整个程序崩溃。我如何输入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");
}
}
}
}
您在“删除”按钮单击中的逻辑做得太多,这可能导致您的前两个错误 如果他们单击删除第一项,假设SelectedIndex为0,则您应该能够调用:
aNewGame[cmbDelete.SelectedIndex].Title = "Sold";
注意:如果SelectedIndex为1,则单击第一项,然后需要减去一
这将使您的单击事件如下所示:
private void btnDelete_Click(object sender, EventArgs e)
{
aNewGame[cmbDelete.SelectedIndex].Title = "Sold";
aNewGame[cmbDelete.SelectedIndex].Genre = "Sold";
aNewGame[cmbDelete.SelectedIndex].Price.ToString("Sold"); // There's another bug here
}
首先,您可以为
Show
按钮尝试类似的操作。我添加了内联if语句,因为不能将字符串
放入十进制
对象中。因此,我们将修改Delete
按钮的行为
private void btnShow_Click(object sender, EventArgs e)
{
rtbShow.Text = "Game Details \n\n"
for(int i = 0; i < aNewGame.Length; i++)
{
rtbShow.Text += Game " + i +
"\n" + aNewGame[i].Title +
"\n" + aNewGame[i].Genre +
"\n" + aNewGame[i].Price == Decimal.Zero ? "Sold" : aNewGame[i].Price;
}
}
注意,我们更改了最后一行,将价格设置为Decimal.Zero
。这是为了让我们能够正确地展示售出的游戏
此代码未经测试,但它应该为您提供如何修复所遇到的错误的线索
对于错误3,您可以这样执行tryParse
语句:
decimal temp;
aNewGame[newGameEntryIndex].Price =
Decimal.tryParse(tbPrice.Text, out temp)? temp : Decimal.Parse("0.00");
该组合框中有多少个条目?为什么要从SelectedIndex中减去常数?SelectedIndex应该已经正确,可以在游戏数组中的正确位置设置“已售出”文本“可能是一个try-catch语句来修复此问题”-否。你掩盖了真正的问题。处理这个问题时,首先不需要尝试/捕获。您当然可以从异常中获得一些有用的信息,希望它足够明显。对于带有“这里有另一个bug”的行,请尝试找出该行有什么影响。它是做什么的?假设该行将一个十进制数转换为一个字符串来列出它,就像soldIt将十进制数转换为字符串一样。。。但是结果会怎样呢?
decimal temp;
aNewGame[newGameEntryIndex].Price =
Decimal.tryParse(tbPrice.Text, out temp)? temp : Decimal.Parse("0.00");