Laden...

[SOLVED] [Selbstgeschriebene (Login-) InputBox] Nach DialogResult.OK passiert nichts.

Erstellt von barclay vor 6 Jahren Letzter Beitrag vor 6 Jahren 1.772 Views
B
barclay Themenstarter:in
3 Beiträge seit 2017
vor 6 Jahren
[SOLVED] [Selbstgeschriebene (Login-) InputBox] Nach DialogResult.OK passiert nichts.

Hallo. Habe gerade folgende Klasse geschrieben, die für einen Login-Prozess eine InputBox bereit stellen soll. Das Problem, das ich habe, ist, dass, wenn ich den OK-Button klicke (ganz unten im Code, der Rest kann wohl sicher überflogen werden), nichts passiert. Wenn ich aber den Abbrechen-Button klicke, passiert das, was soll, die InputBox verschwindet, über ref werden die Werte übegeben, falls es auskommentiert ist. Auch "MessageBox.Show("test");" erscheint nach dem OK/Anmelden-Button nicht, wenn es auskommentiert ist, wenn ich auf Anmelden klicke, also das scheint wohl grundsätzlich nicht wirklich so zu funktionieren. Ein "return;" nach if und else brachte ebenso nichts, selbst dann nicht, wenn ich es vor der if-Klause einfüge, der Dialog bleibt offen. Danach habe ich mal versucht die Sache etwas anders anzugehen und mal ordentlich auf den Tisch zu schlagen. Leider auch vergebens, da hätte ich mir schon wirklich einiges mehr erwartet. 😉

Aufruf/Client:

        private void Form1_Load(object sender, EventArgs e)
        {
            string login;
            string passwort;
            dialog.dialogFuerLogin(ref login, ref passwort);
            MessageBox.Show(login + "\n" + passwort);
        }

Betreffende Methode der Klasse dialog:

public void dialogFuerLogin(ref string strLogin, ref string strPasswort)
        {
            System.Drawing.Size size = new System.Drawing.Size(370, 200);
            Form inpBoxLogin = new Form();

            inpBoxLogin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            inpBoxLogin.ClientSize = size;
            inpBoxLogin.Text = "Login";

            System.Windows.Forms.Label lblLoginName = new Label();
            lblLoginName.AutoSize = true;
            lblLoginName.Location = new System.Drawing.Point(41, 72);
            lblLoginName.Name = "lblLoginName";
            lblLoginName.Size = new System.Drawing.Size(59, 13);
            lblLoginName.TabIndex = 2;
            lblLoginName.Text = "Loginname";

            System.Windows.Forms.Label lblPasswort = new Label();
            lblPasswort.AutoSize = true;
            lblPasswort.Location = new System.Drawing.Point(41, 124);
            lblPasswort.Name = "lblPasswort";
            lblPasswort.Size = new System.Drawing.Size(50, 13);
            lblPasswort.TabIndex = 2;
            lblPasswort.Text = "Passwort";

            System.Windows.Forms.Label lblAnmeldung = new Label();
            lblAnmeldung.AutoSize = true;
            lblAnmeldung.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblAnmeldung.Location = new System.Drawing.Point(40, 30);
            lblAnmeldung.Name = "lblAnmeldung";
            lblAnmeldung.Size = new System.Drawing.Size(99, 20);
            lblAnmeldung.TabIndex = 2;
            lblAnmeldung.Text = "Anmeldung";

            System.Windows.Forms.TextBox txtLoginName = new TextBox();
            txtLoginName.Location = new System.Drawing.Point(44, 88);
            txtLoginName.Name = "txtLogin";
            txtLoginName.Size = new System.Drawing.Size(164, 20);
            txtLoginName.TabIndex = 0;

            System.Windows.Forms.TextBox txtPasswort = new TextBox();
            txtPasswort.Location = new System.Drawing.Point(44, 140);
            txtPasswort.Name = "txtPasswort";
            txtPasswort.UseSystemPasswordChar = true;
            txtPasswort.Size = new System.Drawing.Size(164, 20);
            txtPasswort.TabIndex = 1;

            Button btnAnmelden = new Button();
            btnAnmelden.Location = new System.Drawing.Point(239, 88);
            btnAnmelden.Name = "btnAnmelden";
            btnAnmelden.Size = new System.Drawing.Size(91, 34);
            btnAnmelden.TabIndex = 3;
            btnAnmelden.Text = "Login";
            btnAnmelden.UseVisualStyleBackColor = true;

            Button btnAbbrechen = new Button();
            btnAbbrechen.Location = new System.Drawing.Point(239, 126);
            btnAbbrechen.Name = "btnAbbrechen";
            btnAbbrechen.Size = new System.Drawing.Size(91, 34);
            btnAbbrechen.TabIndex = 4;
            btnAbbrechen.Text = "Login abbrechen";
            btnAbbrechen.UseVisualStyleBackColor = true;

            inpBoxLogin.Controls.Add(btnAnmelden);
            inpBoxLogin.Controls.Add(btnAbbrechen);
            inpBoxLogin.Controls.Add(txtLoginName);
            inpBoxLogin.Controls.Add(txtPasswort);
            inpBoxLogin.Controls.Add(lblLoginName);
            inpBoxLogin.Controls.Add(lblPasswort);
            inpBoxLogin.Controls.Add(lblAnmeldung);            
            inpBoxLogin.AcceptButton = btnAnmelden;
            inpBoxLogin.CancelButton = btnAbbrechen;
            //inpBoxLogin.ShowDialog();
            //return;
            if (inpBoxLogin.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show("test"); // funktioniert ebenfalls nicht
                strLogin = txtLoginName.Text;
                strPasswort = txtPasswort.Text;
                inpBoxLogin.Dispose();
            }
            else
            {
                //strLogin = txtLoginName.Text;
                //strPasswort = txtPasswort.Text;
                inpBoxLogin.Dispose();
            }
        }
