Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactor code to use Custom class instead of the one the System provides
    primarykey
    data
    text
    <p>Well, this is probably the dumbest question yet, but I have a huge problem that bothers me. First off, I used the code sample from <a href="http://dotnetrix.co.uk/tabcontrol.htm" rel="nofollow">Mick Doherty's Tab Control Tips</a> under reposition TabItems at runtime to allow drag and drop of my tabs in my control. The problem is I use a custom TabPage class by the name of <code>ExtendedTabPage</code> and this causes me trouble. I tried casting or using the <code>as</code> keyword but I am out of luck, so I would like someone to help me out on how to refactor the code to allow for dragging of the custom tabs.</p> <p><strong>EDIT:</strong> I forgot to mention that ExtendedTabPage is an abstract class that is inherited by my objects (for example one of my Windows belongs to ConsoleTab class that inherits ExtendedTabPage). Is this any relevant to the problem itself?</p> <p><strong>EDIT 2:</strong> Major discovery - In the DragOver method, if I try using ConsoleTab in the typeof statements, it seems to work perfectly fine. The problem is that I do not want to do this specifically for this class but for all classes inheriting from its parent class, which is abstract (and I can actually convert it to non-abstract if need be but I won't actively use it...).</p> <p><strong>EDIT 3:</strong> Maybe a good way would be to directly swap using the indexes and avoid using the TabPage Data, however I am a bit confused on how to do this...</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ReCodeConsole.Properties; namespace ReCodeConsole.Core { /// &lt;summary&gt; /// Implements all the extra functionality needed for tab pages on top of the existing TabControl. /// Includes events for DrawItem, MouseMove and MouseDown. /// &lt;/summary&gt; public partial class ExtendedTabControl : TabControl { /// &lt;summary&gt; /// Initializes a new instance of the ExtendedTabControl class. All events are added. /// &lt;/summary&gt; public ExtendedTabControl() :base() { this.DrawItem+=new DrawItemEventHandler(DrawTab); this.MouseClick+=new MouseEventHandler(Tab_OnMouseDown); this.MouseMove+=new MouseEventHandler(Tab_OnMouseMove); this.DragOver+=new DragEventHandler(Tab_OnDragOver); } /// &lt;summary&gt; /// Used to store the starting position of a tab drag event. /// &lt;/summary&gt; private Point DragStartPosition = Point.Empty; private void DrawTab(object sender, DrawItemEventArgs e) { // //This code will render the close button at the end of the Tab caption. // e.Graphics.DrawImage(Resources.TabCloseButton, e.Bounds.Right - 22, e.Bounds.Top + 5, 14, 14); e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 3); e.DrawFocusRectangle(); } private void Tab_OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // // Regardless of where the MouseDown event originated, save the coordinates for dragging. // DragStartPosition = new Point(e.X, e.Y); #region Close Button Handling // // Close button code - looping through the controls. // for (int i = 0; i &lt; this.TabPages.Count; i++) { Rectangle r = GetTabRect(i); // //Getting the position of the close button. // Rectangle closeButton = new Rectangle(r.Right - 22, r.Top + 5, 14, 14); if (closeButton.Contains(e.Location)) { if (this.TabPages[i] is ExtendedTabPage) { if ((this.TabPages[i] as ExtendedTabPage).IsCloseable) { if (MessageBox.Show("Are you sure you want to close this tab?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.TabPages.RemoveAt(i); break; } } } else { if (MessageBox.Show("Are you sure you want to close this tab?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.TabPages.RemoveAt(i); break; } } } } #endregion } private TabPage HoverTab() { for (int index = 0; index &lt;= TabCount - 1; index++) { if (GetTabRect(index).Contains(PointToClient(Cursor.Position))) return (TabPage)TabPages[index]; } return null; } private void Tab_OnDragOver(object sender, System.Windows.Forms.DragEventArgs e) { TabPage hover_Tab = HoverTab(); if (hover_Tab == null) e.Effect = DragDropEffects.None; else { if (e.Data.GetDataPresent(typeof(TabPage))) { e.Effect = DragDropEffects.Move; TabPage drag_tab = (TabPage)e.Data.GetData(typeof(TabPage)); if (hover_Tab == drag_tab) return; Rectangle TabRect = GetTabRect(TabPages.IndexOf(hover_Tab)); TabRect.Inflate(-3, -3); if (TabRect.Contains(PointToClient(new Point(e.X, e.Y)))) { SwapTabPages(drag_tab, hover_Tab); SelectedTab = drag_tab; } } } } private void Tab_OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; Rectangle r = new Rectangle(DragStartPosition, Size.Empty); r.Inflate(SystemInformation.DragSize); TabPage tp = HoverTab(); if (tp != null) { if (!r.Contains(e.X, e.Y)) DoDragDrop(tp, DragDropEffects.All); } DragStartPosition = Point.Empty; } private void SwapTabPages(TabPage tp1, TabPage tp2) { int Index1 = this.TabPages.IndexOf(tp1); int Index2 = this.TabPages.IndexOf(tp2); this.TabPages[Index1] = tp2; this.TabPages[Index2] = tp1; } } } </code></pre> <p>Now ignore anything that is extra, I give you the whole code in case anything else messes it all up. So to recap, I want someone to fix the code or at least explain how to do so (or if it isn't possible what to do) in order for me to be able to swap between ExtendedTabPage items instead of regular TabPage. I have tried and regular TabPage objects work, whereas my custom ones don't. If the solution has to include only one of them (so normal TabPages cannot work), go for only ExtendedTabPage as I can convert the rest of the stuff into that. I hope this is not like really dumb and very much a situation where I am overlooking something. </p> <p>P.S: I also checked the page I linked for a solution that worked for custom classes, but with no luck, as it caused me twice the problems and half my code broke even with the proper assembly references, so that is not a real option. :/</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload