Combos have no type-ahead capability?

  • Thread starter Thread starter kiln
  • Start date Start date
K

kiln

I'm very new to .net, using 2003 VB winforms.

Now...am I understanding that the .net winforms comboboxes lack a type-
ahead capability? IE, if you type "h" then "i" then "l" and the list
contains entries like "Hassledorf" and "Hickman" and "Hillage", those
three entries would become the selected entries in turn. I've seen only
meager refs to this capability on the VS and .net newsgroups, and no
built in solution. But having a combo that lacks this capability is like
having half a combo. Is this what everyone is living with? Or are you
supposed to go out and buy and add-in replacement for combos?
 
* kiln said:
Now...am I understanding that the .net winforms comboboxes lack a type-
ahead capability? IE, if you type "h" then "i" then "l" and the list
contains entries like "Hassledorf" and "Hickman" and "Hillage", those

I have never seen a multiselect combobox, except when 'DropDownStyle'
was set to 'Simple'.
 
hirf-spam-me- said:
I have never seen a multiselect combobox, except when 'DropDownStyle'
was set to 'Simple'.
No, wan't requesting a multiselect combo; just one that would "find" the
first best match. Thanks tho...
 
Here is an example which I developed for same purpose ... hope its useful

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text.RegularExpressions;

using System.Windows.Forms;

using System.Diagnostics;



namespace CustomControls

{

#region CustomCombo

/// <summary>

/// Summary description for ComboBox.

/// </summary>

public class CustomCombo : System.Windows.Forms.ComboBox

{

#region Designer Section

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;


#region Default Constructor

public CustomCombo()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call

}

#endregion

#region Dispose

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#endregion


#endregion

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// ComboBox

//

this.Name = "ComboBox";

}

#endregion

#region User Defined variables


/// <summary>

/// Tooltip member to show the tooltip for the combo when the text in the
combo spans out of the

/// visible area of the control

/// </summary>

private ToolTip toolTipCombo = new ToolTip();


#endregion

#region Properties




#endregion

#region ResetControl

public void ResetControl()

{

if(!this.Text.Equals(""))

{

this.Text = "";

}

this.SelectedIndex = -1;

}

#endregion


#endregion

#region Events

#region OnGotFocus

protected override void OnGotFocus(EventArgs e)

{

try

{

if(this.TabIndex == 0)

{

return;

}

foreach( Control ctrl in ((Form)this.TopLevelControl).Controls)

{

if(ctrl.GetType() == typeof(StatusBar))

{

foreach( StatusBarPanel inCtrl in ((StatusBar)ctrl).Panels)

{

inCtrl.Text = "" ;

}

}

}

}

catch( Exception sysExcp )

{


}

finally

{

base.OnGotFocus(e);

}

}

#endregion

protected override void OnKeyDown(KeyEventArgs e)

{

try

{

int index;

string actual;

string found;


if ( ( e.Modifiers == Keys.Control ) || ( e.Modifiers == Keys.Alt ) ||
(e.KeyCode == Keys.Delete)

|| ( e.KeyCode == Keys.Back) || ( e.KeyCode == Keys.Left ) || (e.KeyCode ==
Keys.Right)

|| ( e.KeyCode == Keys.Tab ) || ( e.KeyCode == Keys.Up) || ( e.KeyCode ==
Keys.Down )

|| ( e.KeyCode == Keys.PageUp) || ( e.KeyCode == Keys.PageDown ) ||
(e.KeyCode == Keys.Home)

|| ( e.KeyCode == Keys.End))

{

return;

}

if ( e.KeyCode == Keys.OemMinus )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "-";

}

else

{

if ( e.KeyCode == Keys.D0 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "0";

}

else if ( e.KeyCode == Keys.D1 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "1";

}

else if ( e.KeyCode == Keys.D2 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "2";

}

else if ( e.KeyCode == Keys.D3 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "3";

}

else if ( e.KeyCode == Keys.D4 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "4";

}

else if ( e.KeyCode == Keys.D5 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "5";

}

else if ( e.KeyCode == Keys.D6 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "6";

}

else if ( e.KeyCode == Keys.D7 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "7";

}

else if ( e.KeyCode == Keys.D8 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "8";

}

else if ( e.KeyCode == Keys.D9 )

{

actual = this.Text.Substring( 0, this.SelectionStart ) + "9";

}

else

{

actual = this.Text.Substring( 0, this.SelectionStart ) +
e.KeyCode.ToString();

}

}

index = this.FindString(actual);

if (index > -1)

{

found = this.Items[index].ToString();


this.SelectedIndex = index;

this.SelectionStart = actual.Length;

this.SelectionLength = found.Length;

e.Handled = true;

}

else

{

e.Handled = true;

}

}

