NodeTextBox.cs :  » GUI » TreeViewAdv » Aga » Controls » Tree » NodeControls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » Aga » Controls » Tree » NodeControls » NodeTextBox.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.ComponentModel;

namespace Aga.Controls.Tree.NodeControls{
  public class NodeTextBox : BaseTextControl
  {
    private const int MinTextBoxWidth = 30;

    public NodeTextBox()
    {
    }

    protected override Size CalculateEditorSize(EditorContext context)
    {
      if (Parent.UseColumns)
        return context.Bounds.Size;
      else
      {
        Size size = GetLabelSize(context.CurrentNode, context.DrawContext, _label);
        int width = Math.Max(size.Width + Font.Height, MinTextBoxWidth); // reserve a place for new typed character
        return new Size(width, size.Height);
      }
    }

    public override void KeyDown(KeyEventArgs args)
    {
      if (args.KeyCode == Keys.F2 && Parent.CurrentNode != null && EditEnabled)
      {
        args.Handled = true;
        BeginEdit();
      }
    }

    protected override Control CreateEditor(TreeNodeAdv node)
    {
      TextBox textBox = CreateTextBox();
      textBox.TextAlign = TextAlign;
      textBox.Text = GetLabel(node);
      textBox.BorderStyle = BorderStyle.FixedSingle;
      textBox.TextChanged += EditorTextChanged;
      textBox.KeyDown += EditorKeyDown;
      _label = textBox.Text;
      SetEditControlProperties(textBox, node);
      return textBox;
    }

    protected virtual TextBox CreateTextBox()
    {
      return new TextBox();
    }

    protected override void DisposeEditor(Control editor)
    {
      var textBox = editor as TextBox;
      textBox.TextChanged -= EditorTextChanged;
      textBox.KeyDown -= EditorKeyDown;
    }

    private void EditorKeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Escape)
        EndEdit(false);
      else if (e.KeyCode == Keys.Enter)
        EndEdit(true);
    }

    private string _label;
    private void EditorTextChanged(object sender, EventArgs e)
    {
      var textBox = sender as TextBox;
      _label = textBox.Text;
      Parent.UpdateEditorBounds();
    }

    protected override void DoApplyChanges(TreeNodeAdv node, Control editor)
    {
      var label = (editor as TextBox).Text;
      string oldLabel = GetLabel(node);
      if (oldLabel != label)
      {
        SetLabel(node, label);
        OnLabelChanged(node.Tag, oldLabel, label);
      }
    }

    public override void Cut(Control control)
    {
      (control as TextBox).Cut();
    }

    public override void Copy(Control control)
    {
      (control as TextBox).Copy();
    }

    public override void Paste(Control control)
    {
      (control as TextBox).Paste();
    }

    public override void Delete(Control control)
    {
      var textBox = control as TextBox;
      int len = Math.Max(textBox.SelectionLength, 1);
      if (textBox.SelectionStart < textBox.Text.Length)
      {
        int start = textBox.SelectionStart;
        textBox.Text = textBox.Text.Remove(textBox.SelectionStart, len);
        textBox.SelectionStart = start;
      }
    }

    public event EventHandler<LabelEventArgs> LabelChanged;
    protected void OnLabelChanged(object subject, string oldLabel, string newLabel)
    {
      if (LabelChanged != null)
        LabelChanged(this, new LabelEventArgs(subject, oldLabel, newLabel));
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.