Created
September 16, 2020 13:32
-
-
Save jayu108/482b9c0b8e6ed05a87a8192c442bb68d to your computer and use it in GitHub Desktop.
custom popup listbox window
This file contains 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.Drawing; | |
using System.Windows.Forms; | |
namespace customPopupListBox | |
{ | |
public partial class Form1 : Form | |
{ | |
ListBox listBox1 = new ListBox(); | |
Size listBoxSize; | |
public Form1() | |
{ | |
InitializeComponent(); | |
Console.WriteLine($" this.listBox1.Font.Name == {this.listBox1.Font.Name}"); | |
Console.WriteLine($" this.listBox1.Font.Size == {this.listBox1.Font.Size}"); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
SizeF sizeF; | |
// listBox1 크기 자동 설정용. | |
using (Graphics g = this.CreateGraphics()) | |
{ | |
sizeF = g.MeasureString("한", this.listBox1.Font); // 기본 1글자 height, width 구함. | |
//Console.WriteLine($"sizeF.Width = {sizeF.Width}, sizeF.Height={sizeF.Height}"); | |
} | |
int stringLengthMax = 1; // 초기화 -- 가장 긴 글자길이 | |
// Shutdown the painting of the ListBox as items are added. | |
listBox1.BeginUpdate(); | |
int itemCount = 5; | |
for (int x = 1; x <= itemCount; x++) | |
{ | |
var txt = "Item " + x.ToString(); | |
stringLengthMax = (txt.Length > stringLengthMax) ? txt.Length : stringLengthMax; | |
listBox1.Items.Add(txt); | |
} | |
// Allow the ListBox to repaint and display the new items. | |
listBox1.EndUpdate(); | |
int height = (int)(itemCount * sizeF.Height) + 1; // 형변환시, 소수점이하 짤리는 경우를 고려해서 +1 해줌. | |
int width = (int)(stringLengthMax * sizeF.Width) + 1; // 형변환시, 소수점이하 짤리는 경우를 고려해서 +1 해줌. | |
// listBox1 크기 설정하기. | |
listBoxSize = new Size(width, height); | |
Console.WriteLine($"listBox1.Size = {listBox1.Size}"); | |
Console.WriteLine($"new listBox1.Size => height={height}, width={width}"); | |
} | |
private void Form1_MouseUp(object sender, MouseEventArgs e) | |
{ | |
Control c = sender as Control; | |
if (e.Button == MouseButtons.Right) | |
{ | |
listBox1.MinimumSize = listBoxSize; //new Size(120, 88); | |
listBox1.Size = listBoxSize; //new Size(120, 88); | |
var popup = new PopupWindow(listBox1); | |
popup.Show(c, e.Location); | |
//popup.Show(c, e.Location, this.dropDownDirection); | |
} | |
} | |
} | |
public class PopupWindow : System.Windows.Forms.ToolStripDropDown | |
{ | |
private System.Windows.Forms.Control _content; | |
private System.Windows.Forms.ToolStripControlHost _host; | |
public PopupWindow(System.Windows.Forms.Control content) | |
{ | |
//Basic setup... | |
this.AutoSize = false; | |
this.DoubleBuffered = true; | |
this.ResizeRedraw = true; | |
this._content = content; | |
this._host = new System.Windows.Forms.ToolStripControlHost(content); | |
//Positioning and Sizing | |
this.MinimumSize = content.MinimumSize; | |
this.MaximumSize = content.Size; | |
//this.SetMinimumSize(new Size(200, 200)); | |
this.Size = content.Size; | |
this.Margin = Padding.Empty; | |
this.Padding = Padding.Empty; | |
this._host.Margin = Padding.Empty; | |
this._host.Padding = Padding.Empty; | |
content.Location = Point.Empty; | |
//Console.WriteLine($"MinimumSize={MinimumSize}, MaximumSize={MaximumSize}, content.Size={content.Size}"); | |
//Add the host to the list | |
this.Items.Add(this._host); | |
} | |
public void SetMinimumSize(Size size) | |
{ | |
this.MinimumSize = size; | |
} | |
public void SetMaximumSize(Size size) | |
{ | |
this.MaximumSize = size; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment