Home > Software design >  why when passing the index from the main form to usercontrol the index is 0 all the time?
why when passing the index from the main form to usercontrol the index is 0 all the time?

Time:10-27

at the usercontrol code top

int myindex = 0;
MainForm mainForm;

the constructor

public HistogramaDesenat(MainForm mf)
        {
            mainForm = mf;
            UP();
        }

the UP method

public void UP()
        {
            myindex = mainForm.index;
        }

in the main form

public int index = 0;
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                index = listView1.SelectedIndices[0];
                histogram.UP();
                Histogram.Invalidate();
            }
        }

in the usercontrol class in the UP method when using a breakpoint i see that the myindex value is getting the value from the selected index in the main form.

i selected in the listView the item in index 10 and i see that index in the usercontrol

my index get the value 10

but then when i'm using the variable myindex in the usercontrol paint event for some reason the value of myindex is 0 and not 10 :

the variable myindex reset to 0 instead keep the value 10

private void HistogramaDesenat_Paint(object sender, PaintEventArgs e)
        {
            if (myIsDrawing)
            {

                Graphics g = e.Graphics;
                Pen myPen1 = new Pen(new SolidBrush(Color.Red), myXUnit);
                Pen myPen = new Pen(new SolidBrush(myColor),myXUnit);
                //The width of the pen is given by the XUnit for the control.
                for (int i=0;i<myValues.Length;i  )
                {
                    //We draw each line
                    g.DrawLine(myPen,
                        new PointF(myOffset   (i*myXUnit), this.Height - myOffset), 
                        new PointF(myOffset   (i*myXUnit), this.Height - myOffset - myValues[i] * myYUnit));

                    //We plot the coresponding index for the maximum value.
                    if (myValues[i]==myMaxValue)
                    {
                        SizeF mySize = g.MeasureString(i.ToString(),myFont);

                        g.DrawString(i.ToString(),myFont,new SolidBrush(myColor),
                            new PointF(myOffset   (i*myXUnit) - (mySize.Width/2), this.Height - myFont.Height ),
                            System.Drawing.StringFormat.GenericDefault);
                    }
                }

                
                    g.DrawLine(myPen1,
                        new PointF(myOffset   (myindex * myXUnit), this.Height - myOffset),
                        new PointF(myOffset   (myindex * myXUnit), this.Height - myOffset - myValues[myindex] * myYUnit));

CodePudding user response:

C# is case sensitive. I see this in the listView1_SelectedIndexChanged() method:

histogram.UP();
Histogram.Invalidate();

Notice the difference between the lower and upper-case H on the two lines. Those are two different variables, likely referring to different instances of the control.

  • Related