using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UserControl_1
{
class TextBoxOpendialogControl : UserControl
{
public enum SelectedPathType { SelectFolder, SelectFile }
private TextBox txtCode;
private Button btnCode;
private string _path;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.DefaultValue(false)]
[System.ComponentModel.Category("Behavior")]
[System.ComponentModel.Description("OpenFileDialog is dispalyed and on success the contents of the Cell is replaced with the new file path.")]
public string Path
{
get { return _path; }
set
{
txtCode.Text = value;
}
}
public TextBoxOpendialogControl()
{
this.txtCode = new TextBox();
this.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);
this.Controls.Add(this.txtCode);
this.btnCode = new Button();
this.btnCode.Text = "..";
this.btnCode.Click += new EventHandler(btnCode_Click);
this.Controls.Add(this.btnCode);
this.renderControl();
}
public void renderControl()
{
this.txtCode.Location = new Point(0, 0);
this.txtCode.Width = this.Width + 115;
this.txtCode.Height = this.Height;
this.btnCode.Location = new Point(this.Width + 115, 0);
this.btnCode.Width = 32;
this.btnCode.Height = 21;
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Name = "TextBoxButtonControl";
this.Size = new System.Drawing.Size(267, 176);
this.Leave += new System.EventHandler(this.TextBoxButtonControl_Leave);
this.ResumeLayout(false);
}
private void txtCode_TextChanged(object sender, EventArgs e)
{
if (txtCode.Modified)
{
_path = txtCode.Text;
}
}
private void btnCode_Click(object sender, EventArgs e)
{
//Form2 form2 = new Form2();
//form2.ShowDialog();
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
txtCode.Text = dialog.FileName.ToString();
}
catch (Exception)
{
}
}
}
private void TextBoxButtonControl_Leave(object sender, EventArgs e)
{
try
{
this.Visible = false;
this.txtCode.Text = "";
}
catch (Exception)
{
}
}
}
}
Code 2 Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UserControl_1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private string _text;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.DefaultValue("")]
[System.ComponentModel.Category("Behavior")]
[System.ComponentModel.Description("Test User Control by TOR ")]
public string TextCaption
{
get { return _text; }
set
{
_text = value;
textBox1.Text = value;
}
}
}
}
Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x)