CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 209

209

Chương 6: Windows Form

}

else if (Char.IsControl(e.KeyChar)) {

controlKey = true;

}

else {

controlKey = false;

}

}

// Thực hiện thay thế text.

protected override void OnTextChanged(System.EventArgs e) {

base.OnTextChanged(e);

if (this.Text != "" && !controlKey) {

// Tìm kiếm item trùng khớp.

string matchText = this.Text;

int match = this.FindString(matchText);

// Nếu tìm thấy thì chèn nó vào.

if (match != -1) {

this.SelectedIndex = match;

// Chọn (bôi đen) phần text vừa thêm vào để

// nó có thể được thay thế nếu người dùng kiếp tục gõ.

this.SelectionStart = matchText.Length;

this.SelectionLength =

this.Text.Length - this.SelectionStart;

}

}

}

}

Để thử nghiệm

AutoCompleteComboBox

, bạn có thể tạo một client đơn giản: thêm

ComboBox

vào

form và thêm một số từ (word) vào

ComboBox

. Trong ví dụ này, các từ được lấy từ một file text

ComboBox

được thêm vào form bằng mã lệnh. Bạn cũng có thể biên dịch lớp