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 > C# WinApp สอบถามเรื่อง การสร้าง Usercontrol กำหนด property ให้ Usercontrol แต่มันไม่จำค่าที่ใส่ครับ


 

[.NET] C# WinApp สอบถามเรื่อง การสร้าง Usercontrol กำหนด property ให้ Usercontrol แต่มันไม่จำค่าที่ใส่ครับ

 
Topic : 124673



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



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


ผมการสร้าง Usercontrol ขึ้นมาแล้วกำหนด property ให้ Usercontrol แต่มันไม่จำค่าที่ใส่ครับ

E2

Code 1
Code (C#)
01.using System;
02.using System.Collections.Generic;
03.using System.Drawing;
04.using System.Linq;
05.using System.Text;
06.using System.Windows.Forms;
07. 
08.namespace UserControl_1
09.{
10.    class TextBoxOpendialogControl : UserControl
11.    {
12.        public enum SelectedPathType { SelectFolder, SelectFile }
13.        private TextBox txtCode;
14.        private Button btnCode;
15.        private string _path;
16.        [System.ComponentModel.Browsable(true)]
17.        [System.ComponentModel.DefaultValue(false)]
18.        [System.ComponentModel.Category("Behavior")]
19.        [System.ComponentModel.Description("OpenFileDialog is dispalyed and on success the contents of the Cell is replaced with the new file path.")]
20.        public string Path
21.        {
22. 
23.            get { return _path; }
24.            set
25.            {
26.                txtCode.Text = value;
27.            }
28.        }
29. 
30.        public TextBoxOpendialogControl()
31.        {
32.            this.txtCode = new TextBox();
33.            this.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);
34.            this.Controls.Add(this.txtCode);
35.            this.btnCode = new Button();
36.            this.btnCode.Text = "..";
37.            this.btnCode.Click += new EventHandler(btnCode_Click);
38.            this.Controls.Add(this.btnCode);
39.            this.renderControl();
40.        }
41.        public void renderControl()
42.        {
43.            this.txtCode.Location = new Point(0, 0);
44.            this.txtCode.Width = this.Width + 115;
45.            this.txtCode.Height = this.Height;
46.            this.btnCode.Location = new Point(this.Width + 115, 0);
47.            this.btnCode.Width = 32;
48.            this.btnCode.Height = 21;
49.        }
50. 
51.        private void InitializeComponent()
52.        {
53.            this.SuspendLayout();
54.            this.Name = "TextBoxButtonControl";
55.            this.Size = new System.Drawing.Size(267, 176);
56.            this.Leave += new System.EventHandler(this.TextBoxButtonControl_Leave);
57.            this.ResumeLayout(false);
58. 
59.        }
60.        private void txtCode_TextChanged(object sender, EventArgs e)
61.        {
62.            if (txtCode.Modified)
63.            {
64.                _path = txtCode.Text;
65.            }
66.        }
67. 
68. 
69. 
70.        private void btnCode_Click(object sender, EventArgs e)
71.        {
72.            //Form2 form2 = new Form2();
73.            //form2.ShowDialog();
74.            OpenFileDialog dialog = new OpenFileDialog();
75.            if (dialog.ShowDialog() == DialogResult.OK)
76.            {
77.                try
78.                {
79.                    txtCode.Text = dialog.FileName.ToString();
80.                }
81.                catch (Exception)
82.                {
83.                }
84.            }
85.        }
86.        private void TextBoxButtonControl_Leave(object sender, EventArgs e)
87.        {
88.            try
89.            {
90.                this.Visible = false;
91.                this.txtCode.Text = "";
92.            }
93.            catch (Exception)
94.            {
95.            }
96.        }
97.    }
98.}