catch(Exception ex)

{

MessageBox.Show( ex.ToString() );

}

}

protected override void OnKeyUp(KeyEventArgs e)

{

if ( ( e.Modifiers == Keys.Control ) || ( e.Modifiers == Keys.Alt ) ||
(e.KeyCode == Keys.Delete)

|| ( e.KeyCode == Keys.Back) || ( e.KeyCode == Keys.Left ) || (e.KeyCode ==
Keys.Right)

|| ( e.KeyCode == Keys.Tab ) || ( e.KeyCode == Keys.Up) || ( e.KeyCode ==
Keys.Down )

|| ( e.KeyCode == Keys.PageUp) || ( e.KeyCode == Keys.PageDown ) ||
(e.KeyCode == Keys.Home)

|| ( e.KeyCode == Keys.End))

{

e.Handled = false;

}

else

{

e.Handled = true;

}

}

protected override void OnKeyPress(KeyPressEventArgs e)

{

if ( ( ( ( Keys )e.KeyChar == Keys.Delete) || ( ( Keys )e.KeyChar ==
Keys.Back) || ( ( Keys )e.KeyChar == Keys.Left )

|| (( Keys )e.KeyChar == Keys.Right) || ( ( Keys )e.KeyChar == Keys.Tab ) ||
( ( Keys )e.KeyChar == Keys.Up)

|| ( ( Keys )e.KeyChar == Keys.Down ) || ( ( Keys )e.KeyChar == Keys.PageUp)
|| ( ( Keys )e.KeyChar == Keys.PageDown )

|| (( Keys )e.KeyChar == Keys.Home) || ( ( Keys )e.KeyChar == Keys.End)))

{

e.Handled = false;

}

else

{

e.Handled = true;

}

}



#region OnLeave

/// <summary>

/// Sets/Resets the Selected value/Text in the combo when the focus leaves
the control

/// </summary>

/// <param name="e">EventArgs</param>

protected override void OnLeave(EventArgs e)

{


try

{

string thisText = this.Text.Trim();


if(this.FindString(thisText) != -1 && thisText != "")

{

this.SelectedIndex = this.FindString(this.Text);

}

else

{

this.SelectedIndex = -1;

this.Text = "";

}

}

catch(Exception ex)

{

}

finally

{

base.OnLeave (e);

}

}

#endregion


#region OnMouseMove

protected override void OnMouseMove(MouseEventArgs e)

{

try

{

this.toolTipCombo.Active = true;

ComboBox c = (ComboBox)this;

string s = c.Text.ToString();

Graphics g = c.CreateGraphics();

if(c.Width - 30 < g.MeasureString(s,c.Font).Width)

{

if (s!=this.toolTipCombo.GetToolTip(c).ToString())

{

this.toolTipCombo.SetToolTip(c,s);

}

}

else

{

this.toolTipCombo.SetToolTip(c,"");

}

}

catch(Exception ex)

{


}

finally

{

base.OnMouseMove(e);

}

}

#endregion


#region OnMouseLeave

protected override void OnMouseLeave(EventArgs e)

{

this.toolTipCombo.SetToolTip(this,"");

base.OnMouseLeave (e);

}

#endregion




}

#endregion

}



Regards,

Sanjeeva
 
Back
Top