Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem faced in AutoComplete code for TextBox
 

Problem faced in AutoComplete code for TextBox

I have written Following code to do AutoComlete in textBox.

AutoCompleteStringCollection data = new AutoCompleteStringCollection();
data.Add("Mahesh Chand");
data.Add("Mac Jocky");
data.Add("Millan Peter");
textBox1.AutoCompleteCustomSource = data;

But i am geting error:

Cast is not Valid,

If anybody knows the reseaon, plz reply.

Regards,

Dev.

Dev24  Thursday, April 19, 2007 6:38 AM
Hi Devendra,

Just create a new Windows Application project in a new solution, Create a new Form and try Kevin's code with that form. See if it works or not. If it doesn't work then there must be some unusual problem with your VS.
decyclone  Friday, April 20, 2007 3:58 PM
Code Snippet
AutoCompleteStringCollection data = new AutoCompleteStringCollection();
data.Add("Mahesh Chand");
data.Add("Mac Jocky");
data.Add("Millan Peter");
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteCustomSource = data;

This code snippet was builded successed in Vs 2005(with WinForms project).
Kevin4732948  Thursday, April 19, 2007 7:01 AM

Ensure that AutoCompleteSource property on textBox1 is set to AutoCompleteSource.CustomSource and AutoCompleteMode property set to Suggest, Append or SuggestAppend value.

Yogesh Prabhu  Thursday, April 19, 2007 7:03 AM

Mr.Kevin,

Thanks for quick response

I am also using VS-2005 , and used your code snippet..

But it gives same error as "Specified cast is not valid."

Thanks agains

Dev

Dev24  Thursday, April 19, 2007 7:16 AM
AutoCompleteStringCollection work in Windows Form. If yourTextbox is a
System.Windows.Forms.TextBox, then it won't display in an asp.net form.
Rather you need to use a System.Web.UI.WebControls.DropDownList which
doesn't support this kind of feature.Or you can use the ASP.NET AJAX AutoComplete Control(http:ajax.asp.net).
Kevin4732948  Thursday, April 19, 2007 7:31 AM

Mr.Kevin

I am using C#.Net for Windows Application only

Please see below code in detail:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Collections.Specialized;

//using ICDInterface;

namespace ICDSv2

{

public partial class frmLogin : Form

{

public frmLogin()

{

InitializeComponent();

}

private void frmLogin_Load(object sender, EventArgs e)

{

AutoCompleteStringCollection data = new AutoCompleteStringCollection();

data.Add("Mahesh Chand");

data.Add("Mac Jocky");

data.Add("Millan Peter");

textBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;

textBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

textBox3.AutoCompleteCustomSource = data;

}

It shows error that Cast is Invalid.

Thanks

Dev.

Dev24  Thursday, April 19, 2007 8:44 AM

There are all codes below:

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestWindForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
AutoCompleteStringCollection data = new AutoCompleteStringCollection();
data.Add("Mahesh Chand");
data.Add("Mac Jocky");
data.Add("Millan Peter");
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteCustomSource = data;
}

private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = this.textBox1.Text;
}
}
}

Maybe an other control or codecause the error.

Kevin4732948  Thursday, April 19, 2007 9:13 AM

No Mr.Kevin,

Still same problem is coming Sad

Dev24  Thursday, April 19, 2007 9:40 AM
Hey Dev24,

Can you provide the Exception's full details, with Message, InnerException, StackTrace etc...
decyclone  Thursday, April 19, 2007 10:56 AM

Thanks Mr,Yogesh Prabhu,

I have already set these properties,

"Specified cast is not valid" message comes.

Please reply.

Thanks

Dev.

Dev24  Thursday, April 19, 2007 11:02 AM
Hey, explain full Exception details...
decyclone  Thursday, April 19, 2007 11:15 AM
Sorry,I do not find outwhich problem cause thiserror. If you wish,you will send your project to me(ctkevin@163.com).
Kevin4732948  Friday, April 20, 2007 12:26 AM
Ok, this sounds like a bit of a wild goose chase. Your code snip-it compiles and runs just fine.

You didn't happen to create your own class named
AutoCompleteStringCollection somewhere in the project did you?

When you compile, which line is highlighted with the error?

Steve Py  Friday, April 20, 2007 2:34 AM
System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.StringSource..ctor(String[] strings)
at System.Windows.Forms.TextBox.SetAutoComplete(Boolean reset)
at System.Windows.Forms.TextBox.set_AutoCompleteCustomSource(AutoCompleteStringCollection value)
at ICDSv2.frmLogin.frmLogin_Load(Object sender, EventArgs e) in D:\v3\Anup\ICDSv2\ICDSv2\frmLogin.cs:line 50
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ICDSv2.Init.Main() in D:\v3\Anup\ICDSv2\ICDSv2\Init.cs:line 50
at System.AppDomain.nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Dev24  Friday, April 20, 2007 12:16 PM

Hi,

Following is the details exception

System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.StringSource..ctor(String[] strings)
at System.Windows.Forms.TextBox.SetAutoComplete(Boolean reset)
at System.Windows.Forms.TextBox.set_AutoCompleteCustomSource(AutoCompleteStringCollection value)
at ICDSv2.frmLogin.frmLogin_Load(Object sender, EventArgs e) in D:\v3\Anup\ICDSv2\ICDSv2\frmLogin.cs:line 50
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ICDSv2.Init.Main() in D:\v3\Anup\ICDSv2\ICDSv2\Init.cs:line 50
at System.AppDomain.nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Dev24  Friday, April 20, 2007 12:19 PM
Hi Devendra,

Just create a new Windows Application project in a new solution, Create a new Form and try Kevin's code with that form. See if it works or not. If it doesn't work then there must be some unusual problem with your VS.
decyclone  Friday, April 20, 2007 3:58 PM
Hi,

I am facing the same problem : InvalidCastException, as soon as I put at least one item in the AutoCompleteStringCollection. This occurs in my main project, not on test projects I made to try to identify the bug. And it used to work fine :-(

Devendra, do you remember how you fixed this problem ?

Thanks for any help

Denis
dcOpsys  Wednesday, May 07, 2008 3:21 PM

I'm having the same issue with the InvalidCastException when adding an item to AutoCompleteStringCollection.

Theform that contains the textboxis called from two different places. The one that doesn't work is called into a new thread using the following code (NewNote() is called from a button on the main form):

private void NewNote()

{

System.Threading.Thread newThread = new System.Threading.Thread(OpenNewNote);

newThread.IsBackground = false;

newThread.SetApartmentState(System.Threading.ApartmentState.MTA);

newThread.Start();

}

public void OpenNewNote()

{

Customer addCustomer = new Customer();

Application.Run(addCustomer);

// addCustomer.ShowDialog();

}

When I call theCustomer formusing addCustomer.ShowDialog() the AutoComplete textbox works fine.

Is there a way around this? I really need to be able to call the Customer form into a new thread.

Mobes  Tuesday, May 27, 2008 5:22 AM

Ok I've done some more testing and playing around with the ApartmentState.I'm new to forms developmentand multithreading so I'm not really sure what thatdoes.I switched to MTA and I started getting strange results. The single threaded form stopped working and thenew threaded form started working. So then I switched back to STA and it all started working. I'm really confused now but at least it's working.

Mobes  Tuesday, May 27, 2008 6:07 AM

I am having the same issue...with a slight twist. My code works fine within a normal .Net application, but when I attempt to do some automated testing using nUnit I get the error. Since the other posts seemed to point towards theading issues I tried adding a Synclock to the textbox, but it didn't have any effect. I'm sumped...any help would be REALLY appreciated.

System.InvalidCastException : Specified cast is not valid

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.StringSource..ctor(String[] strings)
at System.Windows.Forms.TextBox.SetAutoComplete(Boolean reset)
at System.Windows.Forms.TextBox.set_AutoCompleteCustomSource(AutoCompleteStringCollection value)
at RCIS.Windows.Forms.SharedControls.AutoComplete.AutoCompleteControllerBase`1.BuildAutoCompleteList() in D:\Sandboxes\$$\Cimax.NET.Archive\Cimax.NET_AppEntry_1504\SOURCE\CustomControls\SharedControls\AutoCompleController\AutoCompleteControllerBase.vb:line 301
at RCIS.Windows.Forms.SharedControls.AutoComplete.AutoCompleteControllerBase`1.SetSourceList(IEnumerable`1 listInstance) in D:\Sandboxes\$$\Cimax.NET.Archive\Cimax.NET_AppEntry_1504\SOURCE\CustomControls\SharedControls\AutoCompleController\AutoCompleteControllerBase.vb:line 178
at PolicySetupBlockExperiment.PolicySetupBlock.Initialize() in D:\Sandboxes\ControlsTest\PolicySetupBlockExperiment\PolicySetupBlock.vb:line 91
at PolicySetupBlockExperiment.PolicySetupBlock.SetCurrentPolicy(Policy& CurrentPolicy, ReferenceTypeCache& CurrentReferenceTypeCache) in D:\Sandboxes\ControlsTest\PolicySetupBlockExperiment\PolicySetupBlock.vb:line 123
at Cimax.Exe.Navigator.UnitTests.ViewTest2.DoTest() in D:\Sandboxes\$$\Cimax.NET.Archive\Cimax.NET_AppEntry_1504\SOURCE\UnitTests\Cimax.Exe.Navigator.UnitTests\ViewTest2.vb:line 28

DMatheson  Friday, June 27, 2008 3:55 PM
I am having the same problem, anyone have a suggestion?
MrAnubis  Monday, August 31, 2009 5:09 AM
Hi Mr. Anubis,

the rease in my case was the handling of the form in an thread. Let me explain:

Opening the Form (here: a Logon-Screen) in a thread (thread ... addressof ... thread.start) causes the error 8(
Opening the Form "in main-thread" works. 8)

Bye!
NichtDieMama  Friday, September 11, 2009 6:23 AM

You can use google to search for other answers

Custom Search

More Threads

• How to add button to TabControl?
• add image to listview subitem
• How to convert a page from PDF file into Image
• Find a window control at a point on the screen
• Why does WebBrowser enlarge the content text?!
• When to do custom drawing on panel?
• Print from a windows form
• Scrolling/Redrawing issue when using a FlowLayoutPanel
• invalid operation exception
• how to insert blank in combobox after databound