Laden...

Checkbox-Array | Boxen prüfen u.ä.

Erstellt von klofisch vor 17 Jahren Letzter Beitrag vor 17 Jahren 7.290 Views
K
klofisch Themenstarter:in
24 Beiträge seit 2006
vor 17 Jahren
Checkbox-Array | Boxen prüfen u.ä.

Hallo,

ich möchte zwei Arrays von Checkboxen erstellen und die boxen auslesen.

Prinzipell sollen die Ergebnisse der Abfrage erstmal in der Konsole ausgegeben werden.

Problem 1:

Bekomme immer diese Fehlermeldung

"System.Windows.Forms.CheckBox" ist ein(e) "Typ", wird aber wie ein(e) "Variable" verwendet.


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            int[] boolarray1 = new int[6];
            int[] boolarray2 = new int[6];

            for (int i = 0; i < 7; i++)
            {
                CheckBox[i].CheckedChanged();
                if (CheckBox[i] = true)
                    boolarray1[i] = 1;
            }
            for (int i = 6; i < 13; i++)
            {
                CheckBox[i].CheckedChanged();
                if (CheckBox[i] = true)
                    boolarray2[i] = 1;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



Was ist falsch? Worüber soll ich mir den Kopf zerbrechen?

Bis dann
Peter

49.485 Beiträge seit 2005
vor 17 Jahren

Hallo klofisch,

ich möchte zwei Arrays von Checkboxen erstellen

das hast du aber nicht getan.

Siehe auch Syntaxfehler selbst lösen (Compilerfehlermeldungen)

Worüber soll ich mir den Kopf zerbrechen?

Warum du ein int-Array boolarray nennst. 🙂

herbivore

S
45 Beiträge seit 2006
vor 17 Jahren

CheckBox_ = true
Was willst du hier überprüfen???
ich geh mal davon aus, dass du überprüfen willst ob sie markiert ist oder nich...

das würde dann so aussehen:

CheckBox_.checked = true

Dann:

CheckBox_
Wo wird das definiert???

und zu guter letzt (verbessert mich wenn ich falsch liege!!!!) in einer Console gibt es keine Checkboxen...

mfg hannes

edit: Frage an alle: sollte man in ner if-abfrage nicht immer "==" verwender???

T
243 Beiträge seit 2006
vor 17 Jahren

Original von sh4rk

CheckBox_ = true
Was willst du hier überprüfen???
ich geh mal davon aus, dass du überprüfen willst ob sie markiert ist oder nich...

das würde dann so aussehen:

CheckBox_.checked = true

Eigentlich würde es sogar so aussehen:

CheckBox[i].checked == true

besser noch:

CheckBox[i].checked
S
45 Beiträge seit 2006
vor 17 Jahren

eben aber mit dem "==" war ich mir nicht sicher!!!

darum die frage am schluss...

aber danke...

mfg hannes

K
klofisch Themenstarter:in
24 Beiträge seit 2006
vor 17 Jahren

mmh also ich hab den Code etwas überarbeitet. Ja es ist eigentlich ein Windows.Form mit 12 Checkboxen und einem Button.

der neue Code und weiter unten das Formular. Funzt leider immer nich garnicht 🙂. Wie kann ich die boxen aus dem Formular auslesen?

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            CheckBox[] chkarray1 = new CheckBox[checkBox()];
            CheckBox[] chkarray2 = new CheckBox[checkBox()];

            for (int i = 0; i < 7; i++)
            {
                if (chkarray1[i].Checked){
                    Console.WriteLine("CheckBox"+chkarray1[i]+": checked");
                } else {
                    Console.WriteLine("CheckBox"+chkarray1[i]+": not checked");
                }
            }
            for (int i = 7; i < 13; i++)
            {
                if (chkarray2[i].Checked)
                {
                     Console.WriteLine("CheckBox" + chkarray2[i] + ": checked");
                 } else {
                     Console.WriteLine("CheckBox" + chkarray2[i] + ": not checked");
                     }
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private static object checkBox()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}


Windows.Form

 

namespace WindowsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox3 = new System.Windows.Forms.CheckBox();
            this.checkBox4 = new System.Windows.Forms.CheckBox();
            this.checkBox5 = new System.Windows.Forms.CheckBox();
            this.checkBox6 = new System.Windows.Forms.CheckBox();
            this.checkBox7 = new System.Windows.Forms.CheckBox();
            this.checkBox8 = new System.Windows.Forms.CheckBox();
            this.checkBox9 = new System.Windows.Forms.CheckBox();
            this.checkBox10 = new System.Windows.Forms.CheckBox();
            this.checkBox11 = new System.Windows.Forms.CheckBox();
            this.button1 = new System.Windows.Forms.Button();
            this.checkBox12 = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(12, 12);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(80, 17);
            this.checkBox1.TabIndex = 0;
            this.checkBox1.Text = "checkBox1";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // checkBox2
            // 
            this.checkBox2.AutoSize = true;
            this.checkBox2.Location = new System.Drawing.Point(98, 12);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(80, 17);
            this.checkBox2.TabIndex = 1;
            this.checkBox2.Text = "checkBox2";
            this.checkBox2.UseVisualStyleBackColor = true;
            // 
            // checkBox3
            // 
            this.checkBox3.AutoSize = true;
            this.checkBox3.Location = new System.Drawing.Point(184, 12);
            this.checkBox3.Name = "checkBox3";
            this.checkBox3.Size = new System.Drawing.Size(80, 17);
            this.checkBox3.TabIndex = 2;
            this.checkBox3.Text = "checkBox3";
            this.checkBox3.UseVisualStyleBackColor = true;
            // 
            // checkBox4
            // 
            this.checkBox4.AutoSize = true;
            this.checkBox4.Location = new System.Drawing.Point(270, 12);
            this.checkBox4.Name = "checkBox4";
            this.checkBox4.Size = new System.Drawing.Size(80, 17);
            this.checkBox4.TabIndex = 3;
            this.checkBox4.Text = "checkBox4";
            this.checkBox4.UseVisualStyleBackColor = true;
            // 
            // checkBox5
            // 
            this.checkBox5.AutoSize = true;
            this.checkBox5.Location = new System.Drawing.Point(356, 12);
            this.checkBox5.Name = "checkBox5";
            this.checkBox5.Size = new System.Drawing.Size(80, 17);
            this.checkBox5.TabIndex = 4;
            this.checkBox5.Text = "checkBox5";
            this.checkBox5.UseVisualStyleBackColor = true;
            // 
            // checkBox6
            // 
            this.checkBox6.AutoSize = true;
            this.checkBox6.Location = new System.Drawing.Point(442, 12);
            this.checkBox6.Name = "checkBox6";
            this.checkBox6.Size = new System.Drawing.Size(80, 17);
            this.checkBox6.TabIndex = 5;
            this.checkBox6.Text = "checkBox6";
            this.checkBox6.UseVisualStyleBackColor = true;
            // 
            // checkBox7
            // 
            this.checkBox7.AutoSize = true;
            this.checkBox7.Location = new System.Drawing.Point(12, 67);
            this.checkBox7.Name = "checkBox7";
            this.checkBox7.Size = new System.Drawing.Size(80, 17);
            this.checkBox7.TabIndex = 6;
            this.checkBox7.Text = "checkBox7";
            this.checkBox7.UseVisualStyleBackColor = true;
            // 
            // checkBox8
            // 
            this.checkBox8.AutoSize = true;
            this.checkBox8.Location = new System.Drawing.Point(98, 67);
            this.checkBox8.Name = "checkBox8";
            this.checkBox8.Size = new System.Drawing.Size(80, 17);
            this.checkBox8.TabIndex = 7;
            this.checkBox8.Text = "checkBox8";
            this.checkBox8.UseVisualStyleBackColor = true;
            // 
            // checkBox9
            // 
            this.checkBox9.AutoSize = true;
            this.checkBox9.Location = new System.Drawing.Point(184, 67);
            this.checkBox9.Name = "checkBox9";
            this.checkBox9.Size = new System.Drawing.Size(80, 17);
            this.checkBox9.TabIndex = 8;
            this.checkBox9.Text = "checkBox9";
            this.checkBox9.UseVisualStyleBackColor = true;
            // 
            // checkBox10
            // 
            this.checkBox10.AutoSize = true;
            this.checkBox10.Location = new System.Drawing.Point(270, 67);
            this.checkBox10.Name = "checkBox10";
            this.checkBox10.Size = new System.Drawing.Size(86, 17);
            this.checkBox10.TabIndex = 9;
            this.checkBox10.Text = "checkBox10";
            this.checkBox10.UseVisualStyleBackColor = true;
            // 
            // checkBox11
            // 
            this.checkBox11.AutoSize = true;
            this.checkBox11.Location = new System.Drawing.Point(356, 67);
            this.checkBox11.Name = "checkBox11";
            this.checkBox11.Size = new System.Drawing.Size(86, 17);
            this.checkBox11.TabIndex = 10;
            this.checkBox11.Text = "checkBox11";
            this.checkBox11.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(210, 213);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 11;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // checkBox12
            // 
            this.checkBox12.AutoSize = true;
            this.checkBox12.Location = new System.Drawing.Point(449, 67);
            this.checkBox12.Name = "checkBox12";
            this.checkBox12.Size = new System.Drawing.Size(86, 17);
            this.checkBox12.TabIndex = 12;
            this.checkBox12.Text = "checkBox12";
            this.checkBox12.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(556, 273);
            this.Controls.Add(this.checkBox12);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.checkBox11);
            this.Controls.Add(this.checkBox10);
            this.Controls.Add(this.checkBox9);
            this.Controls.Add(this.checkBox8);
            this.Controls.Add(this.checkBox7);
            this.Controls.Add(this.checkBox6);
            this.Controls.Add(this.checkBox5);
            this.Controls.Add(this.checkBox4);
            this.Controls.Add(this.checkBox3);
            this.Controls.Add(this.checkBox2);
            this.Controls.Add(this.checkBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.CheckBox checkBox2;
        private System.Windows.Forms.CheckBox checkBox3;
        private System.Windows.Forms.CheckBox checkBox4;
        private System.Windows.Forms.CheckBox checkBox5;
        private System.Windows.Forms.CheckBox checkBox6;
        private System.Windows.Forms.CheckBox checkBox7;
        private System.Windows.Forms.CheckBox checkBox8;
        private System.Windows.Forms.CheckBox checkBox9;
        private System.Windows.Forms.CheckBox checkBox10;
        private System.Windows.Forms.CheckBox checkBox11;
        private System.Windows.Forms.CheckBox checkBox12;
        private System.Windows.Forms.Button button1;

    }
}




