Wątek przeniesiony 2017-06-20 11:47 z C# i .NET przez ŁF.

Szybka Średnia

0

Mam problem. Otóż mój picturebox tworzony w voidzie picturebox() zmienia cały czas swoją pozycję y o pewną wartość i nie wiem dlaczego. Oto kod obu form:

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;

namespace Nowe
{
    public partial class Form1 : Form
    {
        public static int xt = 0;
        public static string nazwa = "";
        public static Double sredniajeden = 0;
        public static Double sredniadwa = 0;
        public static Double sredniatrzy = 0;
        public static Double sredniacztery = 0;
        public static Double sredniapiec = 0;
        public static Double sredniaszesc = 0;
        public static bool nowebool = false;
        public static int x = 0;
        public static int y = 0;
        public static int noweo = 0;
        public static int nowes = 0;
        public static int sumajeden = 0;
        public static int iloscjeden = 0;
        public static int sumadwa = 0;
        public static int iloscdwa = 0;
        public static int sumatrzy = 0;
        public static int ilosctrzy = 0;
        public static int sumacztery = 0;
        public static int ilosccztery = 0;
        public static int sumajpiec = 0;
        public static int iloscpiec = 0;
        public static int sumaszesc = 0;
        public static int iloscszesc = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var myForm = new Form2();
            myForm.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (nowebool)
            {
                nowy();
                nowebool = false;
            }


        }
       
        public void picturebox()
        {
            int tx = x - 20;
            //if (x != 0)
            //{
            //    tx = x - 20;
            //}
            PictureBox p = new PictureBox();
            p.Location = new Point(tx, 0);
            p.Width = 40;
            p.Height = 100;
            panel1.Controls.Add(p);
            panel1.Show();
            testujemy(nazwa, p);
        }
        private void nowy()
        {

            MaskedTextBox a = new MaskedTextBox();
            a.Width = 20;
            a.Location = new Point(x, 0);
            a.ReadOnly = true;
            panel2.Controls.Add(a);
            panel2.Show();
            picturebox();
            x = x + 20;
            if (nowes == 1)
            {
                a.ForeColor = Color.Yellow;
            }
            if (nowes == 2)
            {
                a.ForeColor = Color.Black;
            }
            if (nowes == 3)
            {
                a.ForeColor = Color.Blue;
            }
            if (nowes == 4)
            {
                a.ForeColor = Color.Green;
            }
            if (nowes == 5)
            {
                a.ForeColor = Color.Red;
            }
            if (nowes == 6)
            {
                a.ForeColor = Color.Pink;
            }
            a.Text = noweo.ToString();
            noweo = 0;
            nowes = 0;
            int jd = 1;
            int dd = 2;
            int td = 3;
            int cd = 4;
            int pd = 5;
            int sd = 6;
            if (sumajeden != 0)
            {
                Double sredniajeden_gora = sumajeden * jd;
                Double sredniajeden_dol = iloscjeden * jd;
                sredniajeden = sredniajeden_gora / sredniajeden_dol;
            }
            if (sumadwa != 0)
            {
                Double sredniadwa_gora = sumadwa * dd;
                Double sredniadwa_dol = iloscdwa * dd;
                sredniadwa = sredniadwa_gora / sredniadwa_dol;
            }
            if (sumatrzy != 0)
            {
                Double sredniatrzy_gora = sumatrzy * td;
                Double sredniatrzy_dol = ilosctrzy * td;
                sredniatrzy = sredniatrzy_gora / sredniatrzy_dol;
            }
            if (sumacztery != 0)
            {
                Double sredniacztery_gora = sumacztery * cd;
                Double sredniacztery_dol = ilosccztery * cd;
                sredniacztery = sredniacztery_gora / sredniacztery_dol;
            }
            if (sumajpiec != 0)
            {
                Double sredniapiec_gora = sumajpiec * pd;
                Double sredniapiec_dol = iloscpiec * pd;
                sredniapiec = sredniapiec_gora / sredniapiec_dol;
            }
            if (sumaszesc != 0)
            {
                Double sredniaszesc_gora = sumaszesc * sd;
                Double sredniaszesc_dol = iloscszesc * sd;
                sredniaszesc = sredniaszesc_gora / sredniaszesc_dol;
            }
            int ilosc = iloscjeden + iloscdwa + ilosctrzy + ilosccztery + iloscpiec + iloscszesc;

            if (ilosc != 0)
            {
                Double srednia = sredniajeden + sredniadwa + sredniatrzy + sredniacztery + sredniapiec + sredniaszesc;
                if (srednia > 6)
                {

                }
                else
                {
                    maskedTextBox1.Text = srednia.ToString();
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            nowy();
        }


        public void testujemy(string text, PictureBox p)
        {
            // Our picturebox is an empty container, so we must create the bitmap to go in it.
            Bitmap bmp1 = new Bitmap(p.Width, p.Height);

            //Now make a GDI+ graphics connection to the bitmap
            Graphics gr1 = Graphics.FromImage(bmp1);

            //We want our text to look good, so we'll tell GDI+ to apply AntiAliasing to anything
            //we add
            gr1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

            //Now we're ready to perform our first function - TranslateTransform
            //We're not actually moving anything graphical here, we're just telling GDI+
            //that we want to move the origin of the graphics object for subsequent rendering
            gr1.TranslateTransform(40, 0);

            //Next comes the RotateTransform
            //Again we're not actually moving anything graphical, we're just telling GDI+
            //that anything we draw from now on is to be rotated.
            gr1.RotateTransform(90);

            //Now add the text.
            gr1.DrawString(text, DefaultFont, Brushes.Black, x, y);

            //Finally, put the bitmap (which has been updated via GDI+) into the picturebox.
            p.Image = bmp1;
        }
    }


    }


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;

namespace Nowe
{
    
