How can i get the whole text of an item in a listBox?

时间:2015-07-28 16:28:40

标签: c# .net winforms

How can i fix the exception ?

This is my code in form1 constructor:

listBoxSnap.SelectedIndexChanged += listBoxSnap_SelectedIndexChanged;

            img = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
            imgClone = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
            Graphics g;
            using (g = Graphics.FromImage(img))
            {
                g.Clear(Color.White);
            }
            pictureBoxSnap.Image = img;

            buttonSnap.Enabled = false;
            this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
            buttonSnap.Enabled = true;

            textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
            if (this.listBoxSnap.Items.Count > 0)
            {
                listBoxSnap.Select();
                this.listBoxSnap.SetSelected(0, true);
            }

            label1.Select();
            RefreshWindowsList();

            userVideosDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
            WatchDirectory();
            AutoIt.AutoItX.AutoItSetOption("WinTitleMatchMode", 4);

            result = this.listBoxSnap.Items.Cast<string>().Where(x => x.Contains("Game Capture HD")).ToList();

The GetAllWindows method:

public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
        {
            windowSnaps = new WindowSnapCollection();
            countMinimizedWindows = minimized;//set minimized flag capture
            useSpecialCapturing = specialCapturring;//set specialcapturing flag
            EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
            EnumWindows(callback, IntPtr.Zero);
            return new WindowSnapCollection(windowSnaps.ToArray(), true);
        }

I'm getting exception on the line:

result = this.listBoxSnap.Items.Cast<string>().Where(x => x.Contains("Game Capture HD")).ToList();

Unable to cast object of type 'Automatic_Record.WindowSnap' to type 'System.String

System.InvalidCastException was unhandled
  _HResult=-2147467262
  _message=Unable to cast object of type 'Automatic_Record.WindowSnap' to type 'System.String'.
  HResult=-2147467262
  IsTransient=false
  Message=Unable to cast object of type 'Automatic_Record.WindowSnap' to type 'System.String'.
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Automatic_Record.Form1..ctor() in d:\Form1.cs:line 64
       at Automatic_Record.Program.Main() in d:\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1 个答案:

答案 0 :(得分:0)

使用ListControl.GetItemText

var result = this.listBoxSnap.Items.OfType<Automatic_Record.WindowSnap>()
                             .Select(x => this.listBoxSnap.GetItemText(x))
                             .Where(x => x.Contains("Game Capture HD"));