I am new to C# but have developed an application that worked until the need came up for two additional fields of information (maybe more later). Because of the way the app is structured, I decided to ask for these two fields in a second form (Form2) using a dialog box. This seems to work well and am able to retrieve the two textboxes and include with fields from Form1 to develop a transaction and log entry. However, I have a problem.
I am not sure where I should check that both fields on Form2 have been filled. I have tried checking for blank fields in both forms but although I can generate a message telling the user that both fields are required, I cannot seem to get Form2 to redisplay for the user to fill in the fields. The if/else code continues to the next step which happens to be generating the transaction in Form1. I want it to - if the either field is empty, then display a message and don't leave Form2 until they are filled or the form canceled. Where is the best place to do this check and redisplay the form until the user fills in both fields and hits OK or hits cancel? How do I do it? I have tried various similar ways found in postings using Google and none have worked.
I have similar code in Form1 to check for valid and required fields and it works just fine. Nothing happens until the validations are satisfied. Why not in Form2.
Thanx in advance for any help. This site has been a big help in working through learning C#.
Loading
TonyPosted Oct 29, 2009, 5:31 PM
Thank you for replies. Great forum!!
TonyPosted Oct 29, 2009, 4:52 PM
I appreciate your response and effort to address this.
theLizardPosted Oct 29, 2009, 4:00 PM
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!validated) //but remember there may be cases where closing the form without validating is required.
e.Cancel;
}
Posted Oct 29, 2009, 3:55 PM
It's just too confusing wat u have done..I guess this is a small thing...Wats ur requirement...explain it clearly i'll try to do and explain u the same.
wat i have understood is u require two fields case and reason,and thats need to be validated and a dialog box for ok and cancel..am i right..
With Regards
P.Krishna Prasad
TonyPosted Oct 29, 2009, 3:27 PM
Form1.cs
{
MessageBox.Show("The Last Name and First Name are required!!");
return;
}
// Above code handles Form1 textboxes
//Call Form2 for CaseNo and Reason data
GetForm2();
...
...
// Opens second Diaolog form (Form2) to get Case Number and Reason
public void GetForm2()
{
Form2 qForm = new Form2();
if (qForm.ShowDialog()== DialogResult.OK)
{
CaseNo = qForm.CaseNo;
Reason = qForm.Reason;
MessageBox.Show("Form2 Exists" + " " + CaseNo + " " + Reason);
}
qForm.Dispose();
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DMV_Test_1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string CaseNo
{
get { return textBox1.Text; }
}
public string Reason
{
get { return textBox2.Text; }
}
private void button1_Click(object sender, System.EventArgs e)
{
if (CaseNo == "")
{
MessageBox.Show("The Case No is required!!");
this.textBox1.Focus();
return;
}
else
if (Reason == "")
{
MessageBox.Show("The Reason is required!!");
this.textBox2.Focus();
return;
}
else
{
this.DialogResult = DialogResult.OK; Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel; Close();
}
}
}
Posted Oct 29, 2009, 3:19 PM
To an extent i can figure out ur problem..my doubt is how are u navigating from the first form to the next?
and if u provide a jist of the code..it would be easy to figure out ur problem
With Regards
P.Krishna Prasad