771 Beiträge seit 2009
vor 6 Jahren

Hi,

Form.AcceptButton zu setzen reicht alleine nicht aus (lies dir mal die Beschreibung genau durch), sondern du musst auch noch bei den Buttons Button.DialogResult auf OK bzw. Cancel setzen.

B
barclay Themenstarter:in
3 Beiträge seit 2017
vor 6 Jahren


>
zu setzen reicht alleine nicht aus (lies dir mal die Beschreibung genau durch), sondern du musst auch noch bei den Buttons
>
auf OK bzw. Cancel setzen.

Ah, tausend Dank, Cat, das war es in der Tat. RTFM an mich selbst.

Hier zur Vollständigkeit noch der gesamte Quellcode in Richtig:

class InputDialog
    {
        public void dialogFuerLogin(ref string strLogin, ref string strPasswort)
        {
            System.Drawing.Size size = new System.Drawing.Size(370, 200);
            Form inpBoxLogin = new Form();

            inpBoxLogin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            inpBoxLogin.ClientSize = size;
            inpBoxLogin.Text = "Login";

            System.Windows.Forms.Label lblLoginName = new Label();
            lblLoginName.AutoSize = true;
            lblLoginName.Location = new System.Drawing.Point(41, 72);
            lblLoginName.Name = "lblLoginName";
            lblLoginName.Size = new System.Drawing.Size(59, 13);
            lblLoginName.TabIndex = 2;
            lblLoginName.Text = "Loginname";

            System.Windows.Forms.Label lblPasswort = new Label();
            lblPasswort.AutoSize = true;
            lblPasswort.Location = new System.Drawing.Point(41, 124);
            lblPasswort.Name = "lblPasswort";
            lblPasswort.Size = new System.Drawing.Size(50, 13);
            lblPasswort.TabIndex = 2;
            lblPasswort.Text = "Passwort";

            System.Windows.Forms.Label lblAnmeldung = new Label();
            lblAnmeldung.AutoSize = true;
            lblAnmeldung.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblAnmeldung.Location = new System.Drawing.Point(40, 30);
            lblAnmeldung.Name = "lblAnmeldung";
            lblAnmeldung.Size = new System.Drawing.Size(99, 20);
            lblAnmeldung.TabIndex = 2;
            lblAnmeldung.Text = "Anmeldung";

            System.Windows.Forms.TextBox txtLoginName = new TextBox();
            txtLoginName.Location = new System.Drawing.Point(44, 88);
            txtLoginName.Name = "txtLogin";
            txtLoginName.Size = new System.Drawing.Size(164, 20);
            txtLoginName.TabIndex = 0;

            System.Windows.Forms.TextBox txtPasswort = new TextBox();
            txtPasswort.Location = new System.Drawing.Point(44, 140);
            txtPasswort.Name = "txtPasswort";
            txtPasswort.UseSystemPasswordChar = true;
            txtPasswort.Size = new System.Drawing.Size(164, 20);
            txtPasswort.TabIndex = 1;

            Button btnAnmelden = new Button();
            btnAnmelden.Location = new System.Drawing.Point(239, 88);
            btnAnmelden.Name = "btnAnmelden";
            btnAnmelden.Size = new System.Drawing.Size(91, 34);
            btnAnmelden.TabIndex = 3;
            btnAnmelden.Text = "Login";
            btnAnmelden.UseVisualStyleBackColor = true;
            btnAnmelden.DialogResult = DialogResult.OK;

            Button btnAbbrechen = new Button();
            btnAbbrechen.Location = new System.Drawing.Point(239, 126);
            btnAbbrechen.Name = "btnAbbrechen";
            btnAbbrechen.Size = new System.Drawing.Size(91, 34);
            btnAbbrechen.TabIndex = 4;
            btnAbbrechen.Text = "Login abbrechen";
            btnAbbrechen.UseVisualStyleBackColor = true;
            btnAbbrechen.DialogResult = DialogResult.Cancel;

            inpBoxLogin.Controls.Add(btnAnmelden);
            inpBoxLogin.Controls.Add(btnAbbrechen);
            inpBoxLogin.Controls.Add(txtLoginName);
            inpBoxLogin.Controls.Add(txtPasswort);
            inpBoxLogin.Controls.Add(lblLoginName);
            inpBoxLogin.Controls.Add(lblPasswort);
            inpBoxLogin.Controls.Add(lblAnmeldung);            
            inpBoxLogin.AcceptButton = btnAnmelden;
            inpBoxLogin.CancelButton = btnAbbrechen;
            if (inpBoxLogin.ShowDialog() == DialogResult.OK)
            {
                strLogin = txtLoginName.Text;
                strPasswort = txtPasswort.Text;
                inpBoxLogin.Dispose();
            }
            else
            {
                inpBoxLogin.Dispose();
            }
        }
    }
771 Beiträge seit 2009
vor 6 Jahren

Noch als Tipp: erstelle dir ein UserControl daraus.

F
10.010 Beiträge seit 2004
vor 6 Jahren

Und vergiss das Schlüsselwort ref, denn so wie du es benutzt ist es echt kontraproduktiv.

Erstelle Properties und benutze die so wie auch bei z.b. OpenFileDialog u.ä.

B
barclay Themenstarter:in
3 Beiträge seit 2017
vor 6 Jahren

Also da es sich um eine Abschlussarbeit in einer Höheren Berufsfachschule hadelt, werde ich ersteres jetzt wohl nicht mehr machen, da hab ich noch einiges andere zu tun und die Zeit ist nicht endlos. Letzteres werd ich aber beherzigen! Danke Euch beiden!