{"id":592,"date":"2024-02-23T11:43:45","date_gmt":"2024-02-23T02:43:45","guid":{"rendered":"https:\/\/www.mobile-sta.com\/wordpress\/?p=592"},"modified":"2024-02-23T13:41:08","modified_gmt":"2024-02-23T04:41:08","slug":"desktop%e3%81%a7%e3%80%81%e8%87%aa%e4%bd%9ccombobox","status":"publish","type":"post","link":"https:\/\/www.mobile-sta.com\/wordpress\/?p=592","title":{"rendered":"DeskTop\u3067\u3001\u81ea\u4f5cComboBox!"},"content":{"rendered":"\n<p>Desktop\u30a2\u30d7\u30ea\u958b\u767a\u3067\u3001\u4e00\u756a\u60a9\u3080\u306e\u304c\u3001dataGridView\u95a2\u4fc2\u3068comboBox\u304b\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de\u306f\u3001\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u306e\u4e00\u4f8b\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002WinForms\u306b\u306f\u3001\u6709\u6599\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u6570\u591a\u304f\u3042\u308a\u307e\u3059\u304c\u3001\u7d50\u69cb\u306a\u304a\u5024\u6bb5\u306a\u306e\u3068\u3001\u79c1\u306e\u9577\u5e74\u306e\u7d4c\u9a13\u3068\u3057\u3066\u3001\u610f\u5916\u3068\u30d0\u30b0\u304c\u3042\u3063\u305f\u308a\u3057\u3066\u3001\u305d\u308c\u3092\u88dc\u5b8c\u3057\u306a\u304c\u3089\u306e\u958b\u767a\u3082\u7d50\u69cb\u3001\u5de5\u6570\u304c\u5897\u3048\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u306b\u3064\u3044\u3066\uff11\u30d1\u30bf\u30fc\u30f3\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u30023\u5217\u306e\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u3067\u3001\u30c7\u30fc\u30bf\u3092List&lt;DataCombo&gt;\u5f62\u5f0f\u3067\u6e21\u3057\u3066\u3001\u623b\u308a\u5024\u3068\u3057\u3066\u3001\u9078\u629e\u3057\u305fID(1\u5217\u76ee)\u3068NAME(\u4e8c\u5217\u76ee)\u3092\u53d6\u5f97\u3059\u308b<span style=\"color: #0000ff;\"><strong>\u30e6\u30fc\u30b6\u30fc\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb<\/strong><\/span>\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002(\u4e0b\u8a18\u30a4\u30e1\u30fc\u30b8)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"539\" height=\"432\" src=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll-1.jpg\" alt=\"\" class=\"wp-image-598\" srcset=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll-1.jpg 539w, https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll-1-300x240.jpg 300w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p style=\"text-align: center;\">CustomCombo.cs (\u30e6\u30fc\u30b6\u30fc\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb)\u30af\u30e9\u30b9\u3092\u4e00\u3064\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"536\" src=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1-1024x536.jpg\" alt=\"\" class=\"wp-image-612\" srcset=\"https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1-1024x536.jpg 1024w, https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1-300x157.jpg 300w, https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1-768x402.jpg 768w, https:\/\/www.mobile-sta.com\/wordpress\/wp-content\/uploads\/2024\/02\/usercontroll3-1.jpg 1418w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u2193\u30e6\u30fc\u30b6\u30fc\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb(\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9)\u306eClass\u30bd\u30fc\u30b9<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\nusing static System.Windows.Forms.VisualStyles.VisualStyleElement;\n\nnamespace WinForms01\n{\n    \/\/\/ --------------------------------------------------------------------------------\n    \/\/\/ &lt;summary>\n    \/\/\/ \u4f5c\u6210: \u30e2\u30d0\u30a4\u30eb\u30b9\u30c6\u30fc\u30b7\u30e7\u30f3\u30002024.02.23 M.SAGARA\n    \/\/\/ &lt;\/summary>\n    \/\/\/ --------------------------------------------------------------------------------\n    public partial class CustomCombo : UserControl\n    {\n        private System.Windows.Forms.TextBox textBox;\n        private System.Windows.Forms.Button dropDownButton;\n        private Form dropDownForm;\n        private DataGridView dataGridView;\n\n        public string sNAME = \"\";\n\n        \/\/ \u30ab\u30b9\u30bf\u30e0\u30a4\u30d9\u30f3\u30c8\u306e\u5b9a\u7fa9\n        public event EventHandler TextBoxTextChanged;\n\n        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u30fc\n        \/\/\/ &lt;\/summary>\n        \/\/\/ --------------------------------------------------------------------------------\n        public CustomCombo()\n        {\n            InitializeComponent();\n            InitializeCustomComboBox();\n            InitializeDropDownFormWithDataGridView();\n            \/\/ dataGridView.CellClick\u30a4\u30d9\u30f3\u30c8\u3092\u8cfc\u8aad\n            this.dataGridView.CellClick += MyTextBox_TextChanged;\n        }\n\n        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ \u5916\u90e8\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9 \n        \/\/\/ \u203bDataCombo (Id,Name,Descripton)\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"items\">&lt;\/param>\n        \/\/\/ --------------------------------------------------------------------------------\n        public void SetData(List&lt;DataCombo> items)\n        {\n            dataGridView.DataSource = items;\n            dataGridView.Refresh();\n        }\n\n        \/\/ TextBox\u306eTextChanged\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\n        private void MyTextBox_TextChanged(object sender, EventArgs e)\n        {\n            \/\/ \u30ab\u30b9\u30bf\u30e0\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u706b\n            TextBoxTextChanged?.Invoke(sender, e);\n        }\n        \/\/ TextBox\u306e\u30c6\u30ad\u30b9\u30c8\u3092\u5916\u90e8\u304b\u3089\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\n        public string TextBoxText\n        {\n            get { return textBox.Text; }\n            set { textBox.Text = value; }\n        }\n\n        private void InitializeCustomComboBox()\n        {\n            \/\/ TextBox\u306e\u8a2d\u5b9a\n            textBox = new System.Windows.Forms.TextBox\n            {\n                Dock = DockStyle.Fill,\n                ReadOnly = true\n            };\n            this.Controls.Add(textBox);\n\n            \/\/ \u30c9\u30ed\u30c3\u30d7\u30c0\u30a6\u30f3\u30dc\u30bf\u30f3\u306e\u8a2d\u5b9a\n            dropDownButton = new System.Windows.Forms.Button\n            {\n                Dock = DockStyle.Right,\n                Text = \"\u25bc\",\n                Width = 24,  \/\/ \u30dc\u30bf\u30f3\u306e\u5e45\u309224\u30d4\u30af\u30bb\u30eb\u306b\u8a2d\u5b9a\n                Height = 24\n            };\n            this.Controls.Add(dropDownButton);\n            dropDownButton.Click += (s, e) => ShowDropDownForm();\n\n            \/\/ \u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306e\u9ad8\u3055\u8abf\u6574\n            \/\/ \u30dc\u30bf\u30f3\u306e\u9ad8\u3055\u3092\u8abf\u6574\u3059\u308b\u5834\u5408\u306f\u3001CustomComboBoxControl\u306e\u30b5\u30a4\u30ba\u8abf\u6574\u3092\u884c\u3046\n            this.Height = textBox.Height; \/\/ \u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306e\u9ad8\u3055\u3092\u8abf\u6574\n        }\n\n        private void InitializeDropDownFormWithDataGridView()\n        {\n            \/\/ dataGridView\u306e\u8a2d\u5b9a\n            dataGridView = new DataGridView\n            {\n                Dock = DockStyle.Fill,\n                SelectionMode = DataGridViewSelectionMode.FullRowSelect,\n                AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells,\n                AllowUserToResizeRows = false,\n                RowHeadersVisible = false, \/\/ \u884c\u30d8\u30c3\u30c0\u30fc\u3092\u975e\u8868\u793a\u306b\u8a2d\u5b9a\n                ReadOnly = true,\n                AllowUserToAddRows = false\n            };\n            \/\/ DataGridView\u306e\u884c\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u9ad8\u3055\u3092\u8a2d\u5b9a\n            dataGridView.RowTemplate.Height = 18;\n\n            dropDownForm = new Form\n            {\n                FormBorderStyle = FormBorderStyle.None,\n                StartPosition = FormStartPosition.Manual,\n                Size = new Size(300, 300),  \/\/ \u521d\u671f\u30b5\u30a4\u30ba\u8a2d\u5b9a\n                TopMost = true\n            };\n            dropDownForm.Controls.Add(dataGridView);\n            dropDownForm.Deactivate += (s, e) => dropDownForm.Hide();\n            dataGridView.CellClick += DataGridView_CellClick;\n        }\n\n        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ dataGridView\u306e\u90e8\u5206\u304c\u8868\u793a\u3055\u308c\u308b\u3068\u304d\u306e\u3001\u30a4\u30d9\u30f3\u30c8\n        \/\/\/ &lt;\/summary>\n        \/\/\/ --------------------------------------------------------------------------------\n        private void ShowDropDownForm()\n        {\n            \/\/ \u73fe\u5728\u306e\u30b9\u30af\u30ea\u30fc\u30f3\u306e\u4f5c\u696d\u9818\u57df\u3092\u53d6\u5f97\uff08\u30bf\u30b9\u30af\u30d0\u30fc\u306a\u3069\u3092\u9664\u3044\u305f\u9818\u57df\uff09\n            Rectangle screenWorkingArea = Screen.GetWorkingArea(this);\n\n            \/\/ ComboBox\u306e\u30b9\u30af\u30ea\u30fc\u30f3\u4e0a\u306e\u4f4d\u7f6e\u3092\u53d6\u5f97\n            Point comboBoxScreenLocation = this.textBox.PointToScreen(Point.Empty);\n\n            \/\/ Form\u306e\u4e88\u5b9a\u8868\u793a\u4f4d\u7f6e\uff08ComboBox\u306e\u76f4\u4e0b\uff09\u3092\u8a08\u7b97\n            int formX = comboBoxScreenLocation.X;\n            int formY = comboBoxScreenLocation.Y + textBox.Height;\n\n            \/\/ Form\u306e\u4e88\u5b9a\u8868\u793a\u4f4d\u7f6e\u304c\u753b\u9762\u4e0b\u304b\u3089\u306f\u307f\u51fa\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\n            bool isBelowScreen = (formY + dropDownForm.Height) > screenWorkingArea.Bottom;\n\n            if (dropDownForm.Visible)\n            {\n                dropDownForm.Hide();\n            }\n            else\n            {\n                if (isBelowScreen)\n                {\n                    \/\/ \u753b\u9762\u4e0b\u304b\u3089\u306f\u307f\u51fa\u308b\u5834\u5408\u3001Form\u3092ComboBox\u306e\u4e0a\u306b\u8868\u793a\n                    formY = comboBoxScreenLocation.Y - dropDownForm.Height;\n                }\n\n                \/\/ Form\u306e\u4f4d\u7f6e\u3092\u8a2d\u5b9a\u3057\u3066\u8868\u793a\n                dropDownForm.Location = new Point(formX, formY);\n                dropDownForm.Show();\n            }\n\n            \/\/ dataGridView\u306e\u5217\u5e45\u306e\u8a08\u7b97\n            int totalWidth = 0;\n            foreach (DataGridViewColumn column in dataGridView.Columns)\n            {\n                totalWidth += column.Width;\n            }\n            dropDownForm.Width = totalWidth + 20;\n        }   \/\/ EOF ShowDropDownForm\n\n        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ \u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u306e\u884c\u9078\u629e\u6642\u3001\u30a4\u30d9\u30f3\u30c8\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        \/\/\/ --------------------------------------------------------------------------------\n        private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)\n        {\n            if (e.RowIndex >= 0)\n            {\n                if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)\n                {\n                    \/\/ \u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306b\u9078\u629e\u5024\u306e\u30bb\u30c3\u30c8\n                    \/\/textBox.Text = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();\n                    textBox.Text = dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();\n\n                    sNAME = dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();\n                }\n                dropDownForm.Hide();\n            }\n        }\n    }   \/\/ EOF Class\n}   \/\/ EOF namespace<\/pre>\n\n\n\n<p><strong>Form(\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u3092\u914d\u7f6e\u3057\u305f)\u306e\u30bd\u30fc\u30b9<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ \u30d5\u30a9\u30fc\u30e0\u8aad\u307f\u8fbc\u307f\u6642\u3001\u30a4\u30d9\u30f3\u30c8\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        \/\/\/ --------------------------------------------------------------------------------\n        private void FrmMain_Load(object sender, EventArgs e)\n        {\n            \/\/ \u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u306e\u30c7\u30fc\u30bf\u306e\u6e96\u5099\n            List&lt;DataCombo> items = new List&lt;DataCombo>();\n            for (int i = 0; i &lt; 100; i++)\n            {\n                DataCombo row = new DataCombo\n                {\n                    Id = i.ToString(\"D4\"),\n                    Name = \"Item \" + i.ToString(),\n                    Description = \"Description \" + i.ToString()\n                };\n                items.Add(row);\n            };\n            customCombo1.SetData(items);\n\n            boLoading = false;\n        }\n\n        \/\/\/ --------------------------------------------------------------------------------\n        \/\/\/ &lt;summary>\n        \/\/\/ \u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u3067\u3001\u884c\u9078\u629e\u6642\u30a4\u30d9\u30f3\u30c8\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        \/\/\/ --------------------------------------------------------------------------------\n        private void customCombo1_TextBoxTextChanged(object sender, EventArgs e)\n        {\n            \/\/ \u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u3067\u3001\u9078\u629e\u3055\u308c\u305f\u884c\u306e\u30012\u5217\u76ee\u306e\u53d6\u5f97\n            lblComboName.Text = customCombo1.sNAME;\n        }<\/pre>\n\n\n\n<center><a href=\"https:\/\/px.a8.net\/svt\/ejp?a8mat=3TLC2K+69NB1U+3HKU+1NL281\" rel=\"nofollow\">\n<img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"300\" height=\"250\" alt=\"\" src=\"https:\/\/www26.a8.net\/svt\/bgt?aid=231102812379&#038;wid=001&#038;eno=01&#038;mid=s00000016275010008000&#038;mc=1\"><\/a>\n<img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"1\" height=\"1\" src=\"https:\/\/www14.a8.net\/0.gif?a8mat=3TLC2K+69NB1U+3HKU+1NL281\" alt=\"\"><\/center>\n","protected":false},"excerpt":{"rendered":"<p>Desktop\u30a2\u30d7\u30ea\u958b\u767a\u3067\u3001\u4e00\u756a\u60a9\u3080\u306e\u304c\u3001dataGridView\u95a2\u4fc2\u3068comboBox\u304b\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de\u306f\u3001\u30b3\u30f3\u30dc\u30dc\u30c3\u30af\u30b9\u306e\u4e00\u4f8b\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002WinForms\u306b\u306f\u3001\u6709\u6599\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u6570\u591a\u304f\u3042\u308a\u307e\u3059\u304c\u3001\u7d50\u69cb &hellip; <a href=\"https:\/\/www.mobile-sta.com\/wordpress\/?p=592\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">DeskTop\u3067\u3001\u81ea\u4f5cComboBox!<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-592","post","type-post","status-publish","format-standard","hentry","category-1"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=592"}],"version-history":[{"count":15,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mobile-sta.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}