    public partial class Form2 : Form
    {
        public static int ocenadod = 0;
        public static int sredniadod = 0;
        public static bool buttond = true;
        public static bool buttonj = true;
        public Form2()
        {
            InitializeComponent();
            this.maskedTextBox1.Click += new EventHandler(maskedTextBox1_Click);
            this.maskedTextBox2.Click += new EventHandler(maskedTextBox2_Click);
            this.maskedTextBox3.Click += new EventHandler(maskedTextBox3_Click);
            this.maskedTextBox4.Click += new EventHandler(maskedTextBox4_Click);
            this.maskedTextBox5.Click += new EventHandler(maskedTextBox5_Click);
            this.maskedTextBox6.Click += new EventHandler(maskedTextBox6_Click);
            this.maskedTextBox7.Click += new EventHandler(maskedTextBox7_Click);
            this.maskedTextBox8.Click += new EventHandler(maskedTextBox8_Click);
            this.maskedTextBox9.Click += new EventHandler(maskedTextBox9_Click);
            this.maskedTextBox10.Click += new EventHandler(maskedTextBox10_Click);
            this.maskedTextBox11.Click += new EventHandler(maskedTextBox11_Click);
            this.maskedTextBox12.Click += new EventHandler(maskedTextBox12_Click);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (sredniadod == 1)
            {
                Form1.iloscjeden = Form1.iloscjeden + 1;
                Form1.sumajeden = Form1.sumajeden + ocenadod;
            }
            if (sredniadod == 2)
            {
                Form1.iloscdwa = Form1.iloscdwa + 1;
                Form1.sumadwa = Form1.sumadwa + ocenadod;
            }
            if (sredniadod == 3)
            {
                Form1.ilosctrzy = Form1.ilosctrzy + 1;
                Form1.sumatrzy = Form1.sumatrzy + ocenadod;
            }
            if (sredniadod == 4)
            {
                Form1.ilosccztery = Form1.ilosccztery + 1;
                Form1.sumacztery = Form1.sumacztery + ocenadod;
            }
            if (sredniadod == 5)
            {
                Form1.iloscpiec = Form1.iloscpiec + 1;
                Form1.sumajpiec = Form1.sumajpiec + ocenadod;
            }
            if (sredniadod == 6)
            {
                Form1.iloscszesc = Form1.iloscszesc + 1;
                Form1.sumaszesc = Form1.sumaszesc + ocenadod;
            }
            Form1.noweo = ocenadod;
            Form1.nowes = sredniadod;
            Form1.nowebool = true;
            Form1.nazwa =textBox3.Text;
            Close();

        }

