Showing posts with label User Controls. Show all posts
Showing posts with label User Controls. Show all posts

MultiPicBox control [With SlidShow]

12:18 AM
Once i was working with the Microsoft's PictureBox control. I noticed the each time i want to change the image i will have to create an Image object and Load my Image into and Assign it to PictureBox . And if i want to see the old pic (before assigning to new) i will have to again do the same.

So i thought why not make a control which support Multiple Pictures and a Slidshow Option..(with interval option).

Here are some pics of  the Control:



Properties:


AutoPreview               : Will automatically start the slideshow if enabled.
AutoResizeImages     : will automatically fit images to picture box if enabled.
ImageLimit                 : How many images should be there in control.
ImagesArrary             : Arrary of images to be displayed.
Interval                       : the interval time for the slidshow

Download Control:
Multipicbox

See it Live:


A Custom ListBox Control

3:30 AM
Here is Another User Control

A pick to of The Control:
As you can see this one contains a Listbox and 7 buttons for the Add,Remove,up,down,,RemoveAll,Top,Bottom etc
1. Add a user control to project. Add a list box and 7 button as show in the figure.
Coding:
Properties



This is For Left/Right Side Buttons Docking

public bool _blnLeftSideButtons = false;
        public bool LeftSideButtons
        {
            get { return _blnLeftSideButtons; }
            set {
                _blnLeftSideButtons = value;

                if (_blnLeftSideButtons)
                {
                    lstList.Dock = DockStyle.None;
                    flowLayoutPanel1.Dock = DockStyle.Left;
                    lstList.Dock = DockStyle.Fill;
                    Invalidate();
                }
                else
                {
                    lstList.Dock = DockStyle.None;
                    flowLayoutPanel1.Dock = DockStyle.Right;
                    lstList.Dock = DockStyle.Fill;
                    Invalidate();
                }

                }
        }


Add Button Coding

private void btnAdd_Click(object sender, EventArgs e)
        {
            ofD = new OpenFileDialog();
            ofD.Multiselect = true;
            ofD.Filter = "Image Files(*.gif,*.jpg,*.jpeg,*.bmp,*.png,*.wmf)|*.gif;*.jpg;*.jpeg;*.bmp;*.png;*.wmf";

            if (ofD.ShowDialog() == DialogResult.OK)
            {
                if (ofD.FileNames.Length > 0)
                {
                    for (int i = 0; i < ofD.FileNames.Length; i++)
                    {
                        lstList.Items.Add(Path.GetFileName( ofD.FileNames[i].ToString()));
                    }
                }
            }

        }
Remove Button Coding
//Remove item
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstList.Items.Count > 0 && lstList.SelectedIndex > 0)
            {
                lstList.Items.RemoveAt(lstList.SelectedIndex);
            }
        }
