Click here to Skip to main content
15,892,480 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.9K   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;
using System.Drawing;
using System.Windows.Forms;
using PasswordSafe.Xml;
using PasswordSafe.Classes;

namespace PasswordSafe.Templates
{
    public class DetailTemplate : FluidTemplate
    {
        protected override void InitControl()
        {
            base.InitControl();
            Editable = true;

            this.Bounds = new Rectangle(0, 0, 240, 40);
            FluidLabel l;

            FluidTextBox tb = new FluidTextBox("", 3, 17, 228, 24);
            tb.LineAlignment = StringAlignment.Near;
            tb.Alignment = StringAlignment.Near;
            tb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            tb.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            tb.ShowBorder = false;
            tb.BackColor = Color.Transparent;
            tb.TextChanged += new EventHandler(tb_TextChanged);
            tb.FocusChanged += new EventHandler(tb_FocusChanged);
            tb.Bind += new EventHandler(tb_Bind);
            valueTb = tb;
            Controls.Add(tb);

            l = new FluidLabel("", 3, 3, 228, 14);
            l.LineAlignment = StringAlignment.Near;
            l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            l.Font = new Font(FontFamily.GenericSansSerif, 7f, FontStyle.Regular);
            l.ForeColor = Theme.Current.ListSecondaryForeColor;
            nameLabel = l;
            Controls.Add(l);
        }

        void tb_FocusChanged(object sender, EventArgs e)
        {
            EnsureBound();
        }

        private void EnsureBound()
        {
            if (ItemIndex != SelectedItemIndex)
            {
                Bind(SelectedItemIndex);
            }
        }

        void tb_Bind(object sender, EventArgs e)
        {
            if (ItemIndex != SelectedItemIndex)
            {
                BindData();
            }
        }

        public int SelectedItemIndex { get { return (Parent as FluidListBox).SelectedItemIndex; } }


        void tb_TextChanged(object sender, EventArgs e)
        {
           // EnsureBound();
            PWNameValue item = this.Item as PWNameValue;
            if (item != null)
            {
                item.Value = valueTb.Text;
            }
        }

        private FluidLabel nameLabel;
        private FluidTextBox valueTb;

        public bool Editable { get; set; }

        protected override void OnBindValue(object value)
        {
            PWNameValue item = value as PWNameValue;
            if (item != null)
            {
                nameLabel.Text = item.Title;
                valueTb.Text = item.Value;
            }
            else
            {
                nameLabel.Text = "";
                valueTb.Text = "";
            }
        }

        public DetailListBox DetailListBox { get { return Parent as DetailListBox; } }

        protected override void OnItemUpdate(object value)
        {
            DetailListBox lb = DetailListBox;
            bool isSelected = lb.SelectedItemIndex == this.ItemIndex;
            nameLabel.ForeColor = isSelected ? Theme.Current.ListSecondarySelectedForeColor : Theme.Current.ListSecondaryForeColor;
            valueTb.AllowEdit = Editable && (SelectedItemIndex == ItemIndex);
        }

        public void BindData()
        {
            Bind((Parent as DetailListBox).SelectedItemIndex);
        }

        public override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!valueTb.Focused)
                    {
                        valueTb.Focus();
                    }
                    else Parent.Focus();
                    e.Handled = true;

                    break;
            }
        }


        public void InsertKey(KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r') return;
            if (ItemIndex != SelectedItemIndex)
            {
                BindData();
            }
            if (!valueTb.Focused)
            {
                Focus();
                if (valueTb.Focused) valueTb.OnKeyPress(e);
            }
        }

        public bool IsFocused { get { return valueTb.Focused; } }

        public bool GetFocus { get; set; }

        public override void Focus()
        {
            if (!valueTb.Focused)
            {
                valueTb.Focus();
                valueTb.SelectAll();
            }
        }
    }
}

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