iT邦幫忙

0

點選button會跑出圖片,勾選後會加入flowlayoutpanel裡

c#

最近在做一個項目,點選mybutton2之後上面的flowlayoutpanel會出現圖片,之後打勾,被勾選的圖片會加入下方flowlayoutpanel裡,外觀如下

https://ithelp.ithome.com.tw/upload/images/20180703/201101327RJY699sHK.jpg

這是我button裡的程式碼

 List<PhotoDataItem> photolist = PhotoDataSource.Search(SearchTerm: "Microsoft",results:3);
            this.dataGridView1.DataSource = photolist;

            for (int i = 0; i <= photolist.Count - 1; i++)
            {
                MyItemTemplate x = new MyItemTemplate();
                x.Desc = photolist[i].Title;
                x.ImageURL = photolist[i].ImagePath;

                this.flowLayoutPanel3.Controls.Add(x);
                Application.DoEvents();
            }

這是我MyItemTemplate類別裡的程式碼

 public string Desc
        {
            set
            {
                label1.Text = value;
            }

        }
        public byte[] ImageBytes
        {
            set
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream(value);
                this.pictureBox1.Image = Image.FromStream(ms);
            }
        }

        public string ImageURL
        {
            set
            {
                this.pictureBox1.Load(value);
            }
        }

        public MyItemTemplate()
        {
            InitializeComponent();
            this.pictureBox1.MouseEnter += PictureBox1_MouseEnter;
            this.pictureBox1.MouseLeave += PictureBox1_MouseLeave;
            this.pictureBox1.MouseClick += PictureBox1_MouseClick;
           
        }

        private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (checkBox1.Checked)
            {
               
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        private void PictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox1.BackColor = Color.Gray;
            this.pictureBox1.Width -= 8;
            this.pictureBox1.Height -= 8;
        }

        private void PictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.BackColor = Color.Red;
            this.pictureBox1.Width += 8;
            this.pictureBox1.Height += 8;         
           
        }

我目前想到是用mouseclick這個事件並在裡面用checkbox.check來判斷有無勾選
我在裡面加個messagebox.show("有選到");這段有出現有選到這三個字,我想我沒想錯
那請問我該怎麼打才能把他加入到flowlayoutpanel裡?

還沒回答就結案了~
checkBox的作法:
private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
for (int i = 0; i <= photolist.Count - 1; i++)
{
if(checkBox1.name.IndexOf((i + 1).ToString()) >= 0){
MyItemTemplate x = new MyItemTemplate();
x.Desc = photolist[i].Title;
x.ImageURL = photolist[i].ImagePath;
this.flowLayoutPanel.Controls.Add(x);
}
}
}
tenno081 iT邦研究生 4 級 ‧ 2018-07-04 09:06:01 檢舉
恩,因為後來找到答案了,還是感謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
最佳解答

請參考

//改為全域物件
List<PhotoDataItem> photolist = PhotoDataSource.Search(SearchTerm: "Microsoft",results:3);

private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
	foreach(int index in checkBox1.CheckedIndices)
	{
		for (int i = 0; i <= photolist.Count - 1; i++)
		{
			if(index == i){
				MyItemTemplate x = new MyItemTemplate();
				x.Desc = photolist[i].Title;
				x.ImageURL = photolist[i].ImagePath;
				this.flowLayoutPanel.Controls.Add(x);
			}               
		}
	}
}
看更多先前的回應...收起先前的回應...
tenno081 iT邦研究生 4 級 ‧ 2018-07-03 16:39:15 檢舉

不好意思,請問那個改成全域物件是?

放到event外面, 讓其他事件也能讀到photolist的資料

tenno081 iT邦研究生 4 級 ‧ 2018-07-03 16:54:18 檢舉

然後在請問有checkBox1.CheckedIndices這段嗎?
我只有看到CheckedListBox.CheckedIndices這段

CheckedListBox.CheckedIndices這段? checkBox1這不是你的checkBox物件嗎?

tenno081 iT邦研究生 4 級 ‧ 2018-07-03 17:30:18 檢舉

對阿,但我後面並沒有CheckedIndices這個選項,我後來查了一下
只有CheckedListBox後面才有.CheckedIndices這句

所以你的checkBox1是什麼物件?

tenno081 iT邦研究生 4 級 ‧ 2018-07-03 17:47:22 檢舉

checkbox,您的那段應該是給checkedListBox這物件

那就只能改成上面那樣了~

我要發表回答

立即登入回答