Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / Visual Basic
Article

Resizable ComboBox

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
21 Feb 2011CPOL1 min read 36.8K   1.4K   12   7
Resizable ComboBox

Introduction

When I create a project for using a DataGrid with ComboBox, I hope to get a resizable ComboBox to set its Height as the Height of DataGrid cell. This is my trial to create a resizable ComboBox ActiveX, now I can change its Height. Some programmers use Form, ListBox, TextBox and Button to create ComboBox, but I am using Panel, ComboBox, TextBox and Button to create my ActiveX.

Background

I am using Panel to hide the upper part of ComboBox and using this Panel as container for TextBox and Button.

About the Project of my ActiveX:

Name of the Project is: MKCombo
Name of UserControl is: KCombo
Name of Panel is: ComboHide
Name of ComboBox is: ComboCtrl
Name of TextBox is: ComboText
Name of Button is: ComboButton

After you expand the files: Combo_C#.zip and Combo_VB.zip, you can open 'SizableCombo' solution to read the code of my ActiveX in the 'MKCombo' project and read the code to test the ActiveX in the 'SizableCombo' project. I try to use 'DataSource' to bind my ActiveX with data but I can't, then I use another idea.

My ActiveX has:

Event: cmbSelectedChanged for SelectedIndexChanged event.
Property: cmbBorderStyle for BorderStyle property.
Property: cmbSelectedIndex for SelectedIndex property.
Property: cmbSelectedItem for SelectedItem property.
Property: cmbText for Text property.
Property: cmbItemsCount for Items.Count property.
Method: cmbAddItem for Items.Add method.
Method: cmbClear for Items.Clear method.

Using the Code

C# code to bind the ActiveX with data:

C#
public void cmbBindData(string cmbSql, OleDbConnection cmbCnn, string cmbField)
{ 
    if (cmbCnn.State == ConnectionState.Open)
        cmbCnn.Close(); 
    cmbCnn.Open(); 
    OleDbCommand cmdReader = new OleDbCommand(cmbSql, cmbCnn);
    OleDbDataReader datRdr = cmdReader.ExecuteReader(); 
    ComboCtrl.Items.Clear(); 
    while (datRdr.Read())
    { 
        ComboCtrl.Items.Add(datRdr[cmbField]); 
    } 
    ComboCtrl.SelectedIndex = 0;
} 

VB code to bind the ActiveX with data:

VB.NET
Public Sub cmbBindData(ByVal cmbSql As String, _
    ByVal cmbCnn As OleDbConnection, ByVal cmbField As String)
    If (cmbCnn.State = ConnectionState.Open) Then cmbCnn.Close()
    cmbCnn.Open()
    Dim cmdReader As OleDbCommand = New OleDbCommand(cmbSql, cmbCnn)
    Dim datRdr As OleDbDataReader = cmdReader.ExecuteReader()
 
    ComboCtrl.Items.Clear()
    While (datRdr.Read())
       ComboCtrl.Items.Add(datRdr(cmbField))
    End While 
   ComboCtrl.SelectedIndex = 0
End Sub

Remark

When executing the test project, please resize any row to see how the ComboBox changes its Height.

Final Words

I hope this article is useful. If you have any ideas, please tell me. Thanks to CodeProject and thanks to all.

Mostafa Kaisoun
m_kaisoun@hotmail.com 

History

  • 21st February, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 11:06
professionalKanasz Robert27-Sep-12 11:06 
GeneralRe: My vote of 5 Pin
Mostafa Kaisoun27-Sep-12 11:58
Mostafa Kaisoun27-Sep-12 11:58 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Mar-12 1:01
professionalManoj Kumar Choubey26-Mar-12 1:01 
GeneralMy vote of 5 Pin
gorgias991-Mar-11 23:07
gorgias991-Mar-11 23:07 
GeneralRe: My vote of 5 Pin
Mostafa Kaisoun18-Apr-11 11:38
Mostafa Kaisoun18-Apr-11 11:38 
GeneralMy vote of 5 Pin
Monjurul Habib28-Feb-11 20:14
professionalMonjurul Habib28-Feb-11 20:14 
GeneralRe: My vote of 5 Pin
Mostafa Kaisoun18-Apr-11 11:39
Mostafa Kaisoun18-Apr-11 11:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.