心中有你

博客园 首页 新随笔 联系 订阅 管理

本例实现将一个ComboBox上的内容拖动到一个pictureBox上来让pictureBox上显示当前拖动的ComboBox上的内容所显示的图片。

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace formdemo
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.AllowDrop = true;
            this.comboBox1.AllowDrop = true;
        }

        private void pictureBox1_DragEnter(object sender, DragEventArgs e)
        {

            e.Effect = DragDropEffects.Move; 
        }

        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
           ComboBox cb = e.Data.GetData(typeof(ComboBox)) as ComboBox;
            Bitmap bmp = Bitmap.FromFile("c:\\test\\" + cb.SelectedItem.ToString()) as Bitmap;
            this.pictureBox1.Image = bmp;
            this.pictureBox1.Refresh();
        }

        private void comboBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.comboBox1.DoDragDrop(comboBox1, DragDropEffects.Copy | DragDropEffects.Move);
        }

       
    }
}

  

posted on 2011-11-18 16:53  心中有你  阅读(352)  评论(0编辑  收藏  举报