[C#] Variablenübergabe 2. Form

Hallo,

vielen Dank für eure Antworten. Ich habe das Problem endlich gelöst.

Hier die Antwort:

Über Form1 rufe ich Form3 auf:
Code:
        //Auswahl Form3
        private void Auswahl(TextBox Box, string sBox, string Tabelle)
        {
            if (Box.Text == "")
            {
                Form3 f3 = new Form3(Tabelle, Root, sBox); //die Übergabe der kleinen fiesen Variablen
                f3.frm1 = this;
                f3.Tabelle = Tabelle;
                f3.sBox = sBox;
                f3.Root = Root;
                f3.Show();
            }
        }

In der Form3 kann ich dadurch die Variablen in Optionen() verwenden:
Code:
    public partial class Form3 : Form
    {
        private Form1 _frm1;

        public Form3(string Tabelle, string Root, string sBox) //hier sind wieder die kleinen fiesen Variablen :)
        {
            InitializeComponent();

            this.Text = Tabelle;

            Optionen(Root, Tabelle);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string value = "";

            try
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    if (dr.Cells[0].Value != null) value += dr.Cells[1].Value + ", ";
                    dr.Cells[0].Value = null;
                }

                value = value.Substring(0, value.Length - 2);

                if (sBox == "Box7") this._frm1.textBox7.Text = value;
                if (sBox == "Box8") this._frm1.textBox8.Text = value;
                if (sBox == "Box9") this._frm1.textBox9.Text = value;
                if (sBox == "Box10") this._frm1.textBox10.Text = value;
                if (sBox == "Box11") this._frm1.textBox11.Text = value;
                if (sBox == "Box12") this._frm1.textBox12.Text = value;
            }
            catch
            {
                MessageBox.Show("Es muss mindestens eine Option ausgewählt werden", "Fehler");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

//##########################################################################################
//#################################### Funktionen ##########################################
//##########################################################################################

        private void Optionen(string Pfad, string Table)
        {

            SqlCeConnection conn = new SqlCeConnection("Data Source=" + Pfad + "Termine.sdf");
            conn.Open();

            try
            {
                SqlCeCommand cmd_greattable = new SqlCeCommand("SELECT " + Table + " FROM Werte", conn); // ORDER BY '" + Tabelle + "' ASC", conn);
                SqlCeDataAdapter adp_greattable = new SqlCeDataAdapter(cmd_greattable);

                DataTable dtbudget0 = new DataTable();

                adp_greattable.Fill(dtbudget0);

                dataGridView1.DataSource = dtbudget0;
                dataGridView1.AutoGenerateColumns = true;

                Ereignis("Abfrage Auswahl [" + Table + "]", "", "Erfolgreich");
            }

            catch (Exception ex)
            {
                Ereignis("Abfrage Auswahl [" + Table + "]", ex.ToString(), "Fehlerhaft");

                MessageBox.Show(ex.ToString(), "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            conn.Close();
        }
        
        //Fehleraufzeichnung
        private void Ereignis(string Aktion, string Fehler, string Status)
        {
            string Zeit = DateTime.Now.ToString();

            if (Fehler == "") Fehler = "-";

            try
            {
                SqlCeConnection conn = new SqlCeConnection("Data Source=" + Root + "Termine.sdf");
                conn.Open();

                SqlCeCommand command = conn.CreateCommand();

                command.CommandText = "INSERT INTO Ereignisse(Aktion, Ereignis, Status, Zeitpunkt) VALUES(?, ?, ?, ?)";

                command.Parameters.Add(new SqlCeParameter("Aktion", SqlDbType.NVarChar));
                command.Parameters.Add(new SqlCeParameter("Ereignis", SqlDbType.NVarChar));
                command.Parameters.Add(new SqlCeParameter("Status", SqlDbType.NVarChar));
                command.Parameters.Add(new SqlCeParameter("Zeitpunkt", SqlDbType.NVarChar));

                command.Parameters["Aktion"].Value = Aktion;
                command.Parameters["Ereignis"].Value = Fehler;
                command.Parameters["Status"].Value = Status;
                command.Parameters["Zeitpunkt"].Value = Zeit;

                command.ExecuteNonQuery();

                conn.Close();
            }
            catch (SqlCeException)
            {
                return;
            }
        }

        public Form1 frm1
        {
            set { this._frm1 = value; }
        }

        public string Tabelle
        {
            get;
            set;
        }

        public string Root
        {
            get;
            set;
        }

        public string sBox
        {
            get;
            set;
        }
    }

Noch einmal vielen lieben Dank für eure Hilfe.

VG
BlackRose2
 
Zurück