我有一個組合框,我希望它能夠自動查找并建議包含字典中任何單詞的短語。我找到了這個解決方案 -鏈接,但我注意到如果找不到該短語,則應用程式可能會崩潰 - 無效的引數值 0 對索引無效。試圖以各種可能的方式解決這個問題,但最終應用程式完全關閉。此解決方案中的錯誤是什么,是否有可能以某種方式修復它,如果找不到該短語,則顯示一個有關此問題的訊息框,例如 ..
該詞典包含與鏈接的第一個問題相同的短語:
門打開元素
外門內
門
滑動門
應用程式并不總是崩潰,我認為這是由于在初始字典的組合框中輸入的這些字母的存在
我在寫時迷戀:
orser
erot 等
并單擊視窗表單,
但是當我嘗試時沒有任何反應搜索:
abc
zui 等
我無法修復代碼,我希望你能。謝謝。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listOriginal.Add("Doors opening elements");
listOriginal.Add("exterior doors");
listOriginal.Add("interior doors");
listOriginal.Add("sliding doors");
this.comboBx.Items.AddRange(listOriginal.ToArray());
}
// Bind default keywords
List<string> listOriginal = new List<string>();
// save new keywords
List<string> listNew = new List<string>();
private void comboBx_TextUpdate(object sender, EventArgs e)
{
//clear combobox
this.comboBx.Items.Clear();
//clear listNew
listNew.Clear();
foreach (var item in listOriginal)
{
// call ToLower() .. not case sensitive
if (item.ToLower().Contains(this.comboBx.Text))
{
//add to ListNew
listNew.Add(item);
}
}
this.comboBx.Items.AddRange(listNew.ToArray());
this.comboBx.SelectionStart = this.comboBx.Text.Length;
Cursor = Cursors.Default;
// Automatically pop up drop-down
this.comboBx.DroppedDown = true;
}
}
}
編譯代碼后第 15 行出現例外:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());// <-------- this
}
}
}
堆疊跟蹤:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)
at System.Windows.Forms.ComboBox.get_Text()
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at WindowsFormsApp.Program.Main() in C:\Users\denis\source\repos\WindowsFormsApp\WindowsFormsApp\Program.cs:line 15
uj5u.com熱心網友回復:
例外意味著集合中沒有專案。在這種情況下,TextUpdate 事件會清除 ComboBox 專案,并且當您在 ComboBox 沒有專案時單擊它時會發生例外。
解決此問題的方法是在 TextUpdate 事件結束時填充 ComboBox,并且僅在文本匹配時才操作下拉串列。如果 listOriginal 包含更新后的文本,則將 newList 添加到 ComboBox 并將 ComboBox.DroppedDown 設定為 true。否則,將 listOriginal 添加到 ComboBox 并將 ComboBox.DroppedDown 設定為 false。
private void comboBx_TextUpdate(object sender, EventArgs e)
{
//clear combobox
this.comboBx.Items.Clear();
//clear listNew
listNew.Clear();
foreach (var item in listOriginal)
{
// call ToLower() .. not case sensitive
if (item.ToLower().Contains(this.comboBx.Text.ToString()))
{
//add to ListNew
listNew.Add(item);
}
}
this.comboBx.SelectionStart = this.comboBx.Text.Length;
Cursor = Cursors.Default;
// Check if ListNew has been populated.
if (listNew.Count != 0)
{
this.comboBx.Items.AddRange(listNew.ToArray());
// Automatically pop up drop-down
this.comboBx.DroppedDown = true;
}
else
{
this.comboBx.Items.AddRange(listOriginal.ToArray());
// Automatically close dropdown
this.comboBx.DroppedDown = false;
}
}
肯定有更好的方法來解決這個錯誤,但我猜這是最簡單的修復。希望能幫助到你!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/509975.html
