[C#] Variable aus Form 2 in Form 3

10/18/2013 23:59 Ramon0205#1
Hallo, ich bin gerade dabei ein kleinen Bot zu schreiben.. Dabei habe ich eine Form 1 wo mein Browser ( also das Spiel ) drauf läuft. Nun habe ich einen Button, der eine 2 Form öffnet wo man seine Logindaten eingeben kann. Diese daten sollen in string´s gespeichert werden und dann an die Form 3 ( die sich nachdem man Save auf der Form2 gedrückt hat ) gesendet werden und dort dann wieder ausgegeben werden. Doch leider bekomme ich das einfach nicht hin. In google stehen viele verschiedene sachen, die mir aber alle nicht geholfen haben. Daher habe ich mich dazu entschieden hier mal nach zufragen.

Mfg Ramon0205
10/19/2013 00:21 snow#2
Schau dir mal Konstruktoren an: [Only registered and activated users can see links. Click Here To Register...]
So kannst du Parameter bei der Erstellung von Form3 übergeben.

Das gehört übrigens in die .NET Sektion ([Only registered and activated users can see links. Click Here To Register...]), ich schicke mal einen Moverequest ab. :)
10/19/2013 16:47 berkay2578#3
Form3:
Code:
public string userName;
public string password;
Form2:
Code:
/*
There should be 2 textboxes.
usernameTXT is for the username
passwordTXT is for the password
*/

//Declaration;
private Form3 _Form3 = null;

//Form Load Event;
_Form3 = new Form3();

//Form Close Event;
_Form3.userName = usernameTXT.Text;
_Form3.password = passwordTXT.Text;
_Form3.Dispose(); _Form3 = null;
You can then use them on Form3 using userName & password :p
10/21/2013 10:54 SwarN#4
Hör bitte nicht auf "berkay2578"

sondern löse es wie snow schon schrieb mittels Konstruktoren, denn dafür sind diese da!


Klar funktioniert das bestimmt auch, aber SAUBER ist was anderes...
10/22/2013 19:55 Icestreak#5
are you kidding me?
just create class

Code:
public class hereIam
{
public static string variblenumber1 {get;set;}
}
and use
Code:
hereIam.variblenumber1 = "1";
from everywhere.
10/22/2013 20:27 snow#6
Wow, lets violate every single rule about object orientated programming and data encapsulation. :<
10/22/2013 21:10 Demon-777#7
Form1 (Browser):

Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.InitializeComponent();
        }

        private void OnButtonOpenForm2Click(object sender, EventArgs e)
        {
            var form2 = new Form2();
            form2.ShowDialog(this);
        }
    }
Form2 (Login):

Code:
    public partial class Form2 : Form
    {
        public Form2()
        {
            this.InitializeComponent();
        }

        private void OnButtonSaveClick(object sender, EventArgs e)
        {
            var loginParameter = new LoginParameter
            {
                Username = "John Doe",  // Or Username = this.TextBoxUsername.Text
                Password = "12345"       // Or Password =  this.TextBoxPassword.Text
            };

            var form3 = new Form3(loginParameter); // Pass login parameters to form3 via constructor.
            form3.ShowDialog(this);
        }
    }
Form3 (Output):

Code:
    public partial class Form3 : Form
    {
        private readonly LoginParameter _loginParameter;

        public Form3(LoginParameter loginParameter)
        {
            this.InitializeComponent();
            this._loginParameter = loginParameter;
        }

        private void ShowLoginParameter()
        {
            string text = String.Format("User: {0} | Password: {1}",
                this._loginParameter.Username, this._loginParameter.Password);

            MessageBox.Show(text, "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
LoginParameter.cs (Extra class for login information)

Code:
    public class LoginParameter
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
Not tested.
11/16/2013 17:26 DeinMud#8
Form1:

Code:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
            frm3.Show();
        }
    }
}
Form2:

Code:
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public delegate void LoginEventHandler(object sender, LoginEventArgs e);
        public event LoginEventHandler LoggedIn;
        private void button1_Click_1(object sender, EventArgs e)
        {
            LoggedIn(this, new LoginEventArgs(textBox1.Text, textBox2.Text));
        }
    }

    public class LoginEventArgs
    {
        private string _username;
        public string username { get { return _username; } }
        private string _password;
        public string password { get { return _password; } }
        public LoginEventArgs(string Username, string password)
        {
            this._username = Username;
            this._password = password;
        }

    }
}
Form3:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {

        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            frm2.LoggedIn += received;
        }
        private void received(object sender,LoginEventArgs e)
        {
            MessageBox.Show(e.username);
            MessageBox.Show(e.password);
        }
    }
}
11/23/2013 23:45 Krass, was?#9
Getters und setters festlegen. Dann kannst du ganz normal auf die Variablen zugreifen

LG
11/27/2013 21:35 Strean#10
oder man macht es sich ganz einfach in eine properties datei :rolleyes: