vs中c#如何得到listbox 拖拽1接受拖拽文件的大小

C#实现让ListBox适应最大Item宽度的方法
转载 & & 作者:永远爱好写程序
这篇文章主要介绍了C#实现让ListBox适应最大Item宽度的方法,涉及ListBox控件的操作技巧,需要的朋友可以参考下
本文实例讲述了C#实现让ListBox适应最大Item宽度的方法。分享给大家供大家参考。具体实现方法如下:
private void button1_Click(object sender, EventArgs e)
for (int i = 1; i &= 16; i++)
listBox1.Items.Add("TTTTTTTTTTTTTTTTTTTTTTTTTTTTT" + i.ToString());
private void button2_Click(object sender, EventArgs e)
SizeF size = listBox1.CreateGraphics().
MeasureString(listBox1.Items[0].ToString(), listBox1.Font);
MessageBox.Show(Convert.ToInt32(size.Width).ToString());
listBox1.Width = Convert.ToInt32(size.Width)+20;
同理CheckedListBox也可以这样控制
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)一、将控件内容拖到其他控件
  在开发过程中,经常会有客户要求,拖动一个控件的数据到另外一个控件中。例如将其中一个ListBox中的数据拖到另一个ListBox中。或者将DataGridView中的数据拖动到TreeView的某个节点。  
  在应用程序中,是通过处理一系列事件,如DragEnter,DragLeave和DragDrop事件来实现在Windows应用程序中的拖放操作的。通过使用这些事件参数中的可用信息,可以轻松实现拖放操作。&
  拖放操作在代码中是通过三步实现的,首先是启动拖放操作,在需要拖动数据的控件上实现MouseDown事件响应代码,并调用DoDragDrop()方法;其次是实现拖放效果,在目标控件上添加DragEnter事件响应代码,使用DragDropEffects枚举类型实现移动或复制等拖动效果;最后是放置数据操作,在目标控件上添加DragDrop响应代码,把数据添加到目标控件中。
2 using System.D
3 using System.C
4 using System.ComponentM
5 using System.Windows.F
6 using System.D
8 namespace DragDrop
/// &summary&
/// Form1 的摘要说明。
/// &/summary&
public class Form1 : System.Windows.Forms.Form
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
/// &summary&
/// 必需的设计器变量。
/// &/summary&
private System.ComponentModel.Container components = null;
public Form1()
// Windows 窗体设计器支持所必需的
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
/// &summary&
/// 清理所有正在使用的资源。
/// &/summary&
protected override void Dispose(bool disposing)
if (disposing)
if (components != null)
components.Dispose();
base.Dispose(disposing);
#region Windows 窗体设计器生成的代码
/// &summary&
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// &/summary&
private void InitializeComponent()
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
// listBox1
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(32, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 280);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
// listBox2
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(248, 24);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(120, 280);
this.listBox2.TabIndex = 0;
this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 333);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.listBox2);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
#endregion
private void Form1_Load(object sender, System.EventArgs e)
this.listBox1.AllowDrop = true;
this.listBox2.AllowDrop = true;
this.listBox1.Items.Add("a");
this.listBox1.Items.Add("b");
this.listBox1.Items.Add("c");
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex], DragDropEffects.Move);
private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.M
private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
this.listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
this.listBox1.Items.Remove(e.Data.GetData(DataFormats.Text));
二、将文件拖到控件中获得文件路径
  把文件或者目录直接拖放到你的程序上,这种效果用户体验不错。
  得到拖过来的路径的代码:(System.Array)e.Data.GetData(DataFormats.FileDrop)。
  然后你可以根据这些路径复制粘贴了。
2 using System.Collections.G
3 using System.ComponentM
4 using System.D
5 using System.D
6 using System.L
7 using System.T
8 using System.Windows.F
10 namespace TestFileDrag
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
SetCtrlDrag.SetCtrlDragEvent(this.textBox1);
public class SetCtrlDrag
public static void SetCtrlDragEvent(Control ctrl)
if(ctrl is TextBox)
TextBox tb = ctrl as TextB
tb.AllowDrop = true;
tb.DragEnter += (sender, e) =&
e.Effect = DragDropEffects.L//拖动时的图标
tb.DragDrop += (sender, e) =&
((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
三、相关说明
  实现拖放效果时,C#中提供了一个系统方法DoDragDrop方法,用于实现开始拖放操作,该方法由Control类所定义,由于控件均直接或是间接派生于Control类,因此开发人员可以在任何可视化组件中调用DoDragDrop方法。DoDragDrop方法使用语法如下:
  public DragDropEffects DoDragDrop ( Object data,DragDropEffects allowedEffects)
  data:用户所要拖动的数据内容。必须将所要拖动的内容传入到这个方法的第一个参数位置。
  allowedEffects:DragDropEffects枚举值之一,此类型包含了拖动操作的效果。DragDropEffects枚举值如表32.8所示。
  DragDropEffects枚举值:    枚举值 说明&    All 从拖动源复制、移除数据,并将其滚动到放置目标中&    Copy 将数据复制到放置目标&    Link 将拖动源中的数据链接到放置目标&    Move 将拖动源的数据移动到放置目标&    None 放置目标不接受该数据&    Scroll 即将在放置目标中开始滚动,或当前正在滚动
  开发人员在使用DoDragDrop方法时,必须指定参数allowedEffects为表中的任何一个成员,另外,还可以使用位运算符,把其中的任何一个成员作为一个完整参数传入,以得到所需的拖动效果,实现关键代码如下:
    DragDropEffects.Copy| DragDropEffects.None
  C#中提供了一个系统拖放事件,与拖放方法一起使用来达到更好的效果。常用的拖放事件如表所示。
  表  拖放事件
    名称 说明&    DragEnter 当用户在拖放操作过程中首次将鼠标光标拖到控件上时,会引发该事件&    DragDrop 在完成拖放操作时发生&    GiveFeedback 在执行拖动操作期间发生&    DragLeave 如果用户移出一个窗口,则引发DragLeave事件&    DragOver 如果鼠标移动但停留在同一个控件中,则引发DragOver事件&    QueryContinueDrag 在拖放操作过程中,当键盘或鼠标按钮状态发生变化时,会引发QueryContinueDrag 事件。QueryContinueDrag事件允许拖动源确定是否应取消拖放操作
阅读(...) 评论()c#如何获取listbox中项的值_百度知道
c#如何获取listbox中项的值
已知它是第i行且未被选中
我有更好的答案
ListBox增加值的时候,可以增加一个Value,一个Text,Text就是显示出来的,Value是看不到的,比如你存一个人,就可以存人的ID和人的NAME,这样可以取到ID来唯一确定一个人了。ListBox.Items.Add((new ListItem(&姓名&,&id&));显示出来的就是“姓名”,可以用ListBox.SelectedItem.Text获得“姓名”ListBox.SelectedItem.Value获得&id&
采纳率:92%
来自团队:
那么索引就是2如果是第三行;
/&#47,第一行从零开始计算。string str = ListBox1.Items[2].Text
我没有text!!!!!
你是WINFORM吧。string str = listBox1.Items[2].ToString();//这样写就可以了,。
本回答被提问者和网友采纳
.Items[i]不过是object类型,你要自己转一下,是int 还是 ClassXX
你自己知道的
为您推荐:
其他类似问题
listbox的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 listbox 拖拽 的文章

 

随机推荐