Get the TabPage from a MouseClick??? 
Author Message
 Get the TabPage from a MouseClick???

I try to know which tab i've click on, but can't get it? What is the best
way to know that?

Here's the code i've tried in many way:

private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
{
 if (e.Button == MouseButtons.Right)
 {
  TabControl ctrl = (TabControl)sender;
  if (ctrl != null)
  {
   Point pt = new Point(e.X, e.Y);
   Point ptChild = ctrl.PointToClient(pt);
   Control child = ctrl.GetChildAtPoint(ptChild);
   if (child != null)
   {
    TabPage page = (TabPage)child;
    if (page != null)
    {
        // Look the name?
    }
   }
  }
 }

Quote:
}



Sat, 13 Nov 2004 22:44:32 GMT  
 Get the TabPage from a MouseClick???
I've used the SelectedIndexChanged event then a switch statement on the tab
control's SelectedIndex property.  Works fine.

   switch(tabControl.SelectedIndex)
   {
    case 0 : // First tab
     break;
    case 1 : // Second tab
     break;
    case 2 : // Third tab
     break;
    case 3 : // Fourth tab
     break;
   }

Hope this helps.

Ian


Quote:
> I try to know which tab i've click on, but can't get it? What is the best
> way to know that?

> Here's the code i've tried in many way:

> private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> {
>  if (e.Button == MouseButtons.Right)
>  {
>   TabControl ctrl = (TabControl)sender;
>   if (ctrl != null)
>   {
>    Point pt = new Point(e.X, e.Y);
>    Point ptChild = ctrl.PointToClient(pt);
>    Control child = ctrl.GetChildAtPoint(ptChild);
>    if (child != null)
>    {
>     TabPage page = (TabPage)child;
>     if (page != null)
>     {
>         // Look the name?
>     }
>    }
>   }
>  }
> }



Sun, 14 Nov 2004 00:46:34 GMT  
 Get the TabPage from a MouseClick???
Eric,

    You might want to take a look at the TabIndexChanged event, which will
fire when the tab index is changed at all (through code or through a click).

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I try to know which tab i've click on, but can't get it? What is the best
> way to know that?

> Here's the code i've tried in many way:

> private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> {
>  if (e.Button == MouseButtons.Right)
>  {
>   TabControl ctrl = (TabControl)sender;
>   if (ctrl != null)
>   {
>    Point pt = new Point(e.X, e.Y);
>    Point ptChild = ctrl.PointToClient(pt);
>    Control child = ctrl.GetChildAtPoint(ptChild);
>    if (child != null)
>    {
>     TabPage page = (TabPage)child;
>     if (page != null)
>     {
>         // Look the name?
>     }
>    }
>   }
>  }
> }



Sun, 14 Nov 2004 00:51:06 GMT  
 Get the TabPage from a MouseClick???
You are right, but i want to capture a right-click on the tab... so which is
the right way to do this?


Quote:
> I've used the SelectedIndexChanged event then a switch statement on the
tab
> control's SelectedIndex property.  Works fine.

>    switch(tabControl.SelectedIndex)
>    {
>     case 0 : // First tab
>      break;
>     case 1 : // Second tab
>      break;
>     case 2 : // Third tab
>      break;
>     case 3 : // Fourth tab
>      break;
>    }

> Hope this helps.

> Ian



> > I try to know which tab i've click on, but can't get it? What is the
best
> > way to know that?

> > Here's the code i've tried in many way:

> > private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> > {
> >  if (e.Button == MouseButtons.Right)
> >  {
> >   TabControl ctrl = (TabControl)sender;
> >   if (ctrl != null)
> >   {
> >    Point pt = new Point(e.X, e.Y);
> >    Point ptChild = ctrl.PointToClient(pt);
> >    Control child = ctrl.GetChildAtPoint(ptChild);
> >    if (child != null)
> >    {
> >     TabPage page = (TabPage)child;
> >     if (page != null)
> >     {
> >         // Look the name?
> >     }
> >    }
> >   }
> >  }
> > }



Sun, 14 Nov 2004 01:38:25 GMT  
 Get the TabPage from a MouseClick???
You are right, but i want to capture a right-click on the tab... so which is
the right way to do this?



Quote:
> Eric,

>     You might want to take a look at the TabIndexChanged event, which will
> fire when the tab index is changed at all (through code or through a
click).

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > I try to know which tab i've click on, but can't get it? What is the
best
> > way to know that?

> > Here's the code i've tried in many way:

> > private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> > {
> >  if (e.Button == MouseButtons.Right)
> >  {
> >   TabControl ctrl = (TabControl)sender;
> >   if (ctrl != null)
> >   {
> >    Point pt = new Point(e.X, e.Y);
> >    Point ptChild = ctrl.PointToClient(pt);
> >    Control child = ctrl.GetChildAtPoint(ptChild);
> >    if (child != null)
> >    {
> >     TabPage page = (TabPage)child;
> >     if (page != null)
> >     {
> >         // Look the name?
> >     }
> >    }
> >   }
> >  }
> > }



Sun, 14 Nov 2004 01:38:31 GMT  
 Get the TabPage from a MouseClick???
