我在一個資料庫里有一些圖片,我檢索了它們。為了加載這些圖片,我在Windows Form中制作了一個 "Tab控制元件",其中有一個"Tab page1"。當程式運行時,將為每張圖片創建一個包含PictureBox(和其他一些文本框)的組框。 然而,在運行程序中,我不能選擇這些圖片。誰能提供一個解決方案?
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類中從資料庫中檢索圖片。(output是資料庫的查詢結果)
ProductImages pI = output[i];
imgbyte = pI.Pic;
using (MemoryStream ms = new MemoryStream(imgbyte))
{
Image img = Image.FromStream(ms);
pic.Image = img;
}
//在tabpage上添加組框串列:
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)。
}
}
預先感謝
uj5u.com熱心網友回復:
你的類SelectableGroupBox不適合讓用戶選擇一個或多個影像。在你的應用程式中最多只能有一個焦點控制元件--這可能是你表單上的一個按鈕,用戶點擊它來保存所選的圖片。
一個簡單的解決方案是使用CheckBox控制元件,將Appearance屬性設定為Button。此外,你不必手動布局影像,讓FlowLayoutPanel來完成這項作業。
首先,添加一個名為flowLayoutPanel的flowLayoutPanel到你的tabPage2并設定以下屬性:
flowLayoutPanel.AutoScroll = true。
flowLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill。
然后將Form2的相應代碼改為:
private const int imageWidth =128;
private const int imageHeight =128;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e)。
for (int i = 0; i < output.Count; i )
{
CheckBox cb = new CheckBox()。
cb.Appearance = Appearance.Button;
cb.Size = new Size(imageWidth, imageHeight);
cb.BackgroundImageLayout = ImageLayout.Zoom;
ProductImages pI = output[i];
//Don't dispose the MemoryStream, the Image class will need it!
var ms = new MemoryStream(pI.Pic)。
cb.BackgroundImage = Image.FromStream(ms);
flowLayoutPanel.Controls.Add(cb)。
}
}
這就是你如何獲得你所選擇的圖片:
private void SaveButton_Click(object sender, EventArgs e)。
{
var selected = flowLayoutPanel.Controls.OfType<CheckBox>().Where(x => x.Checked)。
Debug.Print("選定的影像。{0}", selected.Count())。
foreach (var item in selected)
{
//Save the picture from item.BackgroundImage.。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/326909.html
標籤:
上一篇:引數丟失或值為空laravel
