The*_*boy 6 .net c# windows winforms
所以这可能是一个非常基本的问题,但我正在将ListBox项拖放到一个面板上,该面板将根据值创建组件.
作为一个简单的例子,我需要它能够在ListBox中的项目被放到面板上时在面板上创建一个新的Label.
我有以下代码,但不知道如何在删除后动态地将Label添加到面板.
这是我的示例代码......
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("First Name");
listBox1.Items.Add("Last Name");
listBox1.Items.Add("Phone");
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
ListBox box = (ListBox)sender;
String selectedValue = box.Text;
DoDragDrop(selectedValue.ToString(), DragDropEffects.Copy);
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Label newLabel = new Label();
newLabel.Name = "testLabel";
newLabel.Text = e.Data.GetData(DataFormats.Text).ToString();
//Do I need to call either of the following code to make it do this?
newLabel.Visible = true;
newLabel.Show();
panel1.Container.Add(newLabel);
}
}
Run Code Online (Sandbox Code Playgroud)
}
Ver*_*cas 18
//Do I need to call either of the following code to make it do this?
newLabel.Visible = true;
newLabel.Show();
Run Code Online (Sandbox Code Playgroud)
没必要.
newLabel.AutoSize = true;
Run Code Online (Sandbox Code Playgroud)
最有可能的是,必须给它一个尺寸.
panel1.Container.Add(newLabel);
Run Code Online (Sandbox Code Playgroud)
必须被替换
newLabel.Parent = panel1;
Run Code Online (Sandbox Code Playgroud)
发现了这个bug.它必须是panel1.Controls.Add(newLabel);
或newLabel.Parent = panel1;
代替panel1.Container.Add(newLabel);
.Container
是另一回事.
归档时间: |
|
查看次数: |
62640 次 |
最近记录: |