输出值为什么不一样

desperaso 2021-03-13 03:31:35
基础没学好,掉坑里了。这两段为什么输出不一样?

就输出0、1、2
本来想这样

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

for (int i = 0; i < 3; i++)
{
Button button = new Button() { Text = "Button_" + i, Location = new Point(10, 10+i*30), };
button.Click += (s, e) => { Show_Value(i); };
this.Controls.Add(button);
}
}

private void Show_Value(int value)
{
MessageBox.Show(value.ToString());
}
}


最后只能写成样

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

for (int i = 0; i < 3; i++)
{
int u = i;
Button button = new Button() { Text = "Button_" + i, Location = new Point(10, 10+i*30), };
button.Click += (s, e) => { Show_Value(u); };
this.Controls.Add(button);
}
}

private void Show_Value(int value)
{
MessageBox.Show(value.ToString());
}
}
...全文
239 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
MichaelGLX 2021-03-15
  • 打赏
  • 举报
回复
引用 7 楼 desperaso 的回复:
[quote=引用 6 楼 MichaelGLX 的回复:]因为你Show_Value 的是 “i” i最后的值是3。
那u不是=i了,u怎么不是3。 在看3楼的闭包知识,我在补补基础[/quote] 因为你建立了 “三个不同的u”。 。如果把int u定义在for循环外面看看? 我说得比较通俗。
desperaso 2021-03-13
  • 打赏
  • 举报
回复
搞好了,谢谢各位,特别3楼。搞清楚了,以后不会再错了。
正在做的安装程序的Build程序,谢了各位


desperaso 2021-03-13
  • 打赏
  • 举报
回复
引用 6 楼 MichaelGLX 的回复:
因为你Show_Value 的是 “i” i最后的值是3。

那u不是=i了,u怎么不是3。
在看3楼的闭包知识,我在补补基础
MichaelGLX 2021-03-13
  • 打赏
  • 举报
回复
因为你Show_Value 的是 “i” i最后的值是3。
desperaso 2021-03-13
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
恭喜你,写对了,至于为什么需要一个局部变量 u 去过渡一下,请你去查阅一下 闭包的知识

得去看看,基础还是要学啊
desperaso 2021-03-13
  • 打赏
  • 举报
回复
基础差,就掉这个坑里了。

if (type == 11 || type == 12) // 进度文字、安装文件详细信息
{
for (int i = 0; i < Base_Label.Length + Font_Label.Length - 1; i++)
{
Label label = new Label();
if (i < Base_Label.Length) label.Content = Base_Label[i];
else label.Content = Font_Label[i - Base_Label.Length];

Functionality.Set_ControlPosition(label, 5, 5 + i * 25);
Value_Box[type].Children.Add(label);

if (i < Base_Label.Length - 1 || i == 9 || i == 10)
{
TextBox Input_Box = new TextBox() { Width = 90, Height = 16, };
Functionality.Set_ControlPosition(Input_Box, 70, 10 + i * 25);
Value_Box[type].Children.Add(Input_Box);
}
else
if (i == 8 || i == 11)
{
Color color = i == 8 ? Color.FromRgb(255, 255, 255) : Color.FromRgb(0, 0, 0);
int t = i; // 这里,开始没有加上,老是不对
Canvas Color_Flag = new Canvas() { Width = 50, Height = 16, Background = new SolidColorBrush(color), Tag = "flag" };
Functionality.Set_ControlPosition(Color_Flag, 90, 210 + (i - 8) * 25);
Value_Box[type].Children.Add(Color_Flag);

Image Image_Button = new Image() { Width = 20, Height = 20, Source = new BitmapImage(new Uri("Images/color.png", UriKind.RelativeOrAbsolute)), };
Functionality.Set_ControlPosition(Image_Button, 165, 208 + (i - 8) * 25);
Image_Button.MouseDown += (s, e) =>
{
new Element_SetColor(
GetAbsolutePlacement(Image_Button, true).X,
GetAbsolutePlacement(Image_Button, true).Y - 420,
type, t // 这里,开始没有加上,老是不对
).ShowDialog();
};
Value_Box[type].Children.Add(Image_Button);
}
}

Image SelFont_Image = new Image() { Width = 15, Height = 17, Source = new BitmapImage(new Uri("Images/sel_btn.png", UriKind.RelativeOrAbsolute)), };
Functionality.Set_ControlPosition(SelFont_Image, 168, 235);
Value_Box[type].Children.Add(SelFont_Image);

Value_Box[type].Height = Max_Height > 305 ? Max_Height : 305;
}

xuzuning 2021-03-13
  • 打赏
  • 举报
回复
恭喜你,写对了,至于为什么需要一个局部变量 u 去过渡一下,请你去查阅一下 闭包的知识
desperaso 2021-03-13
  • 打赏
  • 举报
回复
引用 1 楼 tangyanzhi1111 的回复:
注意看,你一个是show i ,一个是 show u

当然不一样了


那么直接Show(i) 为什么始终是3
还要加一个u=i 才是 0、1、2呢
江湖评谈 2021-03-13
  • 打赏
  • 举报
回复
注意看,你一个是show i ,一个是 show u 当然不一样了

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