我无法将所选项从一个ListBox传输到另一个ListBox:
protected void Button2_Click(object sender, EventArgs e)
{
foreach (ListItem li in ListBox2.Items)
{
if (li.Selected)
{
ListItem liNew = new ListItem(li.Text, li.Value);
ListBox1.Items.Add(liNew);
ListBox2.Items.Remove(liNew);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了例外:
System.InvalidOperationException:集合已被修改; 枚举操作可能无法执行.
我正在尝试使用2个单独的ListBox来列出脱机和在线服务器.该框每3秒刷新一次,我需要能够遍历每个项目并获取其文本和索引.
我能为此做些什么?
我试过创建一个基于ListBox.Items的For循环但是不成功.
背景:我有一个允许多选的列表框。我的列表框中有一个特定值,如果选中该值,则需要为其运行单独的代码路径,而所有其他选择都通过另一条路径。
问题:我不知道如何在 VB.NET 中正确编写它以使其按照我想象的方式工作。
代码:
For Each Item As String In listbox1.SelectedItems
If listbox1.SelectedItem = myValue Then
Do this
Else
Do that
End If
Next
Run Code Online (Sandbox Code Playgroud)
如果我在列表中进行多项选择,代码将无法正常工作。只有当 myValue 是 listbox1 中的唯一选择时,它才能正常工作。
有什么建议?
我正在asp.net页面上实现一些功能,这样:并排列表框,左边一个是预先填充的,另一个是空的.除了左侧列表框还有添加按钮,除此之外还有删除按钮.我编写了javascript代码,这样在Add按钮上单击所选listItems从左列表框移动到右列表框,反之亦然.
它如下:

当我提交表单时,在代码隐藏中,我无法从右侧ListBox中检索新移动的值(我知道它不可能以这种方式). 如何访问代码隐藏中使用javascript更改到列表框所做的更改.
任何帮助赞赏.
我的 MainWindow 中有两个 UserControl,UserControl2 有 2 个 Listboxes、Texboxes 和 Buttons。当我在 TextBox 中写入一些文本并按下 Button 时,它应该添加到 ListBox 中。有人可以帮我编写代码吗,我是 WPF 和 MVVM 的新手
这是我的 XAML 代码
<Window x:Class="Wpf_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf_MVVM"
Title="Voxer" Background="SlateGray" Height="420" Width="550">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" VerticalAlignment="Top"/>
<local:UserControl2 HorizontalAlignment="Left" VerticalAlignment="Top" Margin="150,29,0,0"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
这是我的 UserControl1.Xaml 代码
<UserControl x:Class="Wpf_MVVM.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="Auto" Width="Auto">
<Grid>
<ListBox HorizontalAlignment="Left" Height="310" VerticalAlignment="Top" Width="150" Margin="0,40,0,0">
<ListBoxItem>Name 1</ListBoxItem>
<ListBoxItem>Name 2</ListBoxItem>
<ListBoxItem>Name 3</ListBoxItem>
<ListBoxItem>Name 4</ListBoxItem>
<ListBoxItem>Name 5</ListBoxItem>
<ListBoxItem>Name 6</ListBoxItem>
</ListBox>
<Label Content="Conversations" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="150" FontSize="20" …Run Code Online (Sandbox Code Playgroud) 我的 C# 代码遇到问题..
我想用这段代码做的是,当我从文本框中输入具有相同符号(相同名称)的内容时,它显示我无法将其添加到列表框中两次或多次。所以我编写了这段代码:
void Btn_addClick(object sender, EventArgs e)
{
string thelist = listBox1.Text;
string text = textBox1.Text;
if(text == thelist)
{
MessageBox.Show("This name already exists!");
}
else
{
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是,只有当我从列表中选择名称并从文本框中进行比较时,它才会显示消息框。如果我不选择任何内容或其他单词,它会添加相同的单词而不告诉它已经存在。
我正在尝试创建 ListBox,其中我将拥有键值对。我从课堂上获得的那些数据是从 getter 那里获得的。
班级:
public class myClass
{
private int key;
private string value;
public myClass() { }
public int GetKey()
{
return this.key;
}
public int GetValue()
{
return this.value;
}
}
Run Code Online (Sandbox Code Playgroud)
程序:
private List<myClass> myList;
public void Something()
{
myList = new myList<myClass>();
// code for fill myList
this.myListBox.DataSource = myList;
this.myListBox.DisplayMember = ??; // wanted something like myList.Items.GetValue()
this.myListBox.ValueMember = ??; // wanted something like myList.Items.GetKey()
this.myListBox.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
它类似于本主题 [ Cannot do key-value in listbox in …
我正在开发 WPF 应用程序。我以以下方式添加CheckBoxes到 aListBox中。
foreach (User ls in lst)
{
AddContacts(ls, lstContactList);
}
private void AddContacts(User UserData, ListBox lstbox)
{
try
{
var txtMsgConversation = new CheckBox()
{
Padding = new Thickness(1),
IsEnabled = true,
//IsReadOnly = true,
Background = Brushes.Transparent,
Foreground = Brushes.White,
Width = 180,
Height = 30,
VerticalAlignment = VerticalAlignment.Top,
VerticalContentAlignment = VerticalAlignment.Top,
Content = UserData.Name, //+ "\n" + UserData.ContactNo,
Margin = new Thickness(10, 10, 10, 10)
};
var SpConversation = new StackPanel() …Run Code Online (Sandbox Code Playgroud) 我有一个Listbox,它有一个ItemSource绑定到后面的代码中的属性.
<Border BorderBrush="#d6786a" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="2" Grid.Column="15" Grid.Row="5" Grid.ColumnSpan="3">
<ListBox x:Name="ListSrc" Background="#ececec" ItemsSource="{Binding Path=ListBoxData}" dd:DragDrop.IsDragSource="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="4" Margin="15"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
</ListBox>
</Border>
Run Code Online (Sandbox Code Playgroud)
从下图中可以看出,项目直接在彼此上方/下方.我怎么能在行之间添加一些填充?
我有这个代码片段:
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
list = ['one','disable me','two']
listbox = tk.Listbox(self,height=len(list))
for item in list:
listbox.insert(tk.END, item)
listbox.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Run Code Online (Sandbox Code Playgroud)
是否可以禁用第二条选择线,使其无法选择?此外,是否可以以不同的(灰色)字体显示它?