Click here to Skip to main content
15,891,621 members
Articles / Mobile Apps

Windows Mobile Password Safe

Rate me:
Please Sign up or sign in to vote.
4.87/5 (58 votes)
12 Jan 2009CPOL16 min read 160.6K   3.1K   139  
A password safe with a touch screen UI introducing Fluid Controls.
using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls.Classes;
using System.Drawing;
using Fluid.Drawing;

namespace Fluid.Controls
{
    public class ButtonGroup : ControlContainer, ICommandContainer,ILayoutPanel
    {
        public ButtonGroup()
            : base()
        {
        }

        public ButtonGroup(int x, int y, int width, int height)
            : base(x, y, width, height)
        {
        }

        protected override void InitControl()
        {
            buttons = new ButtonCollection(this);
            base.InitControl();
        }


        private RoundedCorners corners = RoundedCorners.All;
        public RoundedCorners Corners
        {
            get { return corners; }
            set
            {
                if (corners != value)
                {
                    corners = value;
                    Render();
                }
            }
        }


        private ButtonCollection buttons;

        public ButtonCollection Buttons
        {
            get { return buttons; }
        }

        public void Add(FluidButton button)
        {
            buttons.Add(button);
        }

        public void RemoveAt(int index)
        {
            buttons.RemoveAt(index);
        }

        public void Clear()
        {
            buttons.Clear();
        }

        private int buttonWidth = 0;

        /// <summary>
        /// Gets or sets the width for all but the last button. if set to 0, the width is determined automatically.
        /// </summary>
        public int ButtonWidth
        {
            get { return buttonWidth; }
            set
            {
                if (buttonWidth != value)
                {
                    buttonWidth = value;
                    Render();
                    Invalidate();
                }
            }
        }

        protected override void OnPaintBackground(FluidPaintEventArgs e)
        {
            // do nothing!
        }

        /// <summary>
        /// Renders the buttons;
        /// </summary>
        public void Render()
        {
            if (buttons == null) return;
            int c = buttons.Count;
            controls.Clear();
            int index = 0;
            int x = 0;
            int y = 0;
            int h = Bounds.Height;
            int w0 = c > 0 ? Bounds.Width / c : 0;

            if (buttonWidth > 0) w0 = Math.Min(buttonWidth, Width);

            c--;
            RoundedCorners corners = c == 0 ? RoundedCorners.All : RoundedCorners.Left;
            foreach (FluidButton btn in buttons)
            {
                btn.BeginInit();
                int w = (index != c) ? w0 : (Bounds.Width - x);
                if (btn.BackColor == Color.Transparent || btn.BackColor.IsEmpty)
                {
                    btn.BackColor = this.BackColor;
                }
                btn.Bounds = new Rectangle(x, y, w + ((index != c) ? (int)ScaleFactor.Width : 0), h);
                btn.ScaleFactor = ScaleFactor;
                btn.Corners = corners & Corners;
                btn.Command = index.ToString();
                //btn.Alpha = alpha;
                btn.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
                btn.EndInit();
                index++;
                controls.Add(btn);
                x += w;
                corners = index == c ? RoundedCorners.Right : RoundedCorners.None;
            }
            Invalidate();
            OnButtonsChanged();
        }

        private int alpha = 255;
        public int Alpha
        {
            get { return alpha; }
            set
            {
                if (alpha != value)
                {
                    alpha = value;
                    ModifyAlpha(alpha);
                }
            }
        }

        private void ModifyAlpha(int alpha)
        {
            foreach (FluidControl c in controls)
            {
                FluidButton btn = (FluidButton)c;
                btn.Alpha = alpha;
            }
        }

        protected virtual void OnButtonsChanged()
        {
            if (ButtonsChanged != null) ButtonsChanged(this, EventArgs.Empty);
        }

        public event EventHandler ButtonsChanged;


        private ButtonGroupEventArgs clickEvent;
        public override void RaiseCommand(CommandEventArgs e)
        {
            // base.RaiseCommand(e); // do not fire to parent containers!
            if (ButtonClick != null)
            {
                if (clickEvent == null) clickEvent = new ButtonGroupEventArgs();
                int index = int.Parse(e.Command);
                clickEvent.Index = index;
                clickEvent.Button = buttons[index];
                ButtonClick(this, clickEvent);
            }

        }

        protected override void OnSizeChanged(Size oldSize, Size newSize)
        {
            base.OnSizeChanged(oldSize, newSize);
            if (!oldSize.IsEmpty)
            {
                Render();
            }
        }

        /// <summary>
        /// Occurs when a button was clicked.
        /// </summary>
        public event EventHandler<ButtonGroupEventArgs> ButtonClick;

        public override void Scale(SizeF scaleFactor)
        {
            base.Scale(scaleFactor);
            ButtonWidth = (int)(ButtonWidth * scaleFactor.Width);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer


MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

Comments and Discussions