-2

在 Listview 中投射时出错:

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int a = 1;
        string theimage = textBox1.Text + @"\allimages\";
        foreach (ListViewItem item in listView1.SelectedItems)
        {   
            // 39 zero's + "1"
            string initValue = new String('0', 3) + "0";
            // convert to int and add 1
            int newValue = Int32.Parse(initValue) + a;
            // convert back to string with leading zero's
            string newValueString = newValue.ToString().PadLeft(4, '0');

            string imageslist =  "product" + newValueString + "img";
            string[] images = Directory.GetFiles(theimage, imageslist + "*.jpg");

            // Cast the Picturebox
            PictureBox myPicBox = new PictureBox();
            myPicBox.Location = new Point(7, 240);
            myPicBox.Size = new System.Drawing.Size(140, 140);
            myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
            myPicBox.Margin = new Padding(3,3,3,3);
            myPicBox.Visible = true;
            myPicBox.Image = new Bitmap(images[1]);
            Controls.Add(myPicBox);
            System.Diagnostics.Debugger.Break();
            //List<PictureBox> pictureBoxList = new List<PictureBox>();
        }

    }

这是我的错误:

错误 1 ​​'test.Form1.PictureBoxSizeMode()' 是一个'方法',在给定的上下文 C:\Users\radiaku\Documents\Visual Studio 2008\Projects\test\test\Form1.cs 428 37 测试中无效

当我使用 button_click 处理程序时,上面的代码工作正常。

4

2 回答 2

3

听起来您的表单有一个名为PictureBoxSizeMode. 您可以更改方法名称,或将属性设置器更改为:

myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

不过,更改方法名称会更简洁。

于 2012-12-31T12:42:38.830 回答
0

PictureBoxSizeMode仅当您在代码中命名了一个方法时,才会发生该错误。您应该将该方法重命名为其他名称。像这样的场景:

private void UserInput_KeyPress(object sender, KeyPressEventArgs e)
{
    PictureBox myPicBox = new PictureBox();
    myPicBox.Location = new Point(7, 240);
    myPicBox.Size = new System.Drawing.Size(140, 140);
    myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
    myPicBox.Margin = new Padding(3, 3, 3, 3);
    myPicBox.Visible = true;
}
private void PictureBoxSizeMode()
{
}

或者用命名空间来限定它。

myPicBox.SizeMode = myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
于 2012-12-31T12:41:55.030 回答