Move An Item Up
// item Up
        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstList.SelectedItems.Count == 0) { return; }

                int index = lstList.Items.IndexOf(lstList.SelectedItems[0]);

                if ((index - 1) < 0) { return; }

                object[] obj = new object[lstList.SelectedItems.Count];

                for (int i = 0; i < lstList.SelectedItems.Count; i++)
                {
                    obj[i] = lstList.SelectedItems[i];
                }

                lstList.SelectedIndices.Clear();

                for (int j = 0; j < obj.Length; j++)
                {

                    int temp_I = lstList.Items.IndexOf(obj[j]);
                    lstList.Items.RemoveAt(temp_I);
                    lstList.Items.Insert(temp_I - 1, obj[j]);

                    lstList.SelectedIndices.Add(temp_I - 1);

                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
Move An Item Down
//item down
        private void btnDown_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstList.SelectedItems.Count == 0) { return; }

                int index = lstList.Items.IndexOf(lstList.SelectedItems[0]);

                if (index == lstList.Items.Count - 1) { return; }

                
                if ((index + 1) > lstList.Items.Count) { return; }

                object[] obj = new object[lstList.SelectedItems.Count];

                for (int i = 0; i < lstList.SelectedItems.Count; i++)
                {
                    obj[i] = lstList.SelectedItems[i];
                }

                lstList.SelectedIndices.Clear();

                for (int j = 0; j < obj.Length; j++)
                {


                    int temp_J = lstList.Items.IndexOf(obj[j]);
                    lstList.Items.RemoveAt(temp_J);
                    lstList.Items.Insert(temp_J + 1, obj[j]);

                    lstList.SelectedIndices.Add(temp_J + 1);

                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

Move An Item To Top
//Top
        private void btnTop_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstList.Items.Count == 0 ) { return; }

                int index = lstList.SelectedIndices[0];
                object obj = new object();
                obj = lstList.SelectedItems[0];

                lstList.Items.RemoveAt(index);
                lstList.Items.Insert(0, obj);

                lstList.SelectedIndices.Clear();
                lstList.SelectedIndices.Add(0);

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
Move item to Bottom
//Bottom
        private void btnBottom_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstList.Items.Count == 0) { return; }

                int index = lstList.SelectedIndices[0];
                object obj = new object();
                obj = lstList.SelectedItems[0];

                lstList.Items.RemoveAt(index);
                lstList.Items.Insert(lstList.Items.Count , obj);

                lstList.SelectedIndices.Clear();
                lstList.SelectedIndices.Add(lstList.Items.Count - 1);

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
Remove All Items:
//Remove All
        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            if (lstList.Items.Count > 0)
            {
                if (MessageBox.Show("This Will Delete All The Items", "Are You Sure?", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    lstList.Items.Clear();
                }
            }
        }
For the Source code mail me at: sandeepparekh9@gmail.com

Extended RichTextBox Control

3:10 AM
hi. I really needed a Richtext box with Word like functionality.. i mean Bold,Italic etc etc Function. So i decided to write a Custom Control for this Purpose.

Extended RichTextBox Contains Following Controls

1. Rich Text Box [ rtxtBox ]
2. A tool Strip with Basic button including word Formatting Functionality.. [ tsStrip ]
3. A font Dialog [ fntD ], Color Dialog [ clrD ], OpenFileDialog [ ofD ]
here is a pic.

1. First Add a RichText box and a tool strip as show in figure to your Custom Control.
2. Add basic buttons to tool strip according to screen shot (You can add your own image to buttons)
3. And here is the coding..

Add This Properties to your Custom Control


