C# 控件的属性始终为null,尽管在代码中已设置
很明显,我今天过得很糟糕,因为这一天应该很容易,但我没有看到。啊 我已经创建了一个从C# 控件的属性始终为null,尽管在代码中已设置,c#,winforms,C#,Winforms,很明显,我今天过得很糟糕,因为这一天应该很容易,但我没有看到。啊 我已经创建了一个从面板派生的控件,它将充当一些“tile”的容器,这些tile本身也是从面板派生的子类。因此,它基本上只是一个大面板中的多个面板。 每个“平铺”上都有一个图片盒,如果设置了图标,则会显示一个图标,如果没有设置,则会显示一个占位符 所有这些看起来都很好,除了在我创建平铺对象时将图像传递到该对象中之外,该属性仍然为null,并且显示错误图标 这是一个简化版本,您应该能够将其粘贴到一个空白的Winform中。当然,你需要
面板
派生的控件,它将充当一些“tile”的容器,这些tile本身也是从面板
派生的子类。因此,它基本上只是一个大面板中的多个面板。每个“平铺”上都有一个
图片盒
,如果设置了图标,则会显示一个图标,如果没有设置,则会显示一个占位符
所有这些看起来都很好,除了在我创建平铺对象时将图像传递到该对象中之外,该属性仍然为null,并且显示错误图标
这是一个简化版本,您应该能够将其粘贴到一个空白的Winform中。当然,你需要提供你自己的形象,我建议你只需要一个
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
高度=500;
宽度=500;
MyPanel MyPanel=新建MyPanel();
myPanel.BackColor=Color.White;
myPanel.BorderStyle=BorderStyle.FixedSingle;
myPanel.高度=高度-50;
myPanel.宽度=200;
myPanel.Top=5;
myPanel.Left=5;
控件。添加(myPanel);
//我在创建时设置图像,文件绝对有效
MyPanel.Tile Tile=新的MyPanel.Tile();
tile.TileIcon=Image.FromFile(@“path/to/file”);
tile.BackColor=Color.blue;
myPanel.AddTile(tile);
MyPanel.Tile tile2=新的MyPanel.Tile();
tile2.TileIcon=Image.FromFile(@“path/to/file”);
tile2.BackColor=Color.PeachPuff;
myPanel.AddTile(tile2);
}
}
类别MyPanel:Panel
{
私有常量int TILEHEIGHT=75;
私有静态列表tileIndex=新列表();
公共MyPanel(){}
公共无效添加磁砖(磁砖磁砖)
{
tile.Width=此.Width;
tile.Anchor=主播样式。左|主播样式。右;
tile.Top=tileIndex.Count*TILEHEIGHT;
tileIndex.Add(平铺);
this.Controls.Add(平铺);
}
公共类瓷砖:面板
{
//图像的属性
私人形象;
公众形象
{
获取{return\u tileIcon;}
设置{u tileIcon=value;}
}
公共瓷砖()
{
底座高度=瓷砖高度;
基数左=0;
基础。顶部=0;
base.BackColor=ColorTranslator.FromHtml(“#FF5555”);
//在PICTUREBOX中显示图像
//总是空的!?
PictureBox_picBox=新PictureBox();
_picBox.SizeMode=PictureBoxSizeMode.StretchImage;
如果(_tileIcon!=null)
{{u picBox.Image={u tileIcon;}
其他的
{u picBox.Image=Image.FromFile(@“path/to/file”);}
_picBox.Left=5;
_picBox.Top=5;
_picBox.Height=(this.Height-10);
_picBox.Width=_picBox.Height;
this.Controls.Add(_picBox);
}
}
}
错误右侧
谢谢 这应该可以解决它。我向构造函数传递了一个映像 问题是,正如我在评论中所说,在构造函数中,您还没有设置
\u tileIcon
,因此它将始终为空。在构造函数之后设置它,但不重新初始化picturebox以使用该图像。您需要重置集合中的picturebox才能正常工作
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Height = 500;
Width = 500;
MyPanel myPanel = new MyPanel();
myPanel.BackColor = Color.White;
myPanel.BorderStyle = BorderStyle.FixedSingle;
myPanel.Height = Height - 50;
myPanel.Width = 200;
myPanel.Top = 5;
myPanel.Left = 5;
Controls.Add(myPanel);
var icon1 = Image.FromFile(@"path/to/file"); //Load the image FIRST
MyPanel.Tile tile = new MyPanel.Tile(icon1); //Pass it into the constructor
tile.BackColor = Color.PowderBlue;
myPanel.AddTile(tile);
var icon2 = Image.FromFile(@"path/to/file"); //Again, image first
MyPanel.Tile tile2 = new MyPanel.Tile(icon2); //Then construct
tile2.BackColor = Color.PeachPuff;
myPanel.AddTile(tile2);
}
}
class MyPanel : Panel
{
private const int TILEHEIGHT = 75;
private static List<Panel> tileIndex = new List<Panel>();
public MyPanel() { }
public void AddTile(Tile tile)
{
tile.Width = this.Width;
tile.Anchor = AnchorStyles.Left | AnchorStyles.Right;
tile.Top = tileIndex.Count * TILEHEIGHT;
tileIndex.Add(tile);
this.Controls.Add(tile);
}
public class Tile : Panel
{
private PictureBox _picBox;
// PROPERTY FOR IMAGE
private Image _tileIcon;
public Image TileIcon
{
get { return _tileIcon; }
//Obviously needs additional logic for null images...
set { _tileIcon = value; _picBox.Image = _tileIcon; }
}
public Tile(Image tileIcon = null)
{
_tileIcon = tileIcon;
base.Height = TILEHEIGHT;
base.Left = 0;
base.Top = 0;
base.BackColor = ColorTranslator.FromHtml("#FF5555");
// Now this won't be null, and save a reference to the
//_picBox so you can easily change the image later through
//the icon property.
_picBox = new PictureBox();
_picBox.SizeMode = PictureBoxSizeMode.StretchImage;
if (_tileIcon != null)
{ _picBox.Image = _tileIcon; }
else
{ _picBox.Image = Image.FromFile(@"path/to/file"); }
_picBox.Left = 5;
_picBox.Top = 5;
_picBox.Height = (this.Height - 10);
_picBox.Width = _picBox.Height;
this.Controls.Add(_picBox);
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
高度=500;
宽度=500;
MyPanel MyPanel=新建MyPanel();
myPanel.BackColor=Color.White;
myPanel.BorderStyle=BorderStyle.FixedSingle;
myPanel.高度=高度-50;
myPanel.宽度=200;
myPanel.Top=5;
myPanel.Left=5;
控件。添加(myPanel);
var icon1=Image.FromFile(@“path/to/file”);//首先加载图像
MyPanel.Tile Tile=新建MyPanel.Tile(icon1);//将其传递给构造函数
tile.BackColor=Color.blue;
myPanel.AddTile(tile);
var icon2=Image.FromFile(@“path/to/file”);//同样,图像优先
MyPanel.Tile tile2=新建MyPanel.Tile(icon2);//然后构造
tile2.BackColor=Color.PeachPuff;
myPanel.AddTile(tile2);
}
}
类别MyPanel:Panel
{
私有常量int TILEHEIGHT=75;
私有静态列表tileIndex=新列表();
公共MyPanel(){}
公共无效添加磁砖(磁砖磁砖)
{
tile.Width=此.Width;
tile.Anchor=主播样式。左|主播样式。右;
tile.Top=tileIndex.Count*TILEHEIGHT;
tileIndex.Add(平铺);
this.Controls.Add(平铺);
}
公共类瓷砖:面板
{
私人图片盒;
//图像的属性
私人形象;
公众形象
{
获取{return\u tileIcon;}
//显然,空图像需要额外的逻辑。。。
设置{u tileIcon=value;_picBox.Image=\u tileIcon;}
}
公共磁贴(图像磁贴图标=null)
{
_tileIcon=tileIcon;
底座高度=瓷砖高度;
基数左=0;
基础。顶部=0;
base.BackColor=ColorTranslator.FromHtml(“#FF5555”);
//现在,这将不会为null,并保存对
//_picBox,以便以后通过
//图标属性。
_picBox=新的PictureBox();
_picBox.SizeMode=PictureBoxSizeMode.StretchImage;
如果(_tileIcon!=null)
{{u picBox.Image={u tileIcon;}
其他的
{u picBox.Image=Image.FromFile(@“path/to/file”);}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Height = 500;
Width = 500;
MyPanel myPanel = new MyPanel();
myPanel.BackColor = Color.White;
myPanel.BorderStyle = BorderStyle.FixedSingle;
myPanel.Height = Height - 50;
myPanel.Width = 200;
myPanel.Top = 5;
myPanel.Left = 5;
Controls.Add(myPanel);
var icon1 = Image.FromFile(@"path/to/file"); //Load the image FIRST
MyPanel.Tile tile = new MyPanel.Tile(icon1); //Pass it into the constructor
tile.BackColor = Color.PowderBlue;
myPanel.AddTile(tile);
var icon2 = Image.FromFile(@"path/to/file"); //Again, image first
MyPanel.Tile tile2 = new MyPanel.Tile(icon2); //Then construct
tile2.BackColor = Color.PeachPuff;
myPanel.AddTile(tile2);
}
}
class MyPanel : Panel
{
private const int TILEHEIGHT = 75;
private static List<Panel> tileIndex = new List<Panel>();
public MyPanel() { }
public void AddTile(Tile tile)
{
tile.Width = this.Width;
tile.Anchor = AnchorStyles.Left | AnchorStyles.Right;
tile.Top = tileIndex.Count * TILEHEIGHT;
tileIndex.Add(tile);
this.Controls.Add(tile);
}
public class Tile : Panel
{
private PictureBox _picBox;
// PROPERTY FOR IMAGE
private Image _tileIcon;
public Image TileIcon
{
get { return _tileIcon; }
//Obviously needs additional logic for null images...
set { _tileIcon = value; _picBox.Image = _tileIcon; }
}
public Tile(Image tileIcon = null)
{
_tileIcon = tileIcon;
base.Height = TILEHEIGHT;
base.Left = 0;
base.Top = 0;
base.BackColor = ColorTranslator.FromHtml("#FF5555");
// Now this won't be null, and save a reference to the
//_picBox so you can easily change the image later through
//the icon property.
_picBox = new PictureBox();
_picBox.SizeMode = PictureBoxSizeMode.StretchImage;
if (_tileIcon != null)
{ _picBox.Image = _tileIcon; }
else
{ _picBox.Image = Image.FromFile(@"path/to/file"); }
_picBox.Left = 5;
_picBox.Top = 5;
_picBox.Height = (this.Height - 10);
_picBox.Width = _picBox.Height;
this.Controls.Add(_picBox);
}
}
}