Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PaintEventHandler逻辑问题_C#_Winforms - Fatal编程技术网

C# PaintEventHandler逻辑问题

C# PaintEventHandler逻辑问题,c#,winforms,C#,Winforms,我正在动态创建一些PictureBox,然后分配以下内容: // class variable public String PaintLabel; // private void Form2_Load(object sender, EventArgs e) //begin loop this.PaintLabel = serialno; Shapes[i].Paint += new PaintEventHandler(ctl_Paint); // end loop // my event

我正在动态创建一些PictureBox,然后分配以下内容:

// class variable
public String PaintLabel;

// private void Form2_Load(object sender, EventArgs e)

//begin loop
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop

// my event override
private void ctl_Paint(object sender, PaintEventArgs e)
{
    Control tmp = (Control)sender;

    using (Font myFont = new Font("Arial", 9, FontStyle.Bold))
    {
        e.Graphics.DrawString(this.PaintLabel, myFont, Brushes.LightYellow, new Point(62, 2));
    } // using (Font myFont = new Font("Arial", 10))
} // private void ctl_Paint(object sender, EventArgs e)
它应该创建图片框,并在每个图片框上写上不同的序列号。 但它最终会写下所有图片框上的最后一个序列号

编辑:

好的,你的解决方案对我来说非常先进。但我试着去理解它

我已将你的代码添加到我的代码中

然后将我的图片框数组更改如下

MyControl[] Shapes = new MyControl[Num_Picbox];
在我的循环中,我接着做了以下工作

Shapes[i].SerialNumber = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
但是当我编译并运行代码时,它不会在picturebox上绘制任何序列号

决议:

谢谢你的帮助。我换了你的衣服

var PaintLabels = new Dictionary<Control, string>();
var PaintLabels=new Dictionary();

Dictionary PaintLabels=new Dictionary();

这样一来,绘制事件就看不到局部变量了

这是因为您在循环中反复使用字符串字段,更新其值,直到循环完成,最后一个值将出现在字段中:

//begin loop
// *** here is your problem; there is only one PaintLabel ***
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop
一种解决方案是将
PaintLabel
制作成一个数组,数组中的元素数量与形状数量相同。或者更简单,制作一个
字典
,它还保存形状和序列号之间的引用:

var PaintLabels = new Dictionary<Control, string>();

//begin loop
PaintLabels.Add(Shapes[i], serialno);
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop


// in the paint event
e.Graphics.DrawString(PaintLabel[tmp], myFont, Brushes.LightYellow, new Point(62, 2));
var PaintLabels=new Dictionary();
//开始循环
添加(形状[i],序列号);
形状[i].Paint+=新的PaintEventHandler(ctl_Paint);
//端环
//在油漆事件中
e、 Graphics.DrawString(PaintLabel[tmp],myFont,Bruss.LightYellow,new Point(62,2));

这是因为您在循环中反复使用字符串字段,更新其值,直到循环完成,最后一个值将出现在字段中:

//begin loop
// *** here is your problem; there is only one PaintLabel ***
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop
一种解决方案是将
PaintLabel
制作成一个数组,数组中的元素数量与形状数量相同。或者更简单,制作一个
字典
,它还保存形状和序列号之间的引用:

var PaintLabels = new Dictionary<Control, string>();

//begin loop
PaintLabels.Add(Shapes[i], serialno);
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop


// in the paint event
e.Graphics.DrawString(PaintLabel[tmp], myFont, Brushes.LightYellow, new Point(62, 2));
var PaintLabels=new Dictionary();
//开始循环
添加(形状[i],序列号);
形状[i].Paint+=新的PaintEventHandler(ctl_Paint);
//端环
//在油漆事件中
e、 Graphics.DrawString(PaintLabel[tmp],myFont,Bruss.LightYellow,new Point(62,2));

谢谢,我现在更明白了。如何将要绘制的变量指定给图片框?我是否使用委托函数?我不知道如何解决这个问题。因为我需要为每个图片框运行绘制函数,并在每个图片框上绘制不同的序列号,我想我需要以某种方式将序列号传递给绘制事件处理程序,而不是像我目前正在做的那样重复使用字符串。您在我的代码示例中有答案;
PaintLabel
是一个字典,使用每个
PictureBox
控件作为键。在paint事件处理程序中,您可以使用
tmp
变量作为键从字典中获取相应的字符串(请参阅我的答案中的
DrawString
示例)。谢谢,我没有看到您在这里发表评论,我很抱歉。我已尝试实施您的解决方案。但我在油漆事件中出错了。错误->与“string.this[int]”匹配的最佳重载方法有一些无效参数。它似乎不喜欢PaintLabel[tmp]参数。谢谢,我现在更了解它了。如何将要绘制的变量指定给图片框?我是否使用委托函数?我不知道如何解决这个问题。因为我需要为每个图片框运行绘制函数,并在每个图片框上绘制不同的序列号,我想我需要以某种方式将序列号传递给绘制事件处理程序,而不是像我目前正在做的那样重复使用字符串。您在我的代码示例中有答案;
PaintLabel
是一个字典,使用每个
PictureBox
控件作为键。在paint事件处理程序中,您可以使用
tmp
变量作为键从字典中获取相应的字符串(请参阅我的答案中的
DrawString
示例)。谢谢,我没有看到您在这里发表评论,我很抱歉。我已尝试实施您的解决方案。但我在油漆事件中出错了。错误->与“string.this[int]”匹配的最佳重载方法有一些无效参数。它似乎不喜欢PaintLabel[tmp]参数。