Antti,
I really appreciate the feedback. I should have stated the fact that I am
passing two variables by reference but I don't know how to set the variable
that I pass to the second form to the value selected in the second form.
When I try to set the variable. I get a message from the compiler that
states the varaible does not exist in the namespace. My code is below.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Data.Sql Client;
namespace EnspireUtilitie s
{
/// <summary>
/// Summary description for Lookup.
/// </summary>
public class fclsLookup : System.Windows. Forms.Form
{
private System.Data.Sql Client.SqlDataA dapter sqlDataAdapter1 ;
private System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
private System.Data.Sql Client.SqlConne ction sqlConnection1;
private EnspireUtilitie s.dsLookup dsLookup1;
private System.Windows. Forms.DataGrid dataGrid1;
private System.Windows. Forms.Button btnLoad;
private System.Windows. Forms.TextBox tboxLookupZipCo de;
private System.Windows. Forms.Label lblLookupParame ter;
private System.Windows. Forms.Button btnOK;
private System.Windows. Forms.Button btnCancel;
//private string strZipCode = string.Empty;
private System.Windows. Forms.DataGridT ableStyle dataGridTableSt yle1;
private System.Windows. Forms.DataGridT extBoxColumn dataGridTextBox Column1;
private System.Windows. Forms.DataGridT extBoxColumn dataGridTextBox Column2;
private System.Windows. Forms.DataGridT extBoxColumn dataGridTextBox Column3;
private System.Windows. Forms.DataGridT extBoxColumn dataGridTextBox Column4;
public string strZipCode = String.Empty;
public string strZipCodeDtlId = String.Empty;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public fclsLookup(ref string ZipCode, ref string ZipCodeDtlId)
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
//tboxLookupZipCo de.Text = ZipCode;
sqlSelectComman d1.Parameters["@zip"].Value = ZipCode;
dsLookup1.Clear ();
sqlDataAdapter1 .Fill(dsLookup1 );
strZipCode = ZipCode;
strZipCodeDtlId = ZipCodeDtlId;
tboxLookupZipCo de.Text = strZipCode;
this.dataGrid1. MouseDown += new
System.Windows. Forms.MouseEven tHandler(this.d ataGrid1_MouseD own);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.sqlDataAda pter1 = new System.Data.Sql Client.SqlDataA dapter();
this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
this.sqlConnect ion1 = new System.Data.Sql Client.SqlConne ction();
this.dsLookup1 = new EnspireUtilitie s.dsLookup();
this.dataGrid1 = new System.Windows. Forms.DataGrid( );
this.dataGridTa bleStyle1 = new System.Windows. Forms.DataGridT ableStyle();
this.dataGridTe xtBoxColumn1 = new
System.Windows. Forms.DataGridT extBoxColumn();
this.dataGridTe xtBoxColumn2 = new
System.Windows. Forms.DataGridT extBoxColumn();
this.dataGridTe xtBoxColumn3 = new
System.Windows. Forms.DataGridT extBoxColumn();
this.dataGridTe xtBoxColumn4 = new
System.Windows. Forms.DataGridT extBoxColumn();
this.btnLoad = new System.Windows. Forms.Button();
this.tboxLookup ZipCode = new System.Windows. Forms.TextBox() ;
this.lblLookupP arameter = new System.Windows. Forms.Label();
this.btnOK = new System.Windows. Forms.Button();
this.btnCancel = new System.Windows. Forms.Button();
((System.Compon entModel.ISuppo rtInitialize)(t his.dsLookup1)) .BeginInit();
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .BeginInit();
this.SuspendLay out();
//
// sqlDataAdapter1
//
this.sqlDataAda pter1.SelectCom mand = this.sqlSelectC ommand1;
this.sqlDataAda pter1.TableMapp ings.AddRange(n ew
System.Data.Com mon.DataTableMa pping[] {
new System.Data.Com mon.DataTableMa pping("Table",
"p_getZipCodeDa ta", new System.Data.Com mon.DataColumnM apping[] {
new
System.Data.Com mon.DataColumnM apping("zip", "zip"),
new
System.Data.Com mon.DataColumnM apping("city", "city"),
new
System.Data.Com mon.DataColumnM apping("county" , "county"),
new
System.Data.Com mon.DataColumnM apping("state", "state"),
new
System.Data.Com mon.DataColumnM apping("zip_cod e_dtl_id",
"zip_code_dtl_i d")})});
//
// sqlSelectComman d1
//
this.sqlSelectC ommand1.Command Text = "[p_getZipCodeDat a]";
this.sqlSelectC ommand1.Command Type =
System.Data.Com mandType.Stored Procedure;
this.sqlSelectC ommand1.Connect ion = this.sqlConnect ion1;
this.sqlSelectC ommand1.Paramet ers.Add(new
System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
System.Data.Sql DbType.Int, 4, System.Data.Par ameterDirection .ReturnValue,
false, ((System.Byte)( 0)), ((System.Byte)( 0)), "",
System.Data.Dat aRowVersion.Cur rent, null));
this.sqlSelectC ommand1.Paramet ers.Add(new
System.Data.Sql Client.SqlParam eter("@zip", System.Data.Sql DbType.VarChar,
10));
//
// sqlConnection1
//
this.sqlConnect ion1.Connection String = "workstatio n id=CENGENXP2;pa cket
size=4096;user id=sa;data source=cengenxp 2;persis" +
"t security info=True;initi al catalog=src;pas sword=cocacola" ;
//
// dsLookup1
//
this.dsLookup1. DataSetName = "dsLookup";
this.dsLookup1. Locale = new System.Globaliz ation.CultureIn fo("en-US");
//
// dataGrid1
//
this.dataGrid1. DataMember = "p_getZipCodeDa ta";
this.dataGrid1. DataSource = this.dsLookup1;
this.dataGrid1. HeaderForeColor = System.Drawing. SystemColors.Co ntrolText;
this.dataGrid1. Location = new System.Drawing. Point(0, 40);
this.dataGrid1. Name = "dataGrid1" ;
this.dataGrid1. Size = new System.Drawing. Size(568, 288);
this.dataGrid1. TabIndex = 0;
this.dataGrid1. TableStyles.Add Range(new
System.Windows. Forms.DataGridT ableStyle[] {
this.dataGridTa bleStyle1});
this.dataGrid1. Navigate += new
System.Windows. Forms.NavigateE ventHandler(thi s.dataGrid1_Nav igate);
//
// dataGridTableSt yle1
//
this.dataGridTa bleStyle1.DataG rid = this.dataGrid1;
this.dataGridTa bleStyle1.GridC olumnStyles.Add Range(new
System.Windows. Forms.DataGridC olumnStyle[] {
this.dataGridTe xtBoxColumn1,
this.dataGridTe xtBoxColumn2,
this.dataGridTe xtBoxColumn3,
this.dataGridTe xtBoxColumn4});
this.dataGridTa bleStyle1.Heade rForeColor =
System.Drawing. SystemColors.Co ntrolText;
this.dataGridTa bleStyle1.Mappi ngName = "p_getZipCodeDa ta";
//
// dataGridTextBox Column1
//
this.dataGridTe xtBoxColumn1.Fo rmat = "";
this.dataGridTe xtBoxColumn1.Fo rmatInfo = null;
this.dataGridTe xtBoxColumn1.He aderText = "Zip Code";
this.dataGridTe xtBoxColumn1.Ma ppingName = "zip";
this.dataGridTe xtBoxColumn1.Wi dth = 75;
//
// dataGridTextBox Column2
//
this.dataGridTe xtBoxColumn2.Fo rmat = "";
this.dataGridTe xtBoxColumn2.Fo rmatInfo = null;
this.dataGridTe xtBoxColumn2.He aderText = "City";
this.dataGridTe xtBoxColumn2.Ma ppingName = "city";
this.dataGridTe xtBoxColumn2.Wi dth = 75;
//
// dataGridTextBox Column3
//
this.dataGridTe xtBoxColumn3.Fo rmat = "";
this.dataGridTe xtBoxColumn3.Fo rmatInfo = null;
this.dataGridTe xtBoxColumn3.He aderText = "County";
this.dataGridTe xtBoxColumn3.Ma ppingName = "county";
this.dataGridTe xtBoxColumn3.Wi dth = 75;
//
// dataGridTextBox Column4
//
this.dataGridTe xtBoxColumn4.Fo rmat = "";
this.dataGridTe xtBoxColumn4.Fo rmatInfo = null;
this.dataGridTe xtBoxColumn4.He aderText = "State";
this.dataGridTe xtBoxColumn4.Ma ppingName = "state";
this.dataGridTe xtBoxColumn4.Wi dth = 75;
//
// btnLoad
//
this.btnLoad.Lo cation = new System.Drawing. Point(472, 8);
this.btnLoad.Na me = "btnLoad";
this.btnLoad.Ta bIndex = 2;
this.btnLoad.Te xt = "Load";
this.btnLoad.Cl ick += new System.EventHan dler(this.btnLo ad_Click);
//
// tboxLookupZipCo de
//
this.tboxLookup ZipCode.Locatio n = new System.Drawing. Point(136, 8);
this.tboxLookup ZipCode.Name = "tboxLookupZipC ode";
this.tboxLookup ZipCode.Size = new System.Drawing. Size(200, 20);
this.tboxLookup ZipCode.TabInde x = 1;
this.tboxLookup ZipCode.Text = "";
//
// lblLookupParame ter
//
this.lblLookupP arameter.Locati on = new System.Drawing. Point(24, 8);
this.lblLookupP arameter.Name = "lblLookupParam eter";
this.lblLookupP arameter.Size = new System.Drawing. Size(100, 20);
this.lblLookupP arameter.TabInd ex = 3;
this.lblLookupP arameter.Text = "Zip Code";
this.lblLookupP arameter.TextAl ign =
System.Drawing. ContentAlignmen t.MiddleRight;
//
// btnOK
//
this.btnOK.Dial ogResult = System.Windows. Forms.DialogRes ult.OK;
this.btnOK.Loca tion = new System.Drawing. Point(378, 352);
this.btnOK.Name = "btnOK";
this.btnOK.TabI ndex = 3;
this.btnOK.Text = "OK";
this.btnOK.Clic k += new System.EventHan dler(this.btnOK _Click);
//
// btnCancel
//
this.btnCancel. DialogResult = System.Windows. Forms.DialogRes ult.Cancel;
this.btnCancel. Location = new System.Drawing. Point(472, 352);
this.btnCancel. Name = "btnCancel" ;
this.btnCancel. TabIndex = 4;
this.btnCancel. Text = "Cancel";
this.btnCancel. Click += new System.EventHan dler(this.btnCa ncel_Click);
//
// fclsLookup
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(568, 390);
this.Controls.A dd(this.btnCanc el);
this.Controls.A dd(this.btnOK);
this.Controls.A dd(this.lblLook upParameter);
this.Controls.A dd(this.tboxLoo kupZipCode);
this.Controls.A dd(this.btnLoad );
this.Controls.A dd(this.dataGri d1);
this.Name = "fclsLookup ";
this.Text = "Lookup";
((System.Compon entModel.ISuppo rtInitialize)(t his.dsLookup1)) .EndInit();
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .EndInit();
this.ResumeLayo ut(false);
}
#endregion
private void btnLoad_Click(o bject sender, System.EventArg s e)
{
sqlSelectComman d1.Parameters["@zip"].Value = tboxLookupZipCo de.Text;
dsLookup1.Clear ();
sqlDataAdapter1 .Fill(dsLookup1 );
}
private void btnOK_Click(obj ect sender, System.EventArg s e)
{
Close();
}
private void btnCancel_Click (object sender, System.EventArg s e)
{
Close();
}
private void dataGrid1_Navig ate(object sender,
System.Windows. Forms.NavigateE ventArgs ne)
{
Close();
}
protected void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
string strMessage = "TEST";
//strMessage = "Row: " + dataGrid1.HitTe st(e.X,e.Y).Row .ToString();
int x = dataGrid1.HitTe st(e.X, e.Y).Row;
strZipCode = dataGrid1[x, 0].ToString();
strZipCodeDtlId = dataGrid1[x, 3].ToString();
MessageBox.Show (strZipCode);
MessageBox.Show ("I still don't know what I'm doing, but I'm at " +
strMessage + ". I need to send back " + strZipCode);
}
}
}
"Antti Keskinen" wrote:
Hello !
There are no stupid questions.
The forms you define in C# always consist of a class. A single form is a
single class. Thus, your problem comes from the interaction between classes.
You already knew this, however.
So, in addition to passing a reference variable to the zip code the user has
entered (the one which you wish to check), pass in another reference to the
ACTUAL zip code returned by the lookup form. Now, when the lookup form is
launched, you use the first reference to pass in the zipcode and initialize
the datagrid to show possible values, and the second reference to pass back
the value which the user clicked on in the datagrid.
Here's the most simplest example I can think of. It's written in C++, but
the language syntax is pretty much similar in C#.
class MyForm1
{
public:
int m_nUsersZipCode ;
int m_nDatagridResu lt;
MyForm2* m_pLookupForm;
};
class MyForm2
{
public:
DWORD ValidateZipCode (int& nRefSource, int& nRefSelected);
};
int main(void)
{
MyForm1 form1;
MyForm2 form2;
form1.Show();
}
// In the ShowLookupForm handler of MyForm1
..
m_pLookupForm->ValidateZipCod e( m_nUsersZipCode ,
m_nDatagridResu lt );
..
This would be the most simplistic way to accomplish it. The problem itself
is not C# related by nature, thus it's quite easy to answer it.
-Antti Keskinen
"Johnny" <Jo****@discuss ions.microsoft. com> wrote in message
news:8E******** *************** ***********@mic rosoft.com... I'm a rookie at C# and OO so please don't laugh! I have a form
(fclsTaxCalcula tor) that contains a text box (tboxZipCode) containing a
zip
code. The user can enter a zip code in the text box and click a button to
determine whether the zip code is unique. If the zip code is not unique,
another form/dialog is displayed (fclsLookup) - lookup form/dialog. The
zip
code is passed to the lookup form/dialog by reference. I then load a
datagrid with the possible matches (i.e. zip, city, county, state, zipid).
When the user clicks the row in the datagrid, I want to pass the zipid
back
to the original form/dialog and run a stored procedure. My problem is I
do
not know how to get the ZipId back to the fclsTaxCalculat or form/dialog.
I've search for examples of this and I've not been able to track one
down -
please help!!!