Jump to content

pass variables from form1 to form 2

- - - - -

  • Please log in to reply
4 replies to this topic

#1
cacheBandit

cacheBandit

    Newbie

  • Members
  • Pip
  • 8 posts
i have a program where i have a little loginScreen form and a mailer form and when you type in your email username it sets it to a variable of type string as well as the email password on the login form what i want to do is be able to access the variables that it just set on the mailer form. how would i go about that?

i have tried making everything public, but i am a noob haha.. when i tried to use these variables it wont let me. i have tried initiating these variables in the static void main method of the program but i cant access them that way either :S

#2
cacheBandit

cacheBandit

    Newbie

  • Members
  • Pip
  • 8 posts
oh... let me welcome myself to the world of public string.. lol

now i can use those variables but for some reason when i go to use it on the second form the variables dont take their assigned value from the login form. they get reset to nothing i think its because when i create a new instance of the login screen it redefines my variables as nothing again.. here is the code for the login form
namespace WindowsFormsApplication1

{


    public partial class loginScreen : Form

    {

        public string eUsername = "";

        public string ePassword = "";



        public loginScreen()

        {

            InitializeComponent();



        }


        public void logIn_Click(object sender, EventArgs e)

        {


            mailer mailer = new mailer();

            mailer.Show();

            eUsername = eUsernametxt.Text;

            ePassword = ePasswordtxt.Text;

            this.Hide();


        }

    }

}
and here is the mailer form
namespace WindowsFormsApplication1

{


    public partial class mailer : Form

    {

        string eAddress = "";

        string eSubject = "";

        string eBody = "";

        

        


        public mailer()

        {

            InitializeComponent();

        }


        public void eSend_Click(object sender, EventArgs e)

        {

            // getAddress();

            //  getSubject();

            //   getBody();

            //    getAttach();

            eAddress = eAddresstxt.Text;

            eSubject = eSubjecttxt.Text;

            eBody = eBodytxt.Text;

            



            loginScreen login = new loginScreen();

            MailMessage mailmessage = new MailMessage();

            SmtpClient mailservice = new SmtpClient("smtp.gmail.com");

            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(login.eUsername,login.ePassword);

            mailservice.Port = 587;


            mailservice.UseDefaultCredentials = false;

            mailservice.Credentials = nc;

            mailservice.EnableSsl = true;

            mailmessage.To.Add(eAddress);

            mailmessage.Subject = eSubject;

            mailmessage.Body = eBody;

            mailmessage.From = new MailAddress("proxsi@gmail.com");


            mailservice.Send(mailmessage);

            




        } 



    }


}
im not really sure how to get around this. any ideas?
---------------------------------------------------------------------------------------------------------------------
I make everything more complicated than it needs to be..

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Pass the variables to the constructor. Or use set methods.

mailer mailer = new mailer( eAddresstxt.Text, ePasswordtxt.Text );

in mailer class:
string eAddress = "";

string pass = "";


public mailer(string eAddr, string pass)

{ 

    eAddress = eAddr;

    password = pass;

    InitializeComponent();

}


#4
cacheBandit

cacheBandit

    Newbie

  • Members
  • Pip
  • 8 posts
hmm im not quite getting it, does anyone have a link that explains constructors? i would rather learn how to use them completely and properly. i have found a bunch of links but they are quite hard to follow : /
---------------------------------------------------------------------------------------------------------------------
I make everything more complicated than it needs to be..

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Googling (c#) constructors gives plenty of information.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users