none
Referenzbild RRS feed

  • Frage

  • Hallo!

    Ich muss für meine Bachelorarbeit in Psychologie ein Programm schreiben, bei dem den Probanden Lebensmittel gezeigt werden, die sie nach dem gesundheitlichen Aspekt und nach dem geschmacklichen Aspekt bewerten sollen. Anschließend soll ein Referenzbild berechnet werden, welches in beidenDimensionen mit neutral bewertet wurde. In diesem Fall mit 4 (7 Punkteskala). Genau da komme ich nicht weiter!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.VisualBasic;
    
    namespace essen_prog
    {
        public partial class Form1 : Form
        {
            private List<Voting> Votings { get; set; }
            private Voting CurrentVoting { get; set; }
    
            public Form1()
            {
                InitializeComponent();
                this.Init();
            }
    
            public void Init()
            {
                this.Votings = new List<Voting>();
                //this.Votings.Add( new Voting(1, "Bild1", Properties.Resources.100Grand));
                this.Votings.Add( new Voting(1, "Bild1", Properties.Resources.banana));
                this.Votings.Add( new Voting(2, "Bild2", Properties.Resources.blueberryyogart));
                this.Votings.Add( new Voting(3, "Bild3", Properties.Resources.brocollincauliflower));
                this.Votings.Add( new Voting(4, "Bild4", Properties.Resources.butterfinger));
                this.Votings.Add( new Voting(5, "Bild5", Properties.Resources.carrots));
                this.Votings.Add( new Voting(6, "Bild6", Properties.Resources.cellery));
                this.Votings.Add( new Voting(7, "Bild7", Properties.Resources.cherryicecream));
                this.Votings.Add( new Voting(8, "Bild8", Properties.Resources.ChipsAhoy));
                this.Votings.Add( new Voting(9, "Bild9", Properties.Resources.cookiencream));
                this.Votings.Add( new Voting(10, "Bild10", Properties.Resources.cookies));
                this.Votings.Add( new Voting(11, "Bild11", Properties.Resources.cranberries));
                this.Votings.Add( new Voting(12, "Bild12", Properties.Resources.Doritosranch));
                this.Votings.Add( new Voting(13, "Bild13", Properties.Resources.FamousAmos));
                this.Votings.Add( new Voting(14, "Bild14", Properties.Resources.ffraspsorbet));
                this.Votings.Add( new Voting(15, "Bild15", Properties.Resources.FlamingCheetos));
                this.Votings.Add( new Voting(16, "Bild16",  Properties.Resources.frozenyogart));
                this.Votings.Add( new Voting(17, "Bild17", Properties.Resources.Ghiradelli));
                this.Votings.Add( new Voting(18, "Bild18", Properties.Resources.grannysmith));
                this.Votings.Add( new Voting(19, "Bild19", Properties.Resources.HoHo));
                this.Votings.Add( new Voting(20, "Bild20", Properties.Resources.icecreamsandwich));
                this.Votings.Add( new Voting(21, "Bild21", Properties.Resources.keeblerrainbow));
                this.Votings.Add( new Voting(22, "Bild22", Properties.Resources.KitKat));
                this.Votings.Add( new Voting(23, "Bild23", Properties.Resources.laysclassic));
                this.Votings.Add( new Voting(24, "Bild24", Properties.Resources.Lindt));
                this.Votings.Add( new Voting(25, "Bild25", Properties.Resources.mixedyogart));
                this.Votings.Add( new Voting(26, "Bild26", Properties.Resources.MrsFields));
                this.Votings.Add( new Voting(27, "Bild27", Properties.Resources.orange));
                this.Votings.Add( new Voting(28, "Bild28", Properties.Resources.orangejello));
                this.Votings.Add( new Voting(29, "Bild29", Properties.Resources.Oreos));
                this.Votings.Add( new Voting(30, "Bild30", Properties.Resources.raisins));
                this.Votings.Add( new Voting(31, "Bild31", Properties.Resources.reddelicious));
                this.Votings.Add( new Voting(32, "Bild32", Properties.Resources.redgrapes));
                this.Votings.Add( new Voting(33, "Bild33", Properties.Resources.Reeses));
                this.Votings.Add( new Voting(34, "Bild34", Properties.Resources.RiceKrispyTreat));
                this.Votings.Add( new Voting(35, "Bild35", Properties.Resources.ruffles));
                this.Votings.Add( new Voting(36, "Bild36", Properties.Resources.sbcrackers));
                this.Votings.Add( new Voting(37, "Bild37", Properties.Resources.sbdietbar));
                this.Votings.Add( new Voting(38, "Bild38", Properties.Resources.slimfastC));
                this.Votings.Add( new Voting(39, "Bild39", Properties.Resources.slimfastV));
                this.Votings.Add( new Voting(40, "Bild40", Properties.Resources.specialKbar));
                this.Votings.Add( new Voting(41, "Bild41", Properties.Resources.strawberries));
                this.Votings.Add( new Voting(42, "Bild42", Properties.Resources.strussel));
                this.Votings.Add( new Voting(43, "Bild43", Properties.Resources.uToberlorone));
                this.Votings.Add( new Voting(44, "Bild44", Properties.Resources.uTwix));
                this.Votings.Add( new Voting(45, "Bild45", Properties.Resources.wheatcrisps));
                this.Votings.Add( new Voting(46, "Bild46", Properties.Resources.whitegrapes));
                this.Votings.Add( new Voting(47, "Bild47", Properties.Resources.wwbrownie));
                this.Votings.Add( new Voting(48, "Bild48", Properties.Resources.wwmuffin));
    
    
    
                this.LoadVoting(1);
            }
           
            public void LoadVoting( int index )
            {
                 if( index == 0 || index > this.Votings.Count )
                {
                   index = 1;
                }
            
    
                this.CurrentVoting = this.Votings.Find(f=>f.Index == index);
    
                this.label1.Text = this.CurrentVoting.Name;
                this.pictureBox1.Image = this.CurrentVoting.Image;
                this.label2.Text = this.CurrentVoting.ValueA.ToString();
                this.label3.Text = this.CurrentVoting.ValueB.ToString();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int vpnr = Convert.ToInt32(Interaction.InputBox("Versuchpersonnummer", "Nummer", "Nummer"));
    
                this.FormBorderStyle = FormBorderStyle.None; //Rahmen ausblenden
                this.TopMost = true; // Formular in Vordergrund
                this.Bounds = Screen.PrimaryScreen.Bounds; // Formulargröße auf Größe des Bildschirms festlegen
    
                this.BackColor = System.Drawing.Color.Black;
    
    
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\Daten.csv", true))
                {
                    file.Write("\n"+Convert.ToString(vpnr) + ";");
                }
    
                //System.IO.File.WriteAllText(@"C:\Test\Daten.csv",Convert.ToString(vpnr)+";");
            }
    
           
            private void button1_Click(object sender, EventArgs e)
            {
                this.LoadVoting(this.CurrentVoting.Index + 1);
               
                if (radioButton1.Checked == true)
                {
                    this.CurrentVoting.ValueA = 1; 
                    radioButton1.Checked = false;
                }
    
    
                if (radioButton2.Checked == true)
                {
                    this.CurrentVoting.ValueA = 2;
                    radioButton2.Checked = false;
                }
    
    
                if (radioButton3.Checked == true)
                {
                    this.CurrentVoting.ValueA = 3;
                    radioButton3.Checked = false;
                }
    
    
                if (radioButton4.Checked == true)
                {
                    this.CurrentVoting.ValueA = 4;
                    radioButton4.Checked = false;
                }
    
    
    
                if (radioButton5.Checked == true)
                {
                    this.CurrentVoting.ValueA = 5;
                    radioButton5.Checked = false;
                }
    
    
    
                if (radioButton6.Checked == true)
                {
                    this.CurrentVoting.ValueA = 6;
                    radioButton6.Checked = false;
                }
    
    
    
                if (radioButton7.Checked == true)
                {
                    this.CurrentVoting.ValueA = 7;
                    radioButton7.Checked = false;
                }
    
    
                if (radioButton8.Checked == true)
                {
                    this.CurrentVoting.ValueB = 1;
                    radioButton8.Checked = false;
                }
    
                
                if (radioButton9.Checked == true)
                {
                    this.CurrentVoting.ValueB = 2;
                    radioButton9.Checked = false;
                }
    
               
    
                if (radioButton10.Checked == true)
                {
                    this.CurrentVoting.ValueB = 3;
                    radioButton10.Checked = false;
                }
    
    
                if (radioButton11.Checked == true)
                {
                    this.CurrentVoting.ValueB = 4;
                    radioButton11.Checked = false;
                }
    
                if (radioButton12.Checked == true)
                {
                    this.CurrentVoting.ValueB = 5;
                    radioButton12.Checked = false;
                }
    
                if (radioButton13.Checked == true)
                {
                    this.CurrentVoting.ValueB = 6;
                    radioButton13.Checked = false;
                }
    
    
                if (radioButton14.Checked == true)
                {
                    this.CurrentVoting.ValueB = 7;
                    radioButton14.Checked = false;
                }
    
               
    
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\Daten.csv", true))
            {
                file.Write((this.CurrentVoting.ValueA.ToString()) + ";" + (this.CurrentVoting.ValueB.ToString()) + ";");
            }
        }
    
            
    
           private void button2_Click(object sender, EventArgs e)
            {
    
    
                // this.CurrentVoting = this.Votings.Find(f => f.Index == index);
    
                int referenz; // In den folgenden Zeilen soll das Referenzbild berechnet werden:
                for (this.CurrentVoting.Index = 1; this.CurrentVoting.Index < this.Votings.Count; this.CurrentVoting.Index++)
                {
                    if ((this.CurrentVoting.ValueA == 4) && (this.CurrentVoting.ValueB == 4))
                    {
                        referenz = this.CurrentVoting.Index;
                        label6.Text = Convert.ToString(referenz);
                    }
                  
                }
            }
        }
        public class Voting
        {
            public int Index { get; set; }
            public string Name { get; set; }
            public Image Image { get; set; }
            public int ValueA { get; set; }
            public int ValueB { get; set; }
    
            public Voting()
                :this(0,string.Empty, null, 0, 0)
            {
            }
            
            public Voting(int index, string name, Image image)
                : this(index, name, image, 0, 0)
            {
            }
    
            public Voting(int index, string name, Image image, int ValueA, int ValueB)
            {
                this.Index = index;
                this.Name = name;
                this.Image = image;
                this.ValueA = ValueA;
                this.ValueB = ValueB;
            }
        }
    }
    

    Viele Grüße

    elmo

    Mittwoch, 10. Dezember 2014 11:15

