OK, I give up! Here is the code listed below. This form is displayed from
an earlier form that had the following 2 statements:
Form paymentForm = new frmPayment();
DialogResults button = paymentForm.ShowDialog();
Here is the problem: If the user completes the frmPayment correctly, the
program works fine. If the user fails to enter a card number in
in the txtCardNumber.Text or does not make a selection from the expiration
date combo-boxes the program crashes. The purpose of
IsValidData() method is to test for these conditions, set the focus on the
offending object, and let the user make the correction. Instead,
my program leaves the form and returns to the original form, and dies
following the second statement above.
Thanks for any help you can give. Bob
-----------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Payment
{
/// <summary>
/// Summary description for frmPayment.
/// </summary>
public class frmPayment : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton rdoCreditCard;
private System.Windows.Forms.RadioButton rdoBillCustomer;
private System.Windows.Forms.ListBox lstCreditCardType;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtCardNumber;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cboExpirationYear;
private System.Windows.Forms.ComboBox cboExpirationMonth;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.CheckBox chkDefault;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmPayment()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.rdoBillCustomer = new System.Windows.Forms.RadioButton();
this.rdoCreditCard = new System.Windows.Forms.RadioButton();
this.lstCreditCardType = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.txtCardNumber = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.cboExpirationYear = new System.Windows.Forms.ComboBox();
this.cboExpirationMonth = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.chkDefault = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.rdoBillCustomer);
this.groupBox1.Controls.Add(this.rdoCreditCard);
this.groupBox1.Location = new System.Drawing.Point(8, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 48);
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Billing";
//
// rdoBillCustomer
//
this.rdoBillCustomer.Location = new System.Drawing.Point(144, 16);
this.rdoBillCustomer.Name = "rdoBillCustomer";
this.rdoBillCustomer.Size = new System.Drawing.Size(120, 24);
this.rdoBillCustomer.TabIndex = 1;
this.rdoBillCustomer.Text = "Bill customer";
this.rdoBillCustomer.CheckedChanged += new
System.EventHandler(this.Billing_CheckedChanged);
//
// rdoCreditCard
//
this.rdoCreditCard.Checked = true;
this.rdoCreditCard.Location = new System.Drawing.Point(16, 16);
this.rdoCreditCard.Name = "rdoCreditCard";
this.rdoCreditCard.TabIndex = 0;
this.rdoCreditCard.TabStop = true;
this.rdoCreditCard.Text = "Credit Card";
this.rdoCreditCard.CheckedChanged += new
System.EventHandler(this.Billing_CheckedChanged);
//
// lstCreditCardType
//
this.lstCreditCardType.Location = new System.Drawing.Point(152, 72);
this.lstCreditCardType.Name = "lstCreditCardType";
this.lstCreditCardType.Size = new System.Drawing.Size(120, 43);
this.lstCreditCardType.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 72);
this.label1.Name = "label1";
this.label1.TabIndex = 2;
this.label1.Text = "Credit card type:";
//
// txtCardNumber
//
this.txtCardNumber.Location = new System.Drawing.Point(152, 120);
this.txtCardNumber.Name = "txtCardNumber";
this.txtCardNumber.Size = new System.Drawing.Size(120, 20);
this.txtCardNumber.TabIndex = 1;
this.txtCardNumber.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 120);
this.label2.Name = "label2";
this.label2.TabIndex = 4;
this.label2.Text = "Card number:";
//
// cboExpirationYear
//
this.cboExpirationYear.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboExpirationYear.Location = new System.Drawing.Point(200, 144);
this.cboExpirationYear.Name = "cboExpirationYear";
this.cboExpirationYear.Size = new System.Drawing.Size(72, 21);
this.cboExpirationYear.TabIndex = 3;
//
// cboExpirationMonth
//
this.cboExpirationMonth.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboExpirationMonth.Location = new System.Drawing.Point(120, 144);
this.cboExpirationMonth.Name = "cboExpirationMonth";
this.cboExpirationMonth.Size = new System.Drawing.Size(72, 21);
this.cboExpirationMonth.TabIndex = 2;
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 148);
this.label3.Name = "label3";
this.label3.TabIndex = 7;
this.label3.Text = "Expiration date:";
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(120, 208);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 5;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(208, 208);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 6;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// chkDefault
//
this.chkDefault.Checked = true;
this.chkDefault.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkDefault.Location = new System.Drawing.Point(8, 176);
this.chkDefault.Name = "chkDefault";
this.chkDefault.Size = new System.Drawing.Size(184, 24);
this.chkDefault.TabIndex = 4;
this.chkDefault.Text = "Set as default billing method";
//
// frmPayment
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(292, 266);
this.ControlBox = false;
this.Controls.Add(this.chkDefault);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.label3);
this.Controls.Add(this.cboExpirationMonth);
this.Controls.Add(this.cboExpirationYear);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtCardNumber);
this.Controls.Add(this.label1);
this.Controls.Add(this.lstCreditCardType);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "frmPayment";
this.Text = "Payment";
this.Load += new System.EventHandler(this.frmPayment_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void frmPayment_Load(object sender, System.EventArgs e)
{
lstCreditCardType.Items.Add("VISA");
lstCreditCardType.Items.Add("Mastercard");
lstCreditCardType.Items.Add("American Express");
lstCreditCardType.SelectedIndex = 0; //default VISA
string[] months = {"Select a month...", //like BASIC data statement
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
foreach (string month in months) cboExpirationMonth.Items.Add(month); //List
cboExpirationMonth.SelectedIndex = 0; //default "Select a month..."
int year = DateTime.Today.Year; //pluck from structure Year property
int endYear = year + 8;
cboExpirationYear.Items.Add("Select a year..."); //List can be mixed type
while (year < endYear)
{
cboExpirationYear.Items.Add(year); //load the years
year++;
}
cboExpirationYear.SelectedIndex = 0; //default "Select a year..."
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
//no code is given in text, but this just closes the form
this.Close();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
if (IsValidData())
{
//Load text for the ListBox
string msg = null; //msg to be returned in the Tag
if (rdoCreditCard.Checked) //We are dealing with a credit card.
{
msg += "Charge to credit card." + "\n";
msg += "\n"; //data from list and combo boxes
msg += "Card type: " + lstCreditCardType.Text + "\n";
msg += "Card number: " + txtCardNumber.Text + "\n";
msg += "Expiration date: " + cboExpirationMonth.Text + "/"
+ cboExpirationYear.Text + "\n";
}
else //if it is not a credit card it must be direct billing
{
msg += "Send bill to customer." + "\n";
msg += "\n";
}
bool isDefaultBilling = chkDefault.Checked; //if checked add to msg
msg += "Default billing: " + isDefaultBilling;
this.Tag = msg; /*Notice that form is not a method so we can not
"return" anything. Information is PASSED through the Tag.*/
}
else
{
MessageBox.Show("Returned from Failure", "debug");
}
} /*Expected that if IsValidData() is false to see form at this point with
the focus set to the offending object*/
private bool IsValidData() //Necessary because first list item is
instructions
{
if (rdoCreditCard.Checked) //if not checking is unnessary
{
if (txtCardNumber.Text == "") //failure to enter card number
{
MessageBox.Show("You must enter a credit card number", "Entry error");
txtCardNumber.Focus();
return false;
}
if (cboExpirationMonth.SelectedIndex == 0) //month failure
{
MessageBox.Show("You must enter a month", "Entry error");
cboExpirationMonth.Focus();
return false;
}
if (cboExpirationYear.SelectedIndex == 0) //year failure
{
MessageBox.Show("You must enter a year", "Entry error");
cboExpirationYear.Focus();
return false;
}
}
return true;
}
private void Billing_CheckedChanged(object sender, System.EventArgs e)
{
//This method started out as rdoCreditCard_CheckedChange, but was
//changed to Billing_CheckedChanged. N.B.: You must also change the
//wiring in Form Designer Generated Code. This is how it looks now:
//this.rdoCreditCard.CheckedChanged +=
//new System.EventHandler(this.Billing_CheckedChanged);
if (rdoCreditCard.Checked) EnableControls();
else DisableControls();
}
private void EnableControls() //Credit card processing requires controls ON
{
lstCreditCardType.Enabled = true;
txtCardNumber.Enabled = true;
cboExpirationMonth.Enabled = true;
cboExpirationYear.Enabled = true;
}
private void DisableControls() //Bill customer grays out controls
{
lstCreditCardType.Enabled = false;
txtCardNumber.Enabled = false;
cboExpirationMonth.Enabled = false;
cboExpirationYear.Enabled = false;
}
}
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OU****************@tk2msftngp13.phx.gbl...
Robert,
See inline.
"Robert Schuldenfrei" <bo*@s-i-inc.com> wrote in message
news:WSktc.3038$eY2.1481@attbi_s02... I am an older person trying to learn C# just for the fun of it. I am a
veteran of older style languages (COBOL, FORTRAN, etc.) and I want to learn an Object Orientated language. Currently working my way through
Murach's C# by Joel Murach and Doug Lowe. Since I am not taking a course, I have no one to whom I can address questions. That is why I am turning to this
newsgroup. Is this the best NG for a newbie like me? Are there people
out there who have used the book?
I have not used the book personally, but people in the newsgroup are
more than happy to answer whatever questions you have on C#/.NET, and
well, life in general.
My goal is not to bug the NG, as I am sure you all have many important
things to do with your time.
Apparently, I do not. Also, with the number of posts to this group
per day, I don't think that one or ten by you will be that much of an
annoyance. =)
As for the rest of your questions, it would help if you posted the
code. That way, we can see what is going on, and give you better advice. While
some of us might not have read the book, we can be of help still in
helping you to figure out the code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com