Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > WinApp C# เขียนโค้ด ButtonCollections แล้ว แต่อยากเลือกแก้ไข ปุ่มทีละปุ่มได้ เหมือนปุ่มปกติครับ


 

[.NET] WinApp C# เขียนโค้ด ButtonCollections แล้ว แต่อยากเลือกแก้ไข ปุ่มทีละปุ่มได้ เหมือนปุ่มปกติครับ

 
Topic : 131678



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook


โค้ดครับ

Code (C#)
001.    public class ButtonCollections : UserControl
002.{
003.    public ButtonCollections()
004.    {
005.        RefreshGrid();
006.    }
007.    int sizeX = 10; int sizeY = 10;
008.    int rowCount = 2;
009.    int ColumnCount = 2;
010.    Size _ButtonSize = new Size(300, 140);
011.    Color _ButtonColor =  System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
012.    Color _ButtonForeColor = System.Drawing.Color.Blue;
013.    public void RefreshGrid()
014.    {
015.        if (rowCount <= 0 || ColumnCount <= 0) return;
016.        this.Controls.Clear();
017.        int X = 20, Y = 20;
018.        int row = 0, column = 0;
019.        for (int i = 1; i <= rowCount * ColumnCount; i++)
020.        {
021.            Button btn = new Button();
022.            btn.BackColor = _ButtonColor;
023.            btn.Font = this.Font;
024.            btn.ForeColor = _ButtonForeColor;
025.            btn.Location = new System.Drawing.Point(X, Y);
026.            btn.Name = "button" + column + "_" + row;
027.            btn.Size = _ButtonSize;
028.            btn.Text = btn.Name;
029.            btn.UseVisualStyleBackColor = false;
030.            btn.Click += new EventHandler(buttonChoie_Click);
031.            X += _ButtonSize.Width + sizeX;
032.            this.Controls.Add(btn);
033.            column++;
034.            if (i % ColumnCount == 0)
035.            {
036.                Y += _ButtonSize.Height + sizeY; X = 20;
037.                column = 0;
038.                row++;
039.            }
040. 
041.        }
042.    }
043. 
044.    private void buttonChoie_Click(object sender, EventArgs e)
045.    {
046.        ButtonSelect = sender as Button;
047.    }
048. 
049. 
050.    #region _Property
051.    [System.ComponentModel.Browsable(true)]
052.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
053.    [System.ComponentModel.Category("TOR Setting")]
054.    public Font ButtonFont
055.    {
056.        get { return this.Font; }
057.        set
058.        {
059.            this.Font = value;
060.            RefreshGrid();
061.        }
062.    }
063.    [System.ComponentModel.Browsable(true)]
064.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
065.    [System.ComponentModel.Category("TOR Setting")]
066.    public Color ButtonColor
067.    {
068.        get { return _ButtonColor; }
069.        set
070.        {
071.            _ButtonColor = value;
072.            RefreshGrid();
073.        }
074.    }
075.    [System.ComponentModel.Browsable(true)]
076.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
077.    [System.ComponentModel.Category("TOR Setting")]
078.    public Color ButtonForeColor
079.    {
080.        get { return _ButtonForeColor; }
081.        set
082.        {
083.            _ButtonForeColor = value;
084.            RefreshGrid();
085.        }
086.    }
087.     
088.    [System.ComponentModel.Browsable(true)]
089.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
090.    [System.ComponentModel.DefaultValue(2)]
091.    [System.ComponentModel.Category("TOR Setting")]
092.    public int rowCounts
093.    {
094.        get { return rowCount; }
095.        set
096.        {
097.            rowCount = value;
098.            RefreshGrid();
099.        }
100.    }
101.    [System.ComponentModel.Browsable(true)]
102.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
103.    [System.ComponentModel.Category("TOR Setting")]
104.    public Size ButtonSize
105.    {
106.        get { return _ButtonSize; }
107.        set
108.        {
109.            _ButtonSize = value;
110.            RefreshGrid();
111.        }
112.    }
113.    [System.ComponentModel.Browsable(true)]
114.    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
115.    [System.ComponentModel.DefaultValue(2)]
116.    [System.ComponentModel.Category("TOR Setting")]
117.    public int ColumnCounts
118.    {
119.        get { return ColumnCount; }
120.        set
121.        {
122.            ColumnCount = value;
123.            RefreshGrid();
124.        }
125.    }
126.     public Button Item(int row,int column)
127.    {
128.       return  this.Controls.Find("button" + column + "_" + row,true).FirstOrDefault() as Button;
129.    }
130.    public virtual void Clear()
131.    {
132.        this.Controls.Clear();
133.    }
134.    #endregion
135.    public Button ButtonSelect;
136.    public event EventHandler ButtonClick
137.    {
138.        add
139.        {
140.            for (int r = 0; r < rowCount; r++)
141.            {
142.                for (int c = 0; c < ColumnCount; c++)
143.                {
144.                    if (value != null && Item(r, c) != null)
145.                    Item(r, c).Click += value;
146.                }
147.            }
148.        }
149.        remove
150.        {
151.            for (int r = 0; r < rowCount; r++)
152.            {
153.                for (int c = 0; c < ColumnCount; c++)
154.                {
155.                    if (value != null && Item(r, c) != null)
156.                    Item(r, c).Click -= value;
157.                }
158.            }
159.        }
160.    }
161.     
162.}


ฆฏ


คืออยากจะแก้ไขทีละปุ่มได้ ตรงหน้า Design เหมือนกับที่เราแก้ใน DataGridView อ่าครับ

232

ปล.อยากทำแบบ OutoCopy ด้วยครับ
อย่างเช่น ใส่ test แล้วให้ รัน test_1,test_2,test_3...... จนกว่าจะครบ



Tag : .NET, C#, VS 2013 (.NET 4.x)



ประวัติการแก้ไข
2018-08-03 10:46:52
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2018-08-03 10:41:03 By : lamaka.tor View : 890 Reply : 3
 

 

No. 1



โพสกระทู้ ( 9,590 )
บทความ ( 2 )



สถานะออฟไลน์


แต่อยากเลือกแก้ไข ปุ่มทีละปุ่มได้ เหมือนปุ่มปกติครับ

ต้องการแก้ไข property ของปุ่ม หรือแก้ไข ข้อมูลที่ปุ่มอ้างอิงถีง ครับ

ถ้าต้องการเซท property แบบนี้น่าจะได้
Code (C#)
1.public Font getButtonFont(int idx)
2.{
3.    return (this.Controls[idx] as Button).Font;
4.}
5.public void setButtonFont(int idx, Font value)
6.{
7.    (this.Controls[idx] as Button).Font = value;
8.}


แต่ถ้าต้องการ แก้ไขข้อมูล ลองใช้ property tag เก็บข้อมูลของ object ที่ต้องการแก้ไขครับ
tag property เป็น ตัวแปรชนิด object เอาไว้ อ้างอิง อะไรต่างๆ ได้เลยครับ
จะเป็น reference หรือ constant ก็ได้ทั้งนั้นครับ

ปล. ผิดวัตถุประสงค์ ต้องการแก้ไข หน้า design time


ประวัติการแก้ไข
2018-08-03 14:15:22
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-08-03 14:11:56 By : Chaidhanan
 

 

No. 2



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2018-08-03 14:11:56
รายละเอียดของการตอบ ::
property tag รึครับ ต้องลองดูก่อนนะครับ

ไปลองแกะของ DataGridView หรือ ตัวอื่นๆที่มี dialog โผล่มาเวลาเรียกแก้ไข
เหมือนจะซับซ้อนซ้อนพอสมควรเหมือนกันคัรบ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-08-03 14:46:08 By : lamaka.tor
 

 

No. 3



โพสกระทู้ ( 9,590 )
บทความ ( 2 )



สถานะออฟไลน์


น่าจะลองแกะ ของ tab panel นะครับ เพราะมันมีการ add control ให้เห็นอย่างชัดเจน
เราสามารถ ลาก object ต่างๆ ลงไปและแก้ไข property ต่างๆ ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-08-04 05:35:11 By : Chaidhanan
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : WinApp C# เขียนโค้ด ButtonCollections แล้ว แต่อยากเลือกแก้ไข ปุ่มทีละปุ่มได้ เหมือนปุ่มปกติครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่