Eric,

    Basically, you want to handle the MouseUp event.  When you do that, you
can look at the Button property of the MouseEventArgs instance passed to
your event handler.

    Getting the tab that is right clicked isn't as easy.  You could call
GetTabRect for each tab, and see if the x and y corrdinates in the event
handler fall within that rectangle.

    If you don't like that, then you can use the P/Invoke layer to call
SendMessage, sending the TCM_HITTEST message to the control.  It will return
the index of the tab that is under the point specified in the TCHITTESTINFO
structure.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> You are right, but i want to capture a right-click on the tab... so which
is
> the right way to do this?


wrote

> > Eric,

> >     You might want to take a look at the TabIndexChanged event, which
will
> > fire when the tab index is changed at all (through code or through a
> click).

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > I try to know which tab i've click on, but can't get it? What is the
> best
> > > way to know that?

> > > Here's the code i've tried in many way:

> > > private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> > > {
> > >  if (e.Button == MouseButtons.Right)
> > >  {
> > >   TabControl ctrl = (TabControl)sender;
> > >   if (ctrl != null)
> > >   {
> > >    Point pt = new Point(e.X, e.Y);
> > >    Point ptChild = ctrl.PointToClient(pt);
> > >    Control child = ctrl.GetChildAtPoint(ptChild);
> > >    if (child != null)
> > >    {
> > >     TabPage page = (TabPage)child;
> > >     if (page != null)
> > >     {
> > >         // Look the name?
> > >     }
> > >    }
> > >   }
> > >  }
> > > }



Sun, 14 Nov 2004 03:09:38 GMT  
 Get the TabPage from a MouseClick???

Hey Eric , this is my codes bellow:
  /// <summary>
  /// Catch the Mouse_Up Event, and to Display Popup Menu
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void tabDoc_MouseUp(object sender,System.Windows.Forms.MouseEventArgs e)
  {
   System.Drawing.Point _lp ;
   if(Win32API.GetCursorPos (out _lp))
   {
    //Console.WriteLine(_lp.X.ToString (),_lp.Y.ToString ());
   }
   System.Drawing.Rectangle _rt = new System.Drawing.Rectangle (_lp,this.Cursor.Size );
   System.Drawing.Rectangle _rtTab;
   if(e.Button == System.Windows.Forms.MouseButtons.Right )
   {
    for(int i=0;i<this.tabDoc.TabCount ;i++)
    {
     //if(this.tabDoc.GetTabRect (i).IntersectsWith (_rt))
     _rtTab = this.tabDoc.GetTabRect (i);
     _rtTab = this.tabDoc.TabPages [i].RectangleToScreen (_rtTab);
     if( (_rt.X >= _rtTab.X && _rt.X <= _rtTab.X +_rtTab.Width)
      //&&
      //(_rt.Y >= _rtTab.Y && _rt.Y <= _rtTab.Y + _rtTab.Height)
      )
     {
      this.tabDoc.SelectedIndex = i;
      ShowPopupMenu(sender);
      break;
     }
    }
   }
  }
  /// <summary>
  /// Display PopupMenu
  /// </summary>
  /// <param name="sender"></param>
  private void ShowPopupMenu(object sender)
  {
   System.Drawing.Point _pt = new System.Drawing.Point ();
   System.Drawing.Rectangle _rt;
   _rt = this.tabDoc.GetTabRect (this.tabDoc.SelectedIndex ) ;
   _pt.X = _rt.X ;
   if(this.tabDoc.Dock == System.Windows.Forms.DockStyle.Top )
   {
    _pt.Y = _rt.Y + _rt.Height -20;
   }
   else
   {
    _pt.Y = _rt.Y - _rt.Height;
   }
   this.contextMenu1.Show (this.tabDoc.SelectedTab,_pt);
  }

Hope this helps.

Wilson Wei

Quote:

> I try to know which tab i've click on, but can't get it? What is the best
> way to know that?

> Here's the code i've tried in many way:

> private void OnMainTabControlMouseUp(object sender, MouseEventArgs e)
> {
>  if (e.Button == MouseButtons.Right)
>  {
>   TabControl ctrl = (TabControl)sender;
>   if (ctrl != null)
>   {
>    Point pt = new Point(e.X, e.Y);
>    Point ptChild = ctrl.PointToClient(pt);
>    Control child = ctrl.GetChildAtPoint(ptChild);
>    if (child != null)
>    {
>     TabPage page = (TabPage)child;
>     if (page != null)
>     {
>         // Look the name?
>     }
>    }
>   }
>  }
> }



Sun, 14 Nov 2004 12:52:35 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. KEYDOWN on MouseClick event ???

2. Simulating a mouseclick...

3. What is the API command to flush mouseclicks

4. displaying bitmaps and responding to mouseclicks on them

5. Q: Mouseclick in Editbox

6. Mouseclick on button notifying Parent

7. VBScript and mouseclicks

8. TabPage question

9. How to display richtextbox scrollbars on a tabpage?

10. Newbit: help with TabPage and Controls

11. Adding objects to a tabpage programmitically

12. TabControl tabpage change

 

 
Powered by phpBB® Forum Software