Tag: 列表框

如何更改列表框中单词的颜色

我创建了一个带有TextBox的表单,它接受一个单词并搜索一堆句子,看看是否有任何单词包含该单词。之后我必须出现那些句子并突出显示单词。我的计划是创建一个ListBox并添加里面的句子。 我的问题是如何突出这个词(通过改变我想的颜色),以便可以区分。 有更好的方法吗? 我选择了ListBox,所以我可以选择我正在寻找的句子。 编辑 根据@Thorsten Dittmar的指示,创建一个所有者绘制的列表框。 public partial class Form1 : Form { private List _items; public Form1() { InitializeComponent(); _items = new List(); _items.Add(“One”); _items.Add(“Two”); _items.Add(“Three”); listBox1.DataSource = _items; } private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); e.Graphics.DrawString(_items[e.Index], new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), new SolidBrush(Color.Red), e.Bounds); } } 我怎么分裂句子才能画出一个单词? EDIT2 我最终做的方式是制作两个单独的组件,以便完善我的选择。 一个是ListBox其中所有句子都是彩色的,并且可以选择其中一个,另一个是具有单独颜色单词的RichBox […]

C# – 错误“并非所有代码路径都返回一个值”,数组作为out参数

我目前有以下代码: public int GetSeatInfoString(DisplayOptions choice,out string [] strSeatInfoStrings) { strSeatInfoStrings = null; int count = GetNumOfSeats(choice); if ((count <= 0)) return 0; strSeatInfoStrings = new string[count]; int i = 0; for (int index = 0; index <= m_totNumOfSeats – 1; index++) { if (string.IsNullOrEmpty(m_nameList[index])) strSeatInfoStrings[i++] = m_nameList [索引]的ToString(); } } 此代码产生错误,“… GetSeatInfoString.DisplayOptions,out string [])’:并非所有代码路径都返回一个值。基本上,我在上面的方法中要做的是循环一个数组并为数组中包含字符串的任何值,我想要这些然后添加到新数组strSeatInfoStrings,而strSeatInfoStrings又可以从一个单独的类中调用,然后新的数组内容显示在列表框中。 有关如何纠正此问题的任何建议? […]

如何从ListBox中选择的项目获取特定属性

我有以下内容: 这是SelectedItem定义 public MemEntity SelectedItem {get; set;} MemEntity是一个包含的类 public String s {get; get;}. 基本上,我希望所选项目的s显示在TextBlock (与ListBox显示的属性相同)。 这不起作用,所以我做错了什么?

从ListBox中单击的项目获取数据

我是Windows Phone的新手, 我有一个带有文本块的列表框,我想从列表框中的选定项目中获取所有数据。 这是我的代码片段: .xaml文件 .cs文件 void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args) { Debug.WriteLine(” You selected ” +listbox1.SelectedItem.ToString()); } 我的控制台以这种方式显示输出: You selected Appname.Pagename.methodname 绑定到ListBox的类 public class Attractions { [JsonProperty(“AttractionName”)] public string AttractionName { get; set; } [JsonProperty(“IphoneImage”)] public string IphoneImage { get; set; } [JsonProperty(“price”)] public string price { get; set; } }

(c#+ windows窗体)将项添加到不同类的listBox中

我有两个类(表单),当我单击“接受”按钮时,我希望将class2一个项添加到class1 listBox 。 我尝试使用以下代码,但listBox中没有任何更改: private void button1_Click(object sender, EventArgs e) { CarRental i = new CarRental(); string id = idRental.Text.ToString(); i.listBox1.Items.Add(id); i.listBox1.Update(); this.Close(); } 我在哪里弄错了?

将多个列表框项添加到数据库

有没有简单的方法将列表框中的项目保存到数据库。 我正在使用Windows窗体的访问数据库,用户从combobox中选择项目并将其添加到列表框中。 现在我想将列表框中的所有项目添加到用逗号分隔的数据库中。 我怎么能这样做? 这是该类的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace Purchase_Management { public partial class Form1 : Form { string connString = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb ;Persist Security Info=False;”; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { […]

在ListBox中扩展选定项高度

有没有办法可以让SelectedItem的高度大于ListBox中其他项目的高度? 这就是我现在所拥有的,但它只是作为一个正常的列表框: public class BuddyListBox : ListBox { public BuddyListBox() { this.ResizeRedraw = true; this.DoubleBuffered = true; this.BorderStyle = BorderStyle.None; this.Dock = DockStyle.Fill; this.DrawMode = DrawMode.OwnerDrawVariable; this.ItemHeight = 16; } protected override void OnDrawItem(DrawItemEventArgs e) { if (e.Index == -1 || e.Index >= this.Items.Count) return; Buddy current = (Buddy)this.Items[e.Index]; //Bitmap icon = current.StatusImage; //e.Graphics.DrawImage(icon, e.Bounds.Left, […]

按C#中的代码设置列表框项的字体和颜色

我正忙于一个自定义列表框,我在c#中用作注册读取器。 现在,我想在一个确定的项目中设置一个确定的项目,其中的字体和颜色与其他项目不同。 我检查了这个问题 ,从答案中我做了以下代码: private void myListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); Font myFont; Brush myBrush; int i = e.Index; if (e.Index == 3) { myFont = e.Font; myBrush = Brushes.Black; } else { myFont = new Font(“Microsoft Sans Serif”, 9.75f, FontStyle.Bold); myBrush = Brushes.CadetBlue; } e.Graphics.DrawString(myListBox.Items[i].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault); } 并使用我的IntializeComponent()调用该方法 this.myListBox.DrawItem += […]

更新后阻止ListBox滚动到顶部

我正在尝试使用ListBox播放列表构建一个简单的音乐播放器。 将音频文件添加到播放列表时,它首先使用文件名填充ListBox,然后(在单独的线程上)提取ID3数据并使用正确的艺术家 – 标题信息覆盖文件名(非常类似于Winamp)。 但是当ListBox正在更新时,它是不可滚动的,因为它总是跳到每个项覆盖的顶部。 有什么办法可以防止这个? 编辑: 代码: public Form1() { //Some initialization code omitted here BindingList trackList = new BindingList(); // The Playlist this.playlist = new System.Windows.Forms.ListBox(); this.playlist.Location = new System.Drawing.Point(12, 12); this.playlist.Name = “playlist”; this.playlist.Size = new System.Drawing.Size(229, 316); this.playlist.DataSource = trackList; } private void playlist_add_Click(object sender, EventArgs e) { //Initialize OpenFileDialog OpenFileDialog […]

如何在列表框上实现增量搜索?

我想在绑定到Listbox的键值对列表上实现增量搜索。 如果我有三个值(AAB,AAC,AAD),则用户应该能够在可用列表框中选择一个项目并键入AAC,并且该项目应突出显示并处于焦点。 它也应该是渐进的方式。 处理这个问题的最佳方法是什么?