Allen, please correct me if I have misunderstood.
In one form you have a TextBox. A user writes a number in this TextBox and
clicks a button that will open the EmployeeInOut Form. You want the
EmployeeInOut Form to receive this number.
You can do that by passing the number in a constructor
EmployeeInOut box1 = new EmployeeInOut(123);
or
EmployeeInOut box1 = new EmployeeInOut(Int32.Parse(textBox1.Text));
or
int temp = int.Parse(textBox1.Text)
EmployeeInOut box1 = new EmployeeInOut(temp);
In addition you need to create a constructor in EmployeeInOut that accepts
and stores the number received.
There is another way. Create a property in EmployeeInOut the parent form
can access.
EmployeeInOut box1 = new EmployeeInOut();
box1.EmpNum= temp; //
....
class EmployeeInOut : Form
{
public int EmpNum{ get; set; }
}
A third option, if you really want to you can access the Parent property of
a Form and read fields and properties directly. This is frowned upon, though.
private void OutBtn_Click(object sender, EventArgs e)
{
EmployeeID parent = this.Parent as EmployeeID
switch (parent.EmpNum)
{
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"Allen" wrote:
> I am sorry; I understand you very well, but I did not make it clear form the
> beginning that. I have no control over the integer you suggested.
> the user is going to enter "empNumBox" from a text box. In some point I
> need to use the amount entered which is of type
> "System::Windows::Forms::TextBox^". I would like to use the amount entered
> in a switch to get the right employee. Unfortunately, that switch will
> accept only integers.
>
>
> protected:
>
> private: System::Windows::Forms::TextBox^ empNumBox;
> .
> .
> .
>
> switch (empNumBox
> {
> case 1:
> {
> .
> .
> }
> break;
> case 2:
> {
> .
> .
> }
> break;
> case 3:
> {
> .
> .
>
> }
> break;
> default: MessageBox::Show(" This is an invalid employee number.\n Please try
> again! ",L" Error ");
> }
>
>
> I may have to use property like this:
>
> property TextBox^ EmpNum
> {
> //String^ get(){return empNumBox; }
> TextBox^ get(){return empNumBox; }
> void set(TextBox^ p) {empNumBox = p ;}
> }
>
>
>
>
>
>
> --
> Thanks
> Allen
> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
> news:5A200DA1-58D6-4001-853F-(E-Mail Removed)...
> > Hi Allen,
> >
> > The Employee class in my code is not a Form, just a class to hold employee
> > data.
> >
> > if all you want is to pass temp along to EmployeeInOut just create a
> > constructor in EmployeeInOut accepting an integer.
> >
> >
> > EmployeeInOut box1 = new EmployeeInOut(123);
> >
> >
> > class EmployeeInOut : Form
> > {
> > int externalNumber;
> > public EmployeeInOut(int number)
> > {
> > externalNumber = number;
> > }
> >
> > void SomeMethod()
> > {
> > switch(externalNumber)
> > {
> > }
> > // externalNumber will be the number you send in the constructor
> > }
> > }
> >
> >
> >
> > --
> > Happy Coding!
> > Morten Wennevik [C# MVP]
> >
> >
> > "Allen" wrote:
> >
> >> I am sorry I made a mistake. The 3 forms (classes). 1)Form1, 2)
> >> EmployeeID,
> >> and 3). In other words I said 2) Emploee, but I supposed to say 2)
> >> EmployeeID
> >>
> >> From (int temp = System::Int32::Parse(EmpNum);// I want to use “temp”) ,
> >> you can see all I wanted is to use "temp". As you may notice "temp" is
> >> only the "empNum" (employee number). I have to convert empNum to Int32
> >> so
> >> that I can use it in a switch in the same class (EmployeeID). Now I want
> >> to
> >> use temp or empNum in another switch but in a different class
> >> (EmployeeInOut).
> >>
> >> --
> >> Thanks
> >> Allen
> >> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
> >> news:C4BF979B-CB0B-4680-824D-(E-Mail Removed)...
> >> > Hi Allen,
> >> >
> >> > It is a bit hard to grasp what you are actually trying to do. To let
> >> > EmployeeInOut know of the temp number you can just pass it along in the
> >> > constructor. If what you really want to do is have EmployeeInOut
> >> > change
> >> > the
> >> > temp number you should instead use the DialogResult and set/get a
> >> > property
> >> > on
> >> > the EmployeeInOut.
> >> >
> >> > Then again, a better solution alltogether is to have an Employee class
> >> > you
> >> > can send among the forms, and pass along this object to the
> >> > EmployeeInOut
> >> > class which can then read and update as needed. The Employee class
> >> > would
> >> > contain the employee ID name, number, address and anything else you
> >> > would
> >> > associate with an employee.
> >> >
> >> >
> >> > Employee employee = new Employee();
> >> > EmployeeInOut box1 = new EmployeeInOut(employee);
> >> >
> >> >
> >> > --
> >> > Happy Coding!
> >> > Morten Wennevik [C# MVP]
> >> >
> >> >
> >> > "Allen" wrote:
> >> >
> >> >>
> >> >> I am making a program with 3 forms(classes). 1)Form1, 2) Employee,
> >> >> and
> >> >> 3)
> >> >> EmployeeInOu. I am using MS VS 2008.
> >> >>
> >> >> They all inherit from “System::Windows::Forms::Form”.
> >> >> My problem is I want to access variable “temp”, which belongs to
> >> >> EmployeeID
> >> >> class, in a different class which is EmployeeInOu.
> >> >> Below are some skeleton codes to show the 3 classes. I hope someone
> >> >> can
> >> >> help.
> >> >>
> >> >> //Class(1):
> >> >> public __gc class Form1 : public System::Windows::Forms::From {
> >> >> public:
> >> >> Form1(void)
> >> >> private: System::Void EmpMenuItem_Click(System::Object * sender,
> >> >> System::EventArgs * e)
> >> >> {
> >> >> //Create
> >> >> EmployeeID* box = new EmployeeID();
> >> >>
> >> >> //Fill
> >> >> box->EmpNum = S""; //set is used
> >> >>
> >> >> //Show
> >> >> if (box->ShowDialog() == DialogResult::OK)
> >> >> {
> >> >> box->EmpNum; //get is used
> >> >>
> >> >> }
> >> >> }
> >> >> };
> >> >>
> >> >> //Class (2):
> >> >> public __gc class EmployeeID : public System::Windows::Forms::Form
> >> >> {
> >> >> public:
> >> >>
> >> >> private: System::Windows::Forms::TextBox * empNumBox;
> >> >>
> >> >> public:
> >> >> __property void set_EmpNum(String* p){empNumBox->Text = p;}
> >> >> __property String* get_EmpNum() {return empNumBox->Text;}
> >> >> private: System::Void OKBtn_Click(System::Object * sender,
> >> >> System::EventArgs * e)
> >> >> {
> >> >> //Create
> >> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> >> //Fill
> >> >> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
> >> >>
> >> >> }
> >> >> };
> >> >>
> >> >> //Class (3):
> >> >> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
> >> >> {
> >> >> public:
> >> >> EmployeeInOut(void)
> >> >> {
> >> >>
> >> >>
> >> >> private: System::Void OutBtn_Click(System::Object * sender,
> >> >> //OUT
> >> >> System::EventArgs * e) //OUT
> >> >> {
> >> >> //Create
> >> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> >> //Fill
> >> >> switch (temp)//---- I want to access temp from the 2nd class to use
> >> >> in(let
> >> >> say switch) the 3rd class
> >> >> {
> >> >>
> >> >> }
> >> >>
> >> >>
> >> >>
> >> >>
> >>
>
|