[Browsable(true), Category("Appearance")] 
        public override Font Font 
        { 
            get { return rtxtBox.Font  ; } 
            set { rtxtBox.Font  = value; 
                Invalidate(); } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override Color  BackColor 
        { 
            get { return rtxtBox.BackColor ; } 
            set 
            { 
                rtxtBox.BackColor  = value; 
                Invalidate(); 
            } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override Color ForeColor 
        { 
            get { return rtxtBox.ForeColor ; } 
            set 
            { 
                rtxtBox.ForeColor  = value; 
                Invalidate(); 
            } 
        } 
         
        [Browsable(true), Category("Appearance")] 
        public  string[]  Lines 
        { 
            get { return rtxtBox.Lines; } 
            set { rtxtBox.Lines = value; } 
        } 
        [Browsable(true), Category("Appearance")] 
        public RichTextBoxScrollBars ScrollBars 
        { 
            get { return rtxtBox.ScrollBars; } 
            set { rtxtBox.ScrollBars = value; } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override string Text 
        { 
            get { return rtxtBox.Text;  } 
            set { rtxtBox.Text = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool AcceptsTab 
        { 
            get { return rtxtBox.AcceptsTab;  } 
            set { rtxtBox.AcceptsTab = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool AutoWordSelection 
        { 
            get { return rtxtBox.AutoWordSelection; } 
            set { rtxtBox.AutoWordSelection = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int BulletIndent 
        { 
            get { return rtxtBox.BulletIndent; } 
            set { rtxtBox.BulletIndent  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool DetectUrls 
        { 
            get { return rtxtBox.DetectUrls; } 
            set { rtxtBox.DetectUrls  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool EnableAutoDragDrop 
        { 
            get { return rtxtBox.EnableAutoDragDrop ; } 
            set { rtxtBox.EnableAutoDragDrop = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool HideSelection 
        { 
            get { return rtxtBox.HideSelection ; } 
            set { rtxtBox.HideSelection = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int MaxLength 
        { 
            get { return rtxtBox.MaxLength ; } 
            set { rtxtBox.MaxLength = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool Multiline 
        { 
            get { return rtxtBox.Multiline ; } 
            set { rtxtBox.Multiline = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ReadOnly 
        { 
            get { return rtxtBox.ReadOnly ; } 
            set { rtxtBox.ReadOnly = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int RightMargin 
        { 
            get { return rtxtBox.RightMargin ; } 
            set { rtxtBox.RightMargin = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ShortcutsEnabled 
        { 
            get { return rtxtBox.ShortcutsEnabled; } 
            set { rtxtBox.ShortcutsEnabled = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ShowSelectionMargin 
        { 
            get { return rtxtBox.ShowSelectionMargin ; } 
            set { rtxtBox.ShowSelectionMargin = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool WordWrap  
        { 
            get { return rtxtBox.WordWrap; } 
            set { rtxtBox.WordWrap  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public float ZoomFactor 
        { 
            get { return rtxtBox.ZoomFactor ; } 
            set { rtxtBox.ZoomFactor = value; } 
        }  
Code For Font Button on tool strip to change Richtextbox font
private void tbrFont_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                fntD.Font = rtxtBox.Font; 
            } 
            else 
            { 
                fntD.Font = null; 
            } 
             
            fntD.ShowApply = true; 
            if (fntD.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
            { 
                rtxtBox.SelectionFont = fntD.Font; 
            } 
        } 

Here Is the Rest of The Code:
//Left Alignment 
        private void tbrLeft_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Left; 
        } 
        //Center Alignment 
        private void tbrCenter_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Center; 
        } 
        //Right Alignment 
        private void tbrRight_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Right; 
        } 
        //Bold 
        private void tbrBold_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Bold == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Bold; 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Italic 
        private void tbrItalic_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Italic == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Italic; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Underline 
        private void tbrUnderline_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Underline  == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic  == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Underline; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //StrikeThrough 
        private void tbrStrikeThrough_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Strikeout == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline  == true) { newFont = newFont | FontStyle.Underline; } 
                } 
                else 
                { 
                    newFont = FontStyle.Strikeout; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Add Bullet 
        private void addBulletToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            rtxtBox.BulletIndent = 10; 
            rtxtBox.SelectionBullet = true; 
        } 
        //Remove Bullet 
        private void removeBulletToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionBullet = false; 
        } 
        //Font Fore Color 
        private void tbrFontColor_Click(object sender, EventArgs e) 
        { 
            clrD.Color = rtxtBox.ForeColor; 
            if (clrD.ShowDialog() == DialogResult.OK) 
            { 
                rtxtBox.SelectionColor = clrD.Color; 
            } 
           
        } 
        //Font Back Color 
        private void tbrBackColor_Click(object sender, EventArgs e) 
        { 
            clrD.Color = rtxtBox.SelectionBackColor ; 
            if (clrD.ShowDialog() == DialogResult.OK) 
            { 
                rtxtBox.SelectionBackColor  = clrD.Color; 
            } 
        } 
        
        //KeyDown Event  [Shortcuts] 
        private void rtxtBox_KeyDown(object sender, KeyEventArgs e) 
        { 
            if (e.Control && e.KeyCode == Keys.B) 
            { 
                tbrBold_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.I ) 
            { 
                tbrItalic_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.U) 
            { 
                tbrUnderline_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.S) 
            { 
                tbrStrikeThrough_Click(null, null); 
            } 
        } 
If you want the Complete Source Code then mail me on : sandeepparekh9@gmaill.com