问题描述
嘿,我想我会发布我的代码,并希望在这里得到一些帮助:我遇到麻烦的特定部分是当我尝试将数据重新投射到我的结构实例中时,在此处向用户显示信息:
私有 void Listbox_SelectedIndexChanged( object 发件人, EventArgs e)
{
如果 (lstTransactions.SelectedIndex!= -1)
{
//此带下划线的代码不会显示波浪线引起的任何错误,并且在我尝试单击列表框中的一项之前,程序可以正常编译.我抛出一个异常,说指定的转换无效."任何帮助都将是很棒的,这是一个正在进行的工作,因此仍然需要编写一些代码来完成该项目,但是我需要克服这个障碍.
交易 thisTransaction =( 交易 )lstTransactions.SelectedItem;
txtPayee.Text = thisTransaction.payee;
txtCheck.Text = thisTransaction.checkNumber;
txtAmount.Text = thisTransaction.Amount.ToString();
txtDate.Text = thisTransaction.Date.ToString();
switch (thisTransaction.Type)
{
案例 "存款" :
b rbDeposit.Checked = true ;   ;
break ;
案例 "提款" :
b rbWithdrawal.Checked = true ;   ;
break ;
案例 "服务 费用" :
b rbServiceFee.Checked = true ;   ;
break ;
}
}
}
使用系统; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Linq; 使用System.Text; 使用System.Windows.Forms; 命名空间Account_Balance { 公共局部类BalanceForm:Form { 公共BalanceForm() { InitializeComponent(); } //创建一个类来引用不同的交易类型 枚举transactionTypes {存款,提款,serviceFee} //初始化表单级变量 私有小数currentBalance = 0m,clearedBalance = 0m; int currentIndex = 1; 交易[]记录=新交易[21]; //创建一个变量来存储交易类型. transactionTypes transType; 结构交易 { 公共DateTime日期; 公开字符串收款人; 公共小数金额; 公共字符串类型; 公共字符串checkNumber; 公共字符串ToString(DateTime日期,字符串类型,十进制数量) { 如果(类型!=存款") 返回date.ToString("d").PadRight(13)+ type.PadRight(21)+(金额* -1).ToString("C"); 返回date.ToString("d").PadRight(13)+ type.PadRight(22)+ amount.ToString("C"); } } //显示标题信息. 私人void DisplayHeader() { lstTransactions.Items.Add(日期" .PadRight(13)+交易类型" .PadRight(22)+金额"); } 私人void DisplayBalance() { lblCurrent.Text = currentBalance.ToString("C"); lblCleared.Text = clearedBalance.ToString("C"); } 私人无效Listbox_SelectedIndexChanged(对象发送者,EventArgs e) { 如果(lstTransactions.SelectedIndex!= -1) { 交易thisTransaction =(Transaction)lstTransactions.SelectedItem; txtPayee.Text = thisTransaction.payee; txtCheck.Text = thisTransaction.checkNumber; txtAmount.Text = thisTransaction.Amount.ToString(); txtDate.Text = thisTransaction.Date.ToString(); 开关(thisTransaction.Type) { 案例存款": rbDeposit.Checked = true; 休息; 案例提款": rbWithdrawal.Checked = true; 休息; 案例服务费": rbServiceFee.Checked = true; 休息; } } } //如果类型为提款,请验证可用资金 私人布尔isValidTransaction(小数,transactionTypes类型) { 如果(类型== transactionTypes.withdrawal) { 如果(金额> currentBalance) 返回false; } 返回true; } //加载表单时,使用适当的交易类型标记单选按钮 私有void BalanceForm_Load(对象发送者,EventArgs e) { rbDeposit.Tag = transactionTypes.deposit; rbServiceFee.Tag = transactionTypes.serviceFee; rbWithdrawal.Tag = transactionTypes.withdrawal; ClearData(); DisplayHeader(); } 私有字符串SetType() { 字符串类型="; 如果(rbDeposit.Checked) 类型=存款"; 否则,如果(rbServiceFee.Checked) 类型=服务费"; 别的 类型=提款"; 返回类型; } //根据选中的单选按钮更改交易类型. 私有void RadioButtons_CheckedChanged(object sender,EventArgs e) { RadioButton rb =(RadioButton)sender; 如果(rb.Checked) transType =(transactionTypes)rb.Tag; } //确认我们有一个有效日期,如果有效则返回实际日期,如果无效则返回当前日期前一天的日期以表示无效日期. 私有DateTime isValidDate(字符串输入) { DateTime值; 如果(DateTime.TryParse(entry,out value)) { 如果(值> DateTime.Today) 值= DateTime.Today.AddDays(1); } 别的 值= DateTime.Today.AddDays(1); 返回值; } //确认我们有一个有效的金额,如果有效则返回实际金额,如果无效则将金额发送回-1表示无效金额. 私有十进制isValidAmount(字符串输入) { 十进制值; 如果(decimal.TryParse(entry,out value)) { 如果(值< = 0) 值= -1; } 别的 值= -1; 返回值; } //确认有效的输入,如果全部有效,则返回金额.如果有任何错误,则显示一条消息,通知用户错误 私有十进制isValidEntries() { 字符串消息="; 十进制数量 布尔答案= true; 如果(isValidDate(txtDate.Text)== DateTime.Today.AddDays(1)) { 答案=错误; message =请输入今天或之前的有效日期"; } amountEntry = isValidAmount(txtAmount.Text); 如果(amountEntry == -1) { 答案=错误; 消息+ ="\ n" +金额必须是大于零的数字". } 如果(rbWithdrawal.Checked) { 如果(txtPayee.Text ==") { 答案=错误; 消息+ ="\ n" +必须输入收款人才能提款"; } } 如果(答案) { 如果(!isValidTransaction(amountEntry,transType)) { 答案=错误; 消息=资金不足以完成该交易."; } errorProvider1.Clear(); } 如果(!answer) { amountEntry = -1; errorProvider1.SetError(btnCalculate,message); } 返回金额 } //所有条目此时都有效,确定交易类型并据此进行计算. 私有void CalculateBalance(十进制数量) { 如果(transType!= transactionTypes.deposit) 数量* = -1; currentBalance + =金额; 如果(chkCleared.Checked) clearedBalance + =金额; } 私有void LoadArray() { record [currentIndex] .Date = isValidDate(txtDate.Text); record [currentIndex] .Amount = isValidAmount(txtAmount.Text); record [currentIndex] .checkNumber = txtCheck.Text; record [currentIndex] .payee = txtPayee.Text; record [currentIndex] .Type = SetType(); currentIndex ++; } //使用用户的新余额更新已清除标签和当前标签.在用户进行交易时生成交易列表, //跟踪日期,金额,类型以及是否处理了交易. 私有void DisplayData() { lstTransactions.Items.Add(record [currentIndex] .ToString(isValidDate(txtDate.Text),SetType(),isValidAmount(txtAmount.Text)))); DisplayBalance(); } 私有void ClearData() { //清除所有文本框的另一个事务条目并重置焦点. txtDate.Clear(); txtPayee.Clear(); txtCheck.Clear(); txtAmount.Clear(); txtAmount.Focus(); rbWithdrawal.Checked = true; chkCleared.Checked = false; } 私人无效btnReset_Click(对象发送者,EventArgs e) { //将当前余额重置为零,并显示给用户. currentBalance = 0m; clearedBalance = 0m; DisplayBalance(); lstTransactions.Items.Clear(); DisplayHeader(); ClearData(); } 私有无效btnClear_Click(对象发送者,EventArgs e) { //清除所有文本框的另一个事务条目并重置焦点. ClearData(); } //调用必要的过程以验证所有条目,设置错误消息(如果需要),计算余额并显示余额. 私有无效btnCalculate_Click(对象发送者,EventArgs e) { 十进制数= isValidEntries(); 如果(金额!= -1) { CalculateBalance(金额); LoadArray(); DisplayData(); } } 私有void exitToolStripMenuItem1_Click(对象发送者,EventArgs e) { //关闭表单的此实例. this.Close(); } } }
Hey all so I figured I would post up my code and hopefully get some help here: The specific part I am having trouble with is when I try to cast the data back into an instance of my structure in order to display the information to the user here:
privatevoidListbox_SelectedIndexChanged(objectsender,EventArgse)
{
if(lstTransactions.SelectedIndex != -1)
{
//This underlined piece of code does not show any errors with the squiggly lines and the program compiles fine until I try to click on one of the entries in the listbox.I get an exception thrown saying "Specified cast is not valid." Any help would be wonderful, this is a work in progress so there is code that I still need to write to finish this project but I need to get past this road block.
TransactionthisTransaction = (Transaction)lstTransactions.SelectedItem;
txtPayee.Text = thisTransaction.payee;
txtCheck.Text = thisTransaction.checkNumber;
txtAmount.Text = thisTransaction.Amount.ToString();
txtDate.Text = thisTransaction.Date.ToString();
switch(thisTransaction.Type)
{
case"Deposit":
rbDeposit.Checked =true;
break;
case"Withdrawal":
rbWithdrawal.Checked =true;
break;
case"Service Fee":
rbServiceFee.Checked =true;
break;
}
}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Account_Balance { public partial class BalanceForm : Form { public BalanceForm() { InitializeComponent(); } //Create a class to refer to different transaction types enum transactionTypes { deposit, withdrawal, serviceFee } //initialize form level variables private decimal currentBalance = 0m,clearedBalance = 0m; int currentIndex = 1; Transaction[] record = new Transaction[21]; //Create a variable to store the transaction Type. transactionTypes transType; struct Transaction { public DateTime Date; public string payee; public decimal Amount; public string Type; public string checkNumber; public string ToString(DateTime date, string type, decimal amount) { if (type != "Deposit") return date.ToString("d").PadRight(13) + type.PadRight(21) + (amount * -1).ToString("C"); return date.ToString("d").PadRight(13) + type.PadRight(22) + amount.ToString("C"); } } //Display Header information. private void DisplayHeader() { lstTransactions.Items.Add("Date".PadRight(13) + "Transaction Type".PadRight(22) + "Amount"); } private void DisplayBalance() { lblCurrent.Text = currentBalance.ToString("C"); lblCleared.Text = clearedBalance.ToString("C"); } private void Listbox_SelectedIndexChanged(object sender, EventArgs e) { if (lstTransactions.SelectedIndex != -1) { Transaction thisTransaction = (Transaction)lstTransactions.SelectedItem; txtPayee.Text = thisTransaction.payee; txtCheck.Text = thisTransaction.checkNumber; txtAmount.Text = thisTransaction.Amount.ToString(); txtDate.Text = thisTransaction.Date.ToString(); switch (thisTransaction.Type) { case "Deposit": rbDeposit.Checked = true; break; case "Withdrawal": rbWithdrawal.Checked = true; break; case "Service Fee": rbServiceFee.Checked = true; break; } } } //Verify the funds available if type is a withdrawal private bool isValidTransaction(decimal amount, transactionTypes type) { if (type == transactionTypes.withdrawal) { if (amount > currentBalance) return false; } return true; } //Tag the radio buttons with the appropriate transaction type when the form loads private void BalanceForm_Load(object sender, EventArgs e) { rbDeposit.Tag = transactionTypes.deposit; rbServiceFee.Tag = transactionTypes.serviceFee; rbWithdrawal.Tag = transactionTypes.withdrawal; ClearData(); DisplayHeader(); } private string SetType() { string type = ""; if (rbDeposit.Checked) type = "Deposit"; else if (rbServiceFee.Checked) type = "Service Fee"; else type = "Withdrawal"; return type; } //Change the transaction type according to the radio button selected. private void RadioButtons_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked) transType = (transactionTypes)rb.Tag; } //Confirm we have a valid date, return the actual date if valid, if invalid send back a date one day ahead of current date to signify an invalid date. private DateTime isValidDate(string entry) { DateTime value; if (DateTime.TryParse(entry, out value)) { if (value > DateTime.Today) value = DateTime.Today.AddDays(1); } else value = DateTime.Today.AddDays(1); return value; } //Confirm we have a valid amount, return the actual amount if valid, if invalid send back the amount as -1 to signify an invalid amount. private decimal isValidAmount(string entry) { decimal value; if (decimal.TryParse(entry, out value)) { if (value <= 0) value = -1; } else value = -1; return value; } //Confirm valid entries, if all are valid, return the amount. If any are invalid, display a message to user notifying them of the error private decimal isValidEntries() { string message = ""; decimal amountEntry; bool answer = true; if (isValidDate(txtDate.Text)== DateTime.Today.AddDays(1)) { answer = false; message = "Please enter a valid date on or before today"; } amountEntry = isValidAmount(txtAmount.Text); if (amountEntry == -1) { answer = false; message += "\n" + "Amount must be a number greater than zero."; } if (rbWithdrawal.Checked) { if (txtPayee.Text == "") { answer = false; message += "\n" + "Payee must be entered for a Withdrawal"; } } if (answer) { if (!isValidTransaction(amountEntry, transType)) { answer = false; message = "Insufficient funds to complete this transaction."; } errorProvider1.Clear(); } if (!answer) { amountEntry = -1; errorProvider1.SetError(btnCalculate, message); } return amountEntry; } //All entries are valid at this point, determine the transaction type and calculate accordingly. private void CalculateBalance(decimal amount) { if (transType != transactionTypes.deposit) amount *= -1; currentBalance += amount; if (chkCleared.Checked) clearedBalance += amount; } private void LoadArray() { record[currentIndex].Date = isValidDate(txtDate.Text); record[currentIndex].Amount = isValidAmount(txtAmount.Text); record[currentIndex].checkNumber = txtCheck.Text; record[currentIndex].payee = txtPayee.Text; record[currentIndex].Type = SetType(); currentIndex++; } //Update the cleared and current labels with the new balances for the user. Generate a list of transaction as the user makes them, //keeping track of the date, amount, type, and if the transaction was processed. private void DisplayData() { lstTransactions.Items.Add(record[currentIndex].ToString(isValidDate(txtDate.Text), SetType(), isValidAmount(txtAmount.Text))); DisplayBalance(); } private void ClearData() { //Clear all textboxes for another transaction entry and reset focus. txtDate.Clear(); txtPayee.Clear(); txtCheck.Clear(); txtAmount.Clear(); txtAmount.Focus(); rbWithdrawal.Checked = true; chkCleared.Checked = false; } private void btnReset_Click(object sender, EventArgs e) { //Reset the current balance back to zero and displaying this to the user. currentBalance = 0m; clearedBalance = 0m; DisplayBalance(); lstTransactions.Items.Clear(); DisplayHeader(); ClearData(); } private void btnClear_Click(object sender, EventArgs e) { //Clear all textboxes for another transaction entry and reset focus. ClearData(); } // Call the necessary procedures to valid all entries, set error messages (if needed), calculate balances and display balances. private void btnCalculate_Click(object sender, EventArgs e) { decimal amount = isValidEntries(); if (amount != -1) { CalculateBalance(amount); LoadArray(); DisplayData(); } } private void exitToolStripMenuItem1_Click(object sender, EventArgs e) { //Close this instance of the form. this.Close(); } } }
这篇关于有关铸造的问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!