N
750 Beiträge seit 2004
vor 17 Jahren

was willst du überhaupt machen ??
checkboxen auslesen ???
vor dem öffnen deines formulars, oder auf knopfdruck, ..... ????

mir kommt das ein wenig "durcheinander" vor.....

was bekommst du für fehlermeldungen ???
sollten (nach einem schnellem code überfliegen) die ein oder andere sein.... 😉

?( wer suchet, der findet auch! :]

K
klofisch Themenstarter:in
24 Beiträge seit 2006
vor 17 Jahren

Ja es soll auf Knopfdruck der Status der einzelnen Felder ausgelesen und auf der Konsole ausgegeben werden.


CheckBox[] chkarray1 = new CheckBox[];
CheckBox[] chkarray2 = new CheckBox[];

Fehler hier: Für die Arrayerstellung ist eine Arraygröße oder ein Arrayinitialisierer erforderlich. -> Also hier müsste ich zumindest 6 angeben, da es sich um jeweils 6 Elemente handelt. Aber ist das nötig wenn ich dann später eh per FOR-Schleife immer 6 auszähle? 🤔

hier


if (chkarray1[i].Checked)

Sagt der Compiler, dass ich etwas mit "new" initialisieren soll.

N
750 Beiträge seit 2004
vor 17 Jahren

ich denke mal du solltest dir ein buch zu gemüte führen (nicht böse gemeint!)

?( wer suchet, der findet auch! :]

49.485 Beiträge seit 2005
vor 17 Jahren

Hallo klofisch,

ich muss mich nils anschließen. Das sicher sehr sinnvoll. Viele Fragen würden sich dann erübrigen.

Fehler hier: Für die Arrayerstellung ist eine Arraygröße oder ein Arrayinitialisierer erforderlich. -> Also hier müsste ich zumindest 6 angeben, da es sich um jeweils 6 Elemente handelt. Aber ist das nötig wenn ich dann später eh per FOR-Schleife immer 6 auszähle?

ja!

herbivore