Code 2
Code (C#)
01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Drawing;
05.using System.Data;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.namespace UserControl_1
11.{
12.    public partial class UserControl1 : UserControl
13.    {
14.        public UserControl1()
15.        {
16.            InitializeComponent();
17.        }
18.        private string _text;
19.        [System.ComponentModel.Browsable(true)]
20.        [System.ComponentModel.DefaultValue("")]
21.        [System.ComponentModel.Category("Behavior")]
22.        [System.ComponentModel.Description("Test User Control by TOR ")]
23.        public string TextCaption
24.        {
25.            get { return _text; }
26.            set
27.            {
28.                _text = value;
29.                textBox1.Text = value;
30.            }
31.        }
32.    }
33.}




Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-09-21 10:03:50 By : lamaka.tor View : 1081 Reply : 2
 

 

No. 1



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



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

และแล้วก็สำเร็จ
เอามาให้เล่นกันครับ
TextBoxOpendialogControl แบบที่มีทั้ง colorDialog, folderBrowserDialog, fontDialog, openFileDialog, saveFileDialog

Code (C#)
001.using System;
002.using System.Collections.Generic;
003.using System.ComponentModel;
004.using System.Drawing;
005.using System.Data;
006.using System.Linq;
007.using System.Text;
008.using System.Windows.Forms;
009. 
010.namespace TORServices.Form
011.{
012.    public partial class TextBoxOpendialogControl : UserControl
013.    {
014.        public enum SelectedPathType { colorDialog, folderBrowserDialog, fontDialog, openFileDialog, saveFileDialog }
015.        private TextBox txtCode;
016.        private Button btnCode;
017. 
018.        public string Value
019.        {
020.            get { return txtCode.Text; }
021.        }
022.        [System.ComponentModel.Browsable(true)]
023.        [System.ComponentModel.DefaultValue("..")]
024.        [System.ComponentModel.Category("Behavior")]
025.        [System.ComponentModel.Description("OpenFileDialog is dispalyed and on success the contents of the Cell is replaced with the new file path.")]
026.        public string ButtonCaption
027.        {
028. 
029.            get { return btnCode.Text; }
030.            set
031.            {
032.                btnCode.Text = value;
033.            }
034.        }
035.        private bool _selectFolder;
036.        [System.ComponentModel.Browsable(true)]
037.        [System.ComponentModel.DefaultValue(false)]
038.        [System.ComponentModel.Category("Behavior")]
039.        public bool selectFolder
040.        {
041.            get { return _selectFolder; }
042.            set { _selectFolder = value; }
043.        }
044.        private SelectedPathType _selected;
045.        [System.ComponentModel.Browsable(true)]
046.        //[System.ComponentModel.DefaultValue(SelectedPathType.SelectFile)]
047.        [System.ComponentModel.Category("Behavior")]
048.        public SelectedPathType SelectedMode
049.        {
050.            get { return _selected; }
051.            set { _selected = value; }
052.        }
053. 
054.        [System.ComponentModel.Browsable(true)]
055.        [System.ComponentModel.DefaultValue(null)]
056.        [System.ComponentModel.Category("Behavior")]
057.        public Image Image { get { return btnCode.Image; } set { btnCode.Image = value; } }
058.        public TextBoxOpendialogControl()
059.        {
060.            InitializeComponent();
061.            this.txtCode = new TextBox();
062.            this.Controls.Add(this.txtCode);
063.            this.btnCode = new Button();
064.            this.btnCode.Text = "..";
065.            btnCode.Dock = DockStyle.Right;
066.            txtCode.Dock = DockStyle.Fill;
067.            txtCode.ReadOnly = true;
068.            this.btnCode.Click += new EventHandler(btnCode_Click);
069.            this.Controls.Add(this.btnCode);
070.            this.renderControl();
071.        }
072. 
073.        public void renderControl()
074.        {
075.            this.btnCode.Width = 32;
076.            this.btnCode.Height = 21;
077.        }
078. 
079.        private void btnCode_Click(object sender, EventArgs e)
080.        {
081.            try
082.            {
083.                if (_selected == SelectedPathType.folderBrowserDialog)
084.                {
085.                    OpenFileDialog dialog = new OpenFileDialog();
086.                    if (dialog.ShowDialog() == DialogResult.OK)
087.                    {
088. 
089.                        txtCode.Text = dialog.FileName.ToString();
090. 
091.                    }
092.                }
093.                else if (_selected == SelectedPathType.openFileDialog)
094.                {
095.                    FolderBrowserDialog dialog = new FolderBrowserDialog();
096.                    if (dialog.ShowDialog() == DialogResult.OK)
097.                    {
098.                        txtCode.Text = dialog.SelectedPath;
099.                    }
100.                }
101.                else if (_selected == SelectedPathType.colorDialog)
102.                {
103.                    ColorDialog dialog = new ColorDialog();
104.                    if (dialog.ShowDialog() == DialogResult.OK)
105.                    {
106.                        txtCode.Text = dialog.Color.ToString() + ":"
107.                                       + dialog.Color.Name.ToString()
108.                                      + ": A=" + dialog.Color.A + " R=" + dialog.Color.R + " G=" + dialog.Color.G + " B=" + dialog.Color.B;
109.                    }
110.                }
111.                else if (_selected == SelectedPathType.fontDialog)
112.                {
113.                    FontDialog dialog = new FontDialog();
114.                    if (dialog.ShowDialog() == DialogResult.OK)
115.                    {
116.                        txtCode.Text = dialog.Font.ToString();
117.                    }
118.                }
119.                else if (_selected == SelectedPathType.saveFileDialog)
120.                {
121.                    SaveFileDialog dialog = new SaveFileDialog();
122.                    if (dialog.ShowDialog() == DialogResult.OK)
123.                    {
124.                        txtCode.Text = dialog.FileName;
125.                    }
126.                }
127.            }
128.            catch (Exception)
129.            {
130.            }
131.        }
132.        private void TextBoxButtonControl_Leave(object sender, EventArgs e)
133.        {
134.            try
135.            {
136.                this.Visible = false;
137.                this.txtCode.Text = "";
138.            }
139.            catch (Exception)
140.            {
141.            }
142.        }
143.    }
144.}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-09-21 11:37:34 By : lamaka.tor
 

 

No. 2



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



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

เพิ่มเติมอีกนิด
txtCode.BackColor = dialog.Color;//จะได้ดูสีที่เลือก

Code (C#)
01.else if (_selected == SelectedPathType.colorDialog)
02.                {
03.                    ColorDialog dialog = new ColorDialog();
04.                    if (dialog.ShowDialog() == DialogResult.OK)
05.                    {
06.                        txtCode.Text = dialog.Color.ToString() + ":"
07.                                       + dialog.Color.Name.ToString()
08.                                      + ": A=" + dialog.Color.A + " R=" + dialog.Color.R + " G=" + dialog.Color.G + " B=" + dialog.Color.B;
09.                        txtCode.BackColor = dialog.Color;
10.                    }
11.                }


แต่ตอนนี้ก็มาติดอีกว่า

จะหาวิธีให้ txtCode.ForeColor เป็นสีตัดกับ txtCode.BackColor ได้ยังไง
ปัญหามีกันต่อไปเรื่อย 555
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-09-21 11:49:59 By : lamaka.tor
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# WinApp สอบถามเรื่อง การสร้าง Usercontrol กำหนด property ให้ Usercontrol แต่มันไม่จำค่าที่ใส่ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 อัตราราคา คลิกที่นี่