Laden...

[ExtensionMethods] TabControlX - modifiziertes TabControl

Erstellt von ErfinderDesRades vor 15 Jahren Letzter Beitrag vor 15 Jahren 2.956 Views
ErfinderDesRades Themenstarter:in
5.299 Beiträge seit 2008
vor 15 Jahren
[ExtensionMethods] TabControlX - modifiziertes TabControl

Hi!

Heut binnich drauf gekommen, daß man mit ExtensionMethods auch neue Ereignisse an bestehende Controls dran-extenden kann.
Angewandt auf TabControl: Das selektiert jetzt eine TabPage schon im MouseOver des Register-Reiters (ohne Klick).
Und führt eine Aktion aus, wenn man auf den Reiter draufklickt.
(Das Sample kann helfen, Freund und Feind zu unterscheiden 😉 )


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TabControlEx {

   public partial class frmTabControlEx : Form {

      private List<string> _peoples = new List<string>(
         "Heinz Christel Pablo Fritz Caroline Otto ErfinderDesRades Loriot Ghandi Emma Jolanta Claudia Benjamin"
         .Split());

      public frmTabControlEx() {
         InitializeComponent();
         lstSelect.DataSource = _peoples;
         tabControl1.CreateClickRiders().RiderClick += new EventHandler(tabControl1_RiderClick);
      }

      void tabControl1_RiderClick(object sender, EventArgs e) {
         var person=_peoples[lstSelect.SelectedIndex];
         var tc=(TabControl)sender;
         switch (tc.SelectedIndex) {
            case 1: lstFriends.Items.Add(person);
               break;
            case 2: lstEnemies.Items.Add(person);
               break;
         };
      }

   }
}

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Runtime.InteropServices;

namespace System.Windows.Forms {

   public static class TabControlX {

      /// <summary> returnt Index des Register-Reiters unter der Maus </summary>
      [System.Diagnostics.DebuggerStepThrough]
      public static int GetTabRider(this TabControl subj) {
         return subj.GetTabRider(subj.PointToClient(Control.MousePosition));
      }

      /// <summary> returnt Index des Register-Reiters unter pt </summary>
      [System.Diagnostics.DebuggerStepThrough]
      public static int GetTabRider(this TabControl subj, Point pt) {
         for (int I = 0; I < subj.TabCount; I++) {
            if (subj.GetTabRect(I).Contains(pt)) return I;
         }
         return -1;
      }

      [System.Diagnostics.DebuggerStepThrough]
      public static ClickableRiderBehaviour CreateClickRiders(this TabControl subj) {
         return new ClickableRiderBehaviour(subj);
      }

      public class ClickableRiderBehaviour {

         public event EventHandler RiderClick = delegate { };

         public ClickableRiderBehaviour(TabControl tc) {
            tc.Click += (s, e) => {
               if (((TabControl)s).GetTabRider() >= 0) RiderClick(s, e);
            };
            tc.MouseMove += (s, e) => {
               var tabControl = (TabControl)s;
               if (tabControl.GetTabRect(tabControl.SelectedIndex).Contains(e.Location)) return;
               var indx = tabControl.GetTabRider(e.Location);
               if (indx >= 0) tabControl.SelectedIndex = indx;
            };
         }
      }//ClickableRiderBehaviour
   }
}

Schlagwörter: ExtensionMethods ,Extension,Extensions,TabControl

Der frühe Apfel fängt den Wurm.