        private void maskedTextBox1_Click(object sender, EventArgs e)
        {
            ocenadod = 1;
            hideoc();
        }
        private void maskedTextBox2_Click(object sender, EventArgs e)
        {
            ocenadod = 2;
            hideoc();
        }
        private void maskedTextBox3_Click(object sender, EventArgs e)
        {
            ocenadod = 3;
            hideoc();
        }
        private void maskedTextBox4_Click(object sender, EventArgs e)
        {
            ocenadod = 4;
            hideoc();
        }
        private void maskedTextBox5_Click(object sender, EventArgs e)
        {
            ocenadod = 5;
            hideoc();
        }
        private void maskedTextBox6_Click(object sender, EventArgs e)
        {
            ocenadod = 6;
            hideoc();
        }
        private void maskedTextBox7_Click(object sender, EventArgs e)
        {
            sredniadod = 6;
            hides();
        }
        private void maskedTextBox8_Click(object sender, EventArgs e)
        {
            sredniadod = 5;
            hides();
        }
        private void maskedTextBox9_Click(object sender, EventArgs e)
        {
            sredniadod = 4;
            hides();
        }
        private void maskedTextBox10_Click(object sender, EventArgs e)
        {
            sredniadod = 3;
            hides();
        }
        private void maskedTextBox11_Click(object sender, EventArgs e)
        {
            sredniadod = 2;
            hides();
        }
        private void maskedTextBox12_Click(object sender, EventArgs e)
        {
            sredniadod = 1;
            hides();
        }

        private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            maskedTextBox1.Hide();
            ocenadod = 0;
            sredniadod = 0;
            buttond = true;
            buttonj = true;
    }

        private void button2_Click(object sender, EventArgs e)
        {
            if (buttonj)
            {
                buttonj = false;
                showoc();
            }
            else
            {
                buttonj = true;
                hideoc();
            }


        }
        public void showoc()
        {
            maskedTextBox1.Show();
            maskedTextBox2.Show();
            maskedTextBox3.Show();
            maskedTextBox4.Show();
            maskedTextBox5.Show();
            maskedTextBox6.Show();
        }
        public void shows()
        {
            maskedTextBox7.Show();
            maskedTextBox8.Show();
            maskedTextBox9.Show();
            maskedTextBox10.Show();
            maskedTextBox11.Show();
            maskedTextBox12.Show();
        }
        public void hideoc()
        {
            maskedTextBox1.Hide();
            maskedTextBox2.Hide();
            maskedTextBox3.Hide();
            maskedTextBox4.Hide();
            maskedTextBox5.Hide();
            maskedTextBox6.Hide();
        }
        public void hides()
        {
            maskedTextBox7.Hide();
            maskedTextBox8.Hide();
            maskedTextBox9.Hide();
            maskedTextBox10.Hide();
            maskedTextBox11.Hide();
            maskedTextBox12.Hide();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = ocenadod.ToString();
            if(sredniadod!=0)
            textBox2.Text = "0."+sredniadod.ToString();
            else
            {
                textBox2.Text = "0";
            }
            if(sredniadod==0||ocenadod==0)
            {
                button1.Enabled = false;
            }
            else
            {
                button1.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (buttond)
            {
                buttond = false;
                shows();
            }
            else
            {
                buttond = true;
                hides();
            }
            
        }

        private void maskedTextBox12_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

0

Za każdym razem gdy wchodzisz do picturebox() (a wywołujesz je przyciskiem i z timera) tworzysz nowy przycisk z przesunięciem o x-20.

1 użytkowników online, w tym zalogowanych: 0, gości: 1