Advertisement
Yassine_Abbani

File Browser [Costume Control]

May 14th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. #region Import
  2. using System;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Text;
  8. using System.Windows.Forms;
  9. #endregion
  10. #region Copyright & Contact
  11. // Creator: Cheat Eye
  12. // CEO: Yassine Abbani
  13. // Link: https://www.facebook.com/yassineabbani
  14. // Version: 1.0.0
  15. #endregion
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. #region  FileBrowser
  23.  
  24. [DefaultEvent("TextChanged")]
  25. public class Ce_FileBrowser : Control
  26. {
  27.  
  28.     #region  Variables
  29.  
  30.     private TextBox BlueTB = new TextBox();
  31.     private Button BrowseBTN = new Button();
  32.  
  33.     public OpenFileDialog Dialog;
  34.     private string filter = @"All Files (*.*)|*.*";
  35.  
  36.     private int _maxchars = 32767;
  37.     private bool _ReadOnly;
  38.  
  39.     private HorizontalAlignment ALNType;
  40.     private Pen P1;
  41.     private SolidBrush B1;
  42.     private GraphicsPath Shape;
  43.  
  44.     #endregion
  45.     #region  Properties
  46.  
  47.     public HorizontalAlignment TextAlignment
  48.     {
  49.         get
  50.         {
  51.             return ALNType;
  52.         }
  53.         set
  54.         {
  55.             ALNType = value;
  56.             Invalidate();
  57.         }
  58.     }
  59.     public int MaxLength
  60.     {
  61.         get
  62.         {
  63.             return _maxchars;
  64.         }
  65.         set
  66.         {
  67.             _maxchars = value;
  68.             BlueTB.MaxLength = MaxLength;
  69.             Invalidate();
  70.         }
  71.     }
  72.     public bool ReadOnly
  73.     {
  74.         get
  75.         {
  76.             return _ReadOnly;
  77.         }
  78.         set
  79.         {
  80.             _ReadOnly = value;
  81.             if (BlueTB != null)
  82.             {
  83.                 BlueTB.ReadOnly = value;
  84.             }
  85.         }
  86.     }
  87.  
  88.     [Description("Default is: All Files (*.*)|*.*"), Category("Behavior")]
  89.     public string Filter
  90.     {
  91.         get { return filter; }
  92.         set
  93.         {
  94.             filter = value;
  95.             Invalidate();
  96.         }
  97.     }
  98.  
  99.     #endregion
  100.     #region  EventArgs
  101.  
  102.     private void _Enter(object Obj, EventArgs e)
  103.     {
  104.         P1 = new Pen(Color.FromArgb(146, 192, 224));
  105.         Refresh();
  106.     }
  107.     private void _Leave(object Obj, EventArgs e)
  108.     {
  109.         P1 = new Pen(Color.FromArgb(255, 172, 172));
  110.         Refresh();
  111.     }
  112.  
  113.     private void _OnKeyDown(object Obj, KeyEventArgs e)
  114.     {
  115.         if (e.Control && e.KeyCode == Keys.A)
  116.         {
  117.             BlueTB.SelectAll();
  118.             e.SuppressKeyPress = true;
  119.         }
  120.         if (e.Control && e.KeyCode == Keys.C)
  121.         {
  122.             BlueTB.Copy();
  123.             e.SuppressKeyPress = true;
  124.         }
  125.     }
  126.     private void BrowseDown(object Obj, EventArgs e)
  127.     {
  128.         Dialog = new OpenFileDialog();
  129.         Dialog.Filter = filter;
  130.         DialogResult result = Dialog.ShowDialog();
  131.  
  132.         if (result == DialogResult.OK && Dialog.SafeFileName != null)
  133.         {
  134.             Text = Dialog.FileName;
  135.         }
  136.     }
  137.  
  138.     public void TextChngTxtBox(System.Object sender, System.EventArgs e)
  139.     {
  140.         Text = BlueTB.Text;
  141.     }
  142.  
  143.     public void TextChng(System.Object sender, System.EventArgs e)
  144.     {
  145.         BlueTB.Text = Text;
  146.     }
  147.  
  148.     protected override void OnTextChanged(System.EventArgs e)
  149.     {
  150.         base.OnTextChanged(e);
  151.         Invalidate();
  152.     }
  153.  
  154.     protected override void OnEnabledChanged(EventArgs e)
  155.     {
  156.         P1 = new Pen(Color.FromArgb(172, 172, 172));
  157.         base.OnEnabledChanged(e);
  158.     }
  159.  
  160.     protected override void OnForeColorChanged(System.EventArgs e)
  161.     {
  162.         base.OnForeColorChanged(e);
  163.         BlueTB.ForeColor = ForeColor;
  164.         Invalidate();
  165.     }
  166.  
  167.     protected override void OnFontChanged(System.EventArgs e)
  168.     {
  169.         base.OnFontChanged(e);
  170.         BlueTB.Font = Font;
  171.     }
  172.  
  173.     protected override void OnResize(System.EventArgs e)
  174.     {
  175.         base.OnResize(e);
  176.         Height = BlueTB.Height + 6;
  177.  
  178.         Shape = new GraphicsPath();
  179.         Shape.AddArc(0, 0, 3, 3, 180, 90);
  180.         Shape.AddArc(Width - 4, 0, 3, 3, -90, 90);
  181.         Shape.AddArc(Width - 4, Height - 4, 3, 3, 0, 90);
  182.         Shape.AddArc(0, Height - 4, 3, 3, 90, 90);
  183.         Shape.CloseAllFigures();
  184.     }
  185.  
  186.     protected override void OnGotFocus(System.EventArgs e)
  187.     {
  188.         base.OnGotFocus(e);
  189.         BlueTB.Focus();
  190.     }
  191.  
  192.     #endregion
  193.  
  194.     public void AddTextBox()
  195.     {
  196.         BlueTB.Location = new Point(3, 3);
  197.         BlueTB.Size = new Size(140, 20);
  198.         BlueTB.Text = Text;
  199.         BlueTB.BorderStyle = BorderStyle.None;
  200.         BlueTB.TextAlign = HorizontalAlignment.Left;
  201.         BlueTB.Font = new Font("Tahoma", 10);
  202.         BlueTB.Multiline = false;
  203.         BlueTB.BackColor = Color.FromArgb(22, 44, 51);
  204.         BlueTB.ForeColor = Color.FromArgb(138, 133, 133);
  205.         BlueTB.ScrollBars = ScrollBars.None;
  206.  
  207.         BlueTB.KeyDown += _OnKeyDown;
  208.         BlueTB.Enter += _Enter;
  209.         BlueTB.Leave += _Leave;
  210.     }
  211.     public void AddButton()
  212.     {
  213.         BrowseBTN.Location = new Point(271, 4);
  214.         BrowseBTN.Size = new Size(25, 15);
  215.         BrowseBTN.Text = "...";
  216.         BrowseBTN.FlatStyle = FlatStyle.Flat;
  217.         BrowseBTN.FlatAppearance.BorderSize = 0;
  218.         BrowseBTN.TextAlign = ContentAlignment.MiddleCenter;
  219.         BrowseBTN.BackColor = Color.Transparent;
  220.         BrowseBTN.ForeColor = Color.FromArgb(37, 150, 255);
  221.         BrowseBTN.MouseDown += BrowseDown;
  222.     }
  223.     public Ce_FileBrowser()
  224.     {
  225.         SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  226.         SetStyle(ControlStyles.UserPaint, true);
  227.  
  228.         AddButton();
  229.         AddTextBox();
  230.         Controls.Add(BrowseBTN);
  231.         Controls.Add(BlueTB);
  232.  
  233.         P1 = new Pen(Color.FromArgb(172, 172, 172));
  234.         B1 = new SolidBrush(Color.FromArgb(53, 78, 105));
  235.  
  236.         BackColor = Color.Transparent;
  237.         ForeColor = Color.FromArgb(138, 133, 133);
  238.  
  239.         Text = null;
  240.         Font = new Font("Tahoma", 10);
  241.         Size = new Size(300, 20);
  242.         DoubleBuffered = true;
  243.  
  244.         BlueTB.TextChanged += new EventHandler(TextChngTxtBox);
  245.         base.TextChanged += new EventHandler(TextChng);
  246.     }
  247.  
  248.     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  249.     {
  250.         base.OnPaint(e);
  251.         Bitmap B = new Bitmap(Width, Height);
  252.         Graphics G = Graphics.FromImage(B);
  253.         G.SmoothingMode = SmoothingMode.AntiAlias;
  254.  
  255.         BlueTB.Width = Width - 35;
  256.         BrowseBTN.Location = new Point(Width - 29, 4);
  257.         Rectangle Rect = new Rectangle(BrowseBTN.Location.X - 1, BrowseBTN.Location.Y - 1, BrowseBTN.Width + 1, BrowseBTN.Height + 1);
  258.  
  259.         BlueTB.TextAlign = TextAlignment;
  260.  
  261.         G.Clear(Color.Transparent);
  262.         G.FillPath(B1, Shape);
  263.         G.DrawPath(P1, Shape);
  264.         G.DrawRectangle(P1, Rect);
  265.         G.DrawString("...", Font, new SolidBrush(Color.FromArgb(91, 110, 144)), Width - 25, 0);
  266.  
  267.         e.Graphics.DrawImage((Image)B.Clone(), 0, 0);
  268.         G.Dispose();
  269.         B.Dispose();
  270.     }
  271. }
  272.  
  273. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement