C#里面做一个ListBox点击Add添加

qq_34656079 2018-09-14 10:37:16

如下图,这个效果怎么做,点击Add,ListBox中会有一行等待添加,填完鼠标离开自动添加完成,这个效果使用C#怎么来做?
...全文
321 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏克贝塔03 2018-09-17
  • 打赏
  • 举报
回复
不知道winform的flowlayoutpanel行吗?控件都往上添加或移除,好像也能实现你的功能

public Form2()
{
InitializeComponent();
listTextBoxs = new List<TextBox>();
}

List<TextBox> listTextBoxs;
private void Form2_Load(object sender, EventArgs e)
{
this.flowLayoutPanel1.Dock = DockStyle.Fill;
}
private void button1_Click(object sender, EventArgs e)
{
TextBox t1 = new TextBox();

listTextBoxs.Add(t1);

ShowTextBoxs();
}

private void button2_Click(object sender, EventArgs e)
{
if (listTextBoxs.Count>0)
{
listTextBoxs.RemoveAt(listTextBoxs.Count - 1);
ShowTextBoxs();
}
}

private void ShowTextBoxs()
{
this.flowLayoutPanel1.Controls.Clear();
foreach (TextBox t in listTextBoxs)
{
this.flowLayoutPanel1.Controls.Add(t);
}
}
yanghao1 2018-09-17
  • 打赏
  • 举报
回复
最后一步是鼠标离开事件,有关内容写在其中。
番茄爱上蛋 2018-09-15
  • 打赏
  • 举报
回复
MainWindow.xaml:
<Window x:Class="ListBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ListBoxTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Gray">
<Window.Resources>
<DataTemplate x:Key="ListBoxTemplate">
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="97" Margin="0,5,0,5" FontFamily="Arial" Foreground="Black" FontSize="20" Background="White"/>
</DataTemplate>
<Style TargetType="ListBoxItem" x:Key="ListBoxBack">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF38474E"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF38474E"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF38474E"/>
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" BorderThickness="0" Margin="125,80,0,0" VerticalAlignment="Top" Width="100" ItemContainerStyle="{StaticResource ListBoxBack}" ItemsSource="{Binding TextBoxList}" ItemTemplate="{StaticResource ListBoxTemplate}" SelectedIndex="{Binding SelectIndex}"/>
<Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="283,89,0,0" VerticalAlignment="Top" Width="75" Command="{Binding AddCommand}"/>
<Button x:Name="DeleteBtn" Content="Delete" HorizontalAlignment="Left" Margin="283,149,0,0" VerticalAlignment="Top" Width="75" Command="{Binding DeleteCommand}"/>
</Grid>
</Window>


MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBoxTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

var vm = new MainWindowViewModel();
this.DataContext = vm;
}
}
}


MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;

namespace ListBoxTest
{
public class MainWindowViewModel : NotificationObject
{
private ObservableCollection<TextBox> _textBoxList = new ObservableCollection<TextBox>();
public ObservableCollection<TextBox> TextBoxList
{
get
{
return _textBoxList;
}
set
{
if (_textBoxList != value)
{
_textBoxList = value;
this.RaisePropertyChanged(() => this.TextBoxList);
}
}
}

private int _selectIndex = 0;
public int SelectIndex
{
get
{
return _selectIndex;
}
set
{
_selectIndex = value;
this.RaisePropertyChanged(() => this.SelectIndex);
}
}

protected DelegateCommand<object> _addCommand = null;
public DelegateCommand<object> AddCommand
{
get
{
if (_addCommand == null)
{
_addCommand = new DelegateCommand<object>(OnAddCommand, CanAddCommand);
}
return _addCommand;
}
}

private bool CanAddCommand(object para)
{
return true;
}

private void OnAddCommand(object para)
{
Add();
}

private void Add()
{
if (TextBoxList != null)
{
TextBoxList.Add(new TextBox { Text = "None"});
}
}

protected DelegateCommand<object> _deleteCommand = null;
public DelegateCommand<object> DeleteCommand
{
get
{
if (_deleteCommand == null)
{
_deleteCommand = new DelegateCommand<object>(OnDeleteCommand, CanDeleteCommand);
}
return _deleteCommand;
}
}

private bool CanDeleteCommand(object para)
{
return true;
}

private void OnDeleteCommand(object para)
{
Delete();
}

private void Delete()
{
if (SelectIndex >= 0 && SelectIndex <= TextBoxList.Count)
{
TextBoxList.RemoveAt(SelectIndex);
}
}
}


菜鸟瞎写的,里面还有点问题,你看看能不能帮到你
SoulRed 2018-09-14
  • 打赏
  • 举报
回复
用Textinput控件,点击一下就显示这个控件在你想要的位置,然后回车就取消这个控件显示变为lable 控件或者其他你想要的控件。
  • 打赏
  • 举报
回复
用一个面板控件 往里边添加一个TextBox控件。
123321... 2018-09-14
  • 打赏
  • 举报
回复
你点击add的时候在你设定的位置生成文本控件试试
qq_34656079 2018-09-14
  • 打赏
  • 举报
回复
大佬有没有什么例子可以参考一下吗?我是搞BS的,不怎么会写CS端自定义控件呢。
  • 打赏
  • 举报
回复
应该是没有这样的控件,但是你可以自己写个控件。
qq_34656079 2018-09-14
  • 打赏
  • 举报
回复
C#哪有这个控件?你说的是别的插件吗?

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