C#
C# -- listbox 에서 선택해제하기 (deselect)
자유프로그램
2020. 8. 17. 15:50
반응형
C# -- listbox 에서 선택해제하기 (deselect)
참고 : https://truepia.tistory.com/99
TextBox 의 autocomplete 를 직접 구현하기위해 코딩중에...
ListBox 에서 선택해제(deselect)를 해야 하는 상황 발생함.
this.listBox1.SelectionMode = SelectionMode.None;
을 사용할수 있지만, 그러면 화면깜박임 발생하는 경우있음.
그리고, 그냥
this.listBox1.SelectedIndex = -1;
을 입력하면, 선택해제가 안됨...
열심히 구글링하다가.. 위의 참고 블러그 찾아서..
변형하여 해결함.
< 해결책 >
this.listBox1.BeginInvoke(new Action(() =>
{
//this.listBox1.SelectedItem = null;
this.listBox1.SelectedIndex = -1;
}));
< 실행화면 >
< 소스 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
namespace listbox_deselect | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
this.listBox1.Items.Add(1); | |
this.listBox1.Items.Add(2); | |
this.listBox1.Items.Add(566); | |
this.listBox1.Items.Add("test"); | |
this.listBox1.Items.Add(5); | |
this.listBox1.Items.Add(337); | |
this.listBox1.Items.Add(64); | |
this.listBox1.Items.Add(7); | |
} | |
private void buttonSelect_Click(object sender, EventArgs e) | |
{ | |
//this.listBox1.SelectionMode = SelectionMode.One; | |
this.listBox1.SelectedIndex = 2; | |
} | |
private void buttonDeselect_Click(object sender, EventArgs e) | |
{ | |
//this.listBox1.SelectionMode = SelectionMode.None; | |
//this.listBox1.SelectedItem = null; | |
//this.listBox1.SelectedIndex = -1; | |
this.listBox1.BeginInvoke(new Action(() => | |
{ | |
//this.listBox1.SelectedItem = null; | |
this.listBox1.SelectedIndex = -1; | |
})); | |
} | |
} | |
} |
반응형