【发布时间】:2021-09-19 20:45:42
【问题描述】:
我在数据库中有一些图片,我可以检索它们。为了加载这些图片,我在 Windows 窗体中制作了一个“Tab Control”,它有一个“Tab page1”。当程序运行时,将为每张图片创建一个包含 PictureBox(和一些其他文本框)的组框。我的图片可以加载到这些图片框中,我会有一个分组框列表(gbList)。但是,我无法在运行期间选择这些图片。有人可以提出解决方案吗?
private void Form2_Load(object sender, EventArgs e)
{
tabPage1.Controls.Clear();
int x = 0, y = 0;
int j = 0;
for (int i = 0; i < output.Count - 1; i++)
{
PictureBox pic = new PictureBox();
pic.SizeMode = PictureBoxSizeMode.StretchImage;
SelectablegroupBox gb = new SelectablegroupBox();
gb.Controls.Add(pic);
gbList.Add(gb);
//在ProductImages类中从数据库中获取图片:(输出是查询数据库的结果)
ProductImages pI = output[i];
imgbyte = pI.Pic;
using (MemoryStream ms = new MemoryStream(imgbyte))
{
Image img = Image.FromStream(ms);
pic.Image = img;
}
//将分组框列表添加到标签页:
tabPage1.Controls.Add(gbList[j]);
gbList[j].Location = new Point(x, y);
y += gbList[i].Height;
j++;
}
这是我的问题。我希望用户能够选择图像(然后我想保存这些选定的项目)。但是“结果”总是空的:
var result = from s in gbList
where s.Focused ==true
select s;
foreach (var s in result)
{ //save the selected images}
我从另一篇文章中了解到,我将 SelectablegroupBox" 定义为:
class SelectablegroupBox : GroupBox
{
public SelectablegroupBox()
{
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
protected override void OnEnter(EventArgs e)
{
this.Focus();
this.Invalidate();
base.OnEnter(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (this.Focused)
{
var rc = this.ClientRectangle;
rc.Inflate(-2, -2);
ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
}
}
}
提前致谢
【问题讨论】:
-
您的目标是什么:Winforms、WPF、ASP..?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它! - 你确定它们显示的是 GroupBox 吗? Groupboxes 是容器,通常不接受焦点也不显示图像..
-
我正在使用 windows 窗体。我正在寻找一种能够选择图像的方法。
-
你写的,但是图片在哪里加载?在图片框中?面板??标签???其他控制? - 另外:“此代码不起作用” 总是一个糟糕/无用的描述!怎么了?编译错误?结果错误?没有结果? - 另外:只有 一个 控件(如果有)可以拥有焦点! Focus 和 Selected 有很大区别!!
-
您显示的代码根本不足以看到您的问题。尝试添加更多代码,您要从哪里选择图像以及要在哪里显示它们?
-
我没有看到任何可以“选择”组框或图片框的代码。 Norte
Focus()仅将键盘焦点设置为控件,从而从以前可能拥有它的任何控件中窃取它。 - 你为什么不给你的班级一个Selected属性? - 还要让用户以实用的方式进行选择,例如通过点击而不是仅仅输入!!