Close any TabControl TabPage when middle clicked on - c#

I want to be able to middle click the tab on a TabPage and have it removed from the TabControl but even with HotTracking turned on, I don't know how to capture which tab I middle clicked on.
Is there a way to do that?

You could do something like this on the MouseClick event of your TabControl:
private void tabControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
for (int i = 0; i < tabControl.TabCount; i++)
{
if (tabControl.GetTabRect(i).Contains(e.Location))
{
tabPaControl.TabPages[i].Dispose();
}
}
}
}

Related

How to make sub for Picture box made during Run time in WinForms

So I have a button, and when the button is clicked, a picture box is created. I just wanted to know how I can make a message box appear when I click on that newly created picturebox.
private void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 1; i++)
{
PictureBox p = new PictureBox();
flowLayoutPanel1.Controls.Add(p);
}
}
you have to add a click event to your picturebox
p.MouseClick += p_MouseClick;
after adding a event this function will be called on that event -
void p_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("clicked");
}

How to restore and maximize a form without flickering and layout overlapping?

In my windows forms application, I added a SplitContainer control. In the panel1 of SplitContainer, I have a ListBox and in the panel2 of SpliContainer, I have two buttons. on the mouse move of ListBox I want to select the ListBox item. below are my codes to select the listbox item,
private void ListBox1_MouseMove(object sender, MouseEventArgs e)
{
int i;
this.SuspendLayout();
for (i = 0; i < (this.listBox1.Items.Count); i++)
{
if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
{
this.listBox1.SelectedIndex = i;
return;
}
}
this.ResumeLayout(true);
}
The SuspendLayout() and ResumeLayout() are called to avoid the overlapping of panels when the form is loading.
If I restore the form, mouse move on the ListBox and again maximize the form then the buttons in the SplitContainer panel2 is not loading properly. If I remove the Suspend and ResumeLayout the restore and maximize works fine. I referred this
stackoverflow query link.
The ListBox does not have Resize and dock property. So shouldn't I call the SuspendLayout and ResumeLayout? anyone suggest where to use SuspendLayout/ResumeLayout and where to not?
Try it like this:
private void ListBox1_MouseMove(object sender, MouseEventArgs e)
{
int newindex = ListBox1.IndexFromPoint(e.Location);
if (newindex != index) //avoid flickering
{
int i;
this.SuspendLayout();
for (i = 0; i < (this.listBox1.Items.Count); i++)
{
if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
{
this.listBox1.SelectedIndex = i;
index = newindex;
//return; why return?
}
}
this.ResumeLayout(true);
}
}
Just declare index as a global variable.

Right Clicking Action for Windows Form Buttons

Some quick context: in this project we're using a Visual C# Windows Form project to recreate Minesweeper.
I am using an array of Cells (which inherit from Control.Button).
As extra credit, I want the user to be able to flag a cell like you can in the class version of the game. However, I can't get right-clicking to work.
When trying to find a solution, I read that you need to typecast the EventArg as a MouseEventArg, but that didn't solve my problem as right-clicking doesn't even trigger my click event.
Here's some paraphrased code:
namespace Project_5___Minesweeper_GUI
{
public partial class Form1 : Form
{
public class Cell : Button { /*Custom Cell-Stuff Goes Here*/ }
Cell[,] board = new Cell[AXIS_LENGTH, AXIS_LENGTH]; //Axis Length is just the dimensions of the board (I use 10x10).
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < AXIS_LENGTH; i++)
{
for (int j = 0; j < AXIS_LENGTH; j++)
{
board[i, j] = new Cell();
//Set position and size
board[i, j].MouseClick += button_arrayClick; //button_arrayClick() is never called by a right-click. Code for it is below. I suspect this line of code has to do with right-clicks not class button_arrayClick().
groupBox1.Controls.Add(board[i, j]); //I'm containing the array of Cells inside of a groupbox.
}
}
}
private void button_arrayClick(object sender, EventArgs e) //Is prepared for handling a right-click, but never receives them.
{
Cell temp = (Cell)sender;
MouseEventArgs me = (MouseEventArgs)e;
if (me.Button == MouseButtons.Left)
{
//Stuff that happens on left-click
} else {
//Stuff that happens on right-click
}
}
}
}
This is where I grabbed the type-casting the event arguments from.
MouseClick does not handle right clicks for buttons. You can use MouseDown.
board[i, j].MouseDown += button_arrayClick;
Use the _MouseDown event instead.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//stuff that happen on right-click
}
else if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//stuff that happen on left click
}
}
listen for MouseDown event
private void button1_MouseDown(object sender, MouseEventArgs e)

How to link buttons with lines based in graphs

I have a "button creator"(that creates my own custom buttons) in my form and I need to, after creating some buttons in the form, clicking in 2 random ones to connect then with a simple line (can be a System.Drawing.Pen). And i should use some kind of graph logical connection to do it. But I have no idea how I should do. Any code suggestions? Thank You
Here is an example:
// two variables we will need:
Button lastBtn = null;
List<Tuple<Button, Button>> buttons = new List<Tuple<Button, Button>>();
void commonButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn == null) return;
if (lastBtn == null) { lastBtn = btn; return; }
else if (btn == lastBtn) { lastBtn = null; return; }
else { buttons.Add(new Tuple<Button, Button>(lastBtn, btn)); lastBtn = null; }
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach ( Tuple<Button, Button> t in buttons)
{
e.Graphics.DrawLine(Pens.RoyalBlue, t.Item1.Location, t.Item2.Location);
}
}
When creating your buttons hook each up with the common click event:
yourButtonClass btn = new yourButtonClass()..
..
btn.Click += commonButton_Click;
This always draw lines from and to the upper left corner. Using the middle instead or a smart loaction on an edge the is cloest to the other button if left for you. In the latter case you could also add start- and endcaps to the pen, if you want to.

How to check Mouse Out on a Tab area of TabPage in C# Winform?

I want to check Mouse In/Out on Tab Area of TabPage in C# Winform.
There are Event MouseLeave, MouseEnter, MouseMove, but there work for whole TabPage. I just want for Tab only.
TabControl tabControl = new TabControl();
TabPage tabpage = new TabPage();
tabpage.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);
tabControl.Controls.Add(tabpage);
this.Controls.Add(tabControl);
I'm thinking that If I get to know the Tab area so that I can write Code in MouseMove event for the same, Is there any better way to do the same.
I want for the area pointed by the arrow in the attached image.
Tab
The GetTabRect function would help you here:
TabPage mouseTab = null;
void tabControl1_MouseMove(object sender, MouseEventArgs e) {
TabPage checkTab = null;
for (int i = 0; i < tabControl1.TabPages.Count; ++i) {
if (tabControl1.GetTabRect(i).Contains(e.Location)) {
checkTab = tabControl1.TabPages[i];
break; // To avoid unnecessary loop
}
}
if (checkTab == null && mouseTab != null) {
mouseTab = null;
} else if (checkTab != null) {
if (mouseTab == null || !checkTab.Equals(mouseTab)) {
mouseTab = checkTab;
// or do something here...
}
}
}
And to handle the mouse leaving the tab header area:
void tabControl1_MouseLeave(object sender, EventArgs e) {
if (mouseTab != null) {
// do something here with mouseTab...
mouseTab = null;
}
}
You can place a Panel control on the whole area of the Tab, and then use the events you mentioned for that Panel control..

Categories