Antworten

Alle Antworten

  • Hallo elmote,

    Du solltest als Laufvariable eine lokale Variable benutzen oder noch besser ein foreach [1]. Zum Beispiel folgendermaßen:

    private void button2_Click(object sender, EventArgs e)
    {
        var referenz = findeReferenz();
        // ...
    } 
    
    private Voting findeReferenz()
    {
        foreach(Voting v in this.Votings)
        {
            if ((v.ValueA == 4) && (v.ValueB == 4))
            {
                label6.Text = Convert.ToString(v.Index);
                return v;
            }
        }
        
        // Keine passende Referenz gefunden:
        return null;
    }

    [1] http://msdn.microsoft.com/de-de/library/ttw7t8t6.aspx

    Viele Grüße
    Tobias K.
    Entwickler-Hotline für MSDN Online Deutschland

    Disclaimer:
    Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
    Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Schnelle & kompetente Hilfe für Entwickler: kostenfrei!

    Es gelten für die MSDN-Entwickler-Hotline und dieses Posting diese Nutzungsbedingungen , Hinweise zu Markenzeichen, Informationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die MSDN-Entwickler-Hotline .

    Mittwoch, 10. Dezember 2014 14:06
  • Hallo!

    Vielen Dank, das hat schonmal funktioniert. Ich versteh den Quellcode zwar nicht ganz, aber hauptsache es klappt.

    Jetzt würde ich gerne einen Timer installieren, der den Probanden 5 Sekunden Zeit geben soll zum antworten. Das funktioniert irgendwie nicht, weil schon beim kompilieren Fehler angezeigt werden:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.VisualBasic;
    using System.IO;
    using System.Timers;
     
    
    
    namespace essen_prog
    {
        
        public partial class Form1 : Form
        {   
            private List<Voting> Votings { get; set; }
            private Voting CurrentVoting { get; set; }
    
            public Form1()
            {
                InitializeComponent();
                this.Init();
            }
    
           // Hier soll der Timer initialisiert werden Er soll nach 5 Sekunden Alarm geben, dass die Zeit abgelaufen ist.
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval=5000;
            aTimer.Enabled=true;      
    
            private static void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                label6.Text = "Zeit ist abgelaufen";
            }
    
            // Leider funktioniert es so nicht.
    
    
            public void Init()
            {
                this.Votings = new List<Voting>();
                //this.Votings.Add( new Voting(1, "Bild1", Properties.Resources.100Grand));
                this.Votings.Add(new Voting(1, "Bild1", Properties.Resources.banana));
                this.Votings.Add(new Voting(2, "Bild2", Properties.Resources.blueberryyogart));
                this.Votings.Add(new Voting(3, "Bild3", Properties.Resources.brocollincauliflower));
                this.Votings.Add(new Voting(4, "Bild4", Properties.Resources.butterfinger));
                this.Votings.Add(new Voting(5, "Bild5", Properties.Resources.carrots));
                this.Votings.Add(new Voting(6, "Bild6", Properties.Resources.cellery));
                this.Votings.Add(new Voting(7, "Bild7", Properties.Resources.cherryicecream));
                this.Votings.Add(new Voting(8, "Bild8", Properties.Resources.ChipsAhoy));
                this.Votings.Add(new Voting(9, "Bild9", Properties.Resources.cookiencream));
                this.Votings.Add(new Voting(10, "Bild10", Properties.Resources.cookies));
                this.Votings.Add(new Voting(11, "Bild11", Properties.Resources.cranberries));
                this.Votings.Add(new Voting(12, "Bild12", Properties.Resources.Doritosranch));
                this.Votings.Add(new Voting(13, "Bild13", Properties.Resources.FamousAmos));
                this.Votings.Add(new Voting(14, "Bild14", Properties.Resources.ffraspsorbet));
                this.Votings.Add(new Voting(15, "Bild15", Properties.Resources.FlamingCheetos));
                this.Votings.Add(new Voting(16, "Bild16", Properties.Resources.frozenyogart));
                this.Votings.Add(new Voting(17, "Bild17", Properties.Resources.Ghiradelli));
                this.Votings.Add(new Voting(18, "Bild18", Properties.Resources.grannysmith));
                this.Votings.Add(new Voting(19, "Bild19", Properties.Resources.HoHo));
                this.Votings.Add(new Voting(20, "Bild20", Properties.Resources.icecreamsandwich));
                this.Votings.Add(new Voting(21, "Bild21", Properties.Resources.keeblerrainbow));
                this.Votings.Add(new Voting(22, "Bild22", Properties.Resources.KitKat));
                this.Votings.Add(new Voting(23, "Bild23", Properties.Resources.laysclassic));
                this.Votings.Add(new Voting(24, "Bild24", Properties.Resources.Lindt));
                this.Votings.Add(new Voting(25, "Bild25", Properties.Resources.mixedyogart));
                this.Votings.Add(new Voting(26, "Bild26", Properties.Resources.MrsFields));
                this.Votings.Add(new Voting(27, "Bild27", Properties.Resources.orange));
                this.Votings.Add(new Voting(28, "Bild28", Properties.Resources.orangejello));
                this.Votings.Add(new Voting(29, "Bild29", Properties.Resources.Oreos));
                this.Votings.Add(new Voting(30, "Bild30", Properties.Resources.raisins));
                this.Votings.Add(new Voting(31, "Bild31", Properties.Resources.reddelicious));
                this.Votings.Add(new Voting(32, "Bild32", Properties.Resources.redgrapes));
                this.Votings.Add(new Voting(33, "Bild33", Properties.Resources.Reeses));
                this.Votings.Add(new Voting(34, "Bild34", Properties.Resources.RiceKrispyTreat));
                this.Votings.Add(new Voting(35, "Bild35", Properties.Resources.ruffles));
                this.Votings.Add(new Voting(36, "Bild36", Properties.Resources.sbcrackers));
                this.Votings.Add(new Voting(37, "Bild37", Properties.Resources.sbdietbar));
                this.Votings.Add(new Voting(38, "Bild38", Properties.Resources.slimfastC));
                this.Votings.Add(new Voting(39, "Bild39", Properties.Resources.slimfastV));
                this.Votings.Add(new Voting(40, "Bild40", Properties.Resources.specialKbar));
                this.Votings.Add(new Voting(41, "Bild41", Properties.Resources.strawberries));
                this.Votings.Add(new Voting(42, "Bild42", Properties.Resources.strussel));
                this.Votings.Add(new Voting(43, "Bild43", Properties.Resources.uToberlorone));
                this.Votings.Add(new Voting(44, "Bild44", Properties.Resources.uTwix));
                this.Votings.Add(new Voting(45, "Bild45", Properties.Resources.wheatcrisps));
                this.Votings.Add(new Voting(46, "Bild46", Properties.Resources.whitegrapes));
                this.Votings.Add(new Voting(47, "Bild47", Properties.Resources.wwbrownie));
                this.Votings.Add(new Voting(48, "Bild48", Properties.Resources.wwmuffin));
    
    
    
                this.LoadVoting(1);
            }
    
            byte part = 1;
    
            public void LoadVoting(int index)
            {
                if (index == 0 || index > this.Votings.Count)
                {
                    index = 1;
                    MessageBox.Show("Fertig", "Ende");
                    label5.Text = "Geschmack";
                    part = 2;
                }
    
    
                this.CurrentVoting = this.Votings.Find(f => f.Index == index);
    
                this.label1.Text = this.CurrentVoting.Name;
                this.pictureBox1.Image = this.CurrentVoting.Image;
                this.label2.Text = this.CurrentVoting.ValueA.ToString();
                this.label3.Text = this.CurrentVoting.ValueB.ToString();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int vpnr = Convert.ToInt32(Interaction.InputBox("Versuchpersonnummer", "Nummer", "Nummer"));
    
                this.FormBorderStyle = FormBorderStyle.None; //Rahmen ausblenden
                this.TopMost = true; // Formular in Vordergrund
                this.Bounds = Screen.PrimaryScreen.Bounds; // Formulargröße auf Größe des Bildschirms festlegen
    
                this.BackColor = System.Drawing.Color.Black;
    
                if (File.Exists(@"C:\test\Daten.csv"))
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\Daten.csv",true))
                    file.Write("\n" + Convert.ToString(vpnr) + ";");
                }
                else 
                {
                   System.IO.File.WriteAllText(@"C:\Test\Daten.csv",Convert.ToString(vpnr)+";");
                }
    
                
            }
           
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.LoadVoting(this.CurrentVoting.Index + 1);
    
                if (part == 1)
                {
                    if (radioButton1.Checked == true)
                    {
                        this.CurrentVoting.ValueA = 1;
                        radioButton1.Checked = false;
                    }
    
    
                    if (radioButton2.Checked == true)
                    {
                        this.CurrentVoting.ValueA = 2;
                        radioButton2.Checked = false;
                    }
    
    
                    if (radioButton3.Checked == true)
                    {
                        this.CurrentVoting.ValueA = 3;
                        radioButton3.Checked = false;
                    }
    
    
                    if (radioButton4.Checked == true)
                    {
                        this.CurrentVoting.ValueA = 4;
                        radioButton4.Checked = false;
                    }
    
                       
                    if (radioButton5.Checked == true)
                     {
                       this.CurrentVoting.ValueA = 5;
                      radioButton5.Checked = false;
                    }
    
    
    
                    if (radioButton6.Checked == true)
                    {
                        this.CurrentVoting.ValueA = 6;
                        radioButton6.Checked = false;
                    }
     
    
    
                      if (radioButton7.Checked == true)
                      {
                         this.CurrentVoting.ValueA = 7;
                         radioButton7.Checked = false;
                      }
                }
    
                if (part == 2)
                {
                    if (radioButton1.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 1;
                        radioButton1.Checked = false;
                    }
    
    
                    if (radioButton2.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 2;
                        radioButton2.Checked = false;
                    }
    
    
    
                    if (radioButton3.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 3;
                        radioButton3.Checked = false;
                    }
    
    
                    if (radioButton4.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 4;
                        radioButton4.Checked = false;
                    }
    
                    if (radioButton5.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 5;
                        radioButton5.Checked = false;
                    }
    
                    if (radioButton6.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 6;
                        radioButton6.Checked = false;
                    }
    
    
                    if (radioButton7.Checked == true)
                    {
                        this.CurrentVoting.ValueB = 7;
                        radioButton7.Checked = false;
                    }
                }
    
    
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\Daten.csv", true))
                {
                    file.Write((this.CurrentVoting.ValueA.ToString()) + ";" + (this.CurrentVoting.ValueB.ToString()) + ";");
                }
            }
    
    
    
            private void button2_Click(object sender, EventArgs e)
            {
    
                var referenz = findeReferenz();
                // this.CurrentVoting = this.Votings.Find(f => f.Index == index);
    
                /* int referenz; // In den folgenden Zeilen soll das Referenzbild berechnet werden:
                for (this.CurrentVoting.Index = 1; this.CurrentVoting.Index < this.Votings.Count; this.CurrentVoting.Index++)
                {
                    if ((this.CurrentVoting.ValueA == 4) && (this.CurrentVoting.ValueB == 4))
                    {
                        referenz = this.CurrentVoting.Index;
                        label6.Text = Convert.ToString(referenz);
                    }
                  
                } */
            }
    
            private Voting findeReferenz()
            {
                foreach (Voting v in this.Votings)
                {
                    if ((v.ValueA == 4) && (v.ValueB == 4))
                    {
                        label6.Text = Convert.ToString(v.Index);
                        return v;
                    }
                } 
            
            return null;
            }
    
    
            public class Voting
            {
                public int Index { get; set; }
                public string Name { get; set; }
                public Image Image { get; set; }
                public int ValueA { get; set; }
                public int ValueB { get; set; }
    
                public Voting()
                    : this(0, string.Empty, null, 0, 0)
                {
                }
    
                public Voting(int index, string name, Image image)
                    : this(index, name, image, 0, 0)
                {
                }
    
                public Voting(int index, string name, Image image, int ValueA, int ValueB)
                {
                    this.Index = index;
                    this.Name = name;
                    this.Image = image;
                    this.ValueA = ValueA;
                    this.ValueB = ValueB;
                }
            }
        }
        }
    

    Viele Grüße

    elmo

    Freitag, 19. Dezember 2014 10:27