Change SelectedTab of TabControl on MouseOver - c#

I have a Windows Forms project with a TabControl.
Does anyone know how to change the SelectedTab when you hover over it with the pointer?

You can use TabControl's MouseMove event to detect whether your mouse is present on any tab and then can select it:
private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
Rectangle mouseRect = new Rectangle(e.X, e.Y, 1, 1);
for (int i = 0; i < tabControl1.TabCount; i++)
{
if (tabControl1.GetTabRect(i).IntersectsWith(mouseRect))
{
tabControl1.SelectedIndex = i;
break;
}
}
}

Try this:
private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
for (int i = 0; i < tabControl1.TabCount; i++)
{
if (tabControl1.GetTabRect(i).Contains(e.X, e.Y))
{
tabControl1.SelectedIndex = i;
break;
}
}
}

Related

DataGridView OnRowPostPaint draw

Actually, my code is "selecting hover" all the line, when i have the mouse over the cell, but, i want to change it, i want to select all the column, not the line.
Data Grid
How can i manage it?
My code is:
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
base.OnRowPostPaint(e);
if (this.RectangleToScreen(e.RowBounds).Contains(MousePosition))
{
using (var b = new SolidBrush(Color.FromArgb(50, Color.Black)))
using (var p = new Pen(Color.MediumVioletRed))
{
var r = e.RowBounds;
r.Width -= 1;
r.Height -= 1;
e.Graphics.FillRectangle(b, e.RowBounds.X, e.RowBounds.Y,e.RowBounds.Width, e.RowBounds.Height);
e.Graphics.DrawRectangle(p, r);
}
}
}
I can find the same method, but to draw vertically...
Thank you
If You just want to select entire column while mouse is over any cell then You don't have to use OnRowPostPaint event , You can make the selection mode for the entire grid to select entire column instead of only one cell or entire row but to do that You need to make the SortMode for the Columns set to anything other than Automatic. here is how to do it:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
}
then use either OnCellMouseEnter or OnCellMouseMove events to select whole column like this:
private void OnCellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
return;
dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];
}
refer to this Answer for more information on selecting entire column.
Maybe not exactly what you want but maybe this example can get you started:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the inset highlight box.
e.Graphics.DrawRectangle(Pens.Blue, newRect);
// Draw the text content of the cell, ignoring alignment.
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
The example comes from this page learn.microsoft.com DataGridView.OnCellPainting
This will select all the cells within the hovered column... though it's not really clear what you are asking for.
using System.Windows.Forms;
namespace DatagridViewSelectAllCells_58842788
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
for (int i = 0; i < 10; i++)
{
dataGridView1.Rows.Add($"col1_{i}", $"col2_{i}");
}
dataGridView1.CellMouseEnter += DataGridView1_CellMouseEnter;
}
private void DataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == -1)//don't do anything if the rowheader is being hovered
{
return;
}
if (dataGridView1.SelectedCells.Count > 1 && e.ColumnIndex == dataGridView1.SelectedCells[0].ColumnIndex)//don't do anything if the column is already selected
{
return;
}
dataGridView1.ClearSelection();//clear the current selection
//select all the cells in that hovered column
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1[e.ColumnIndex, i].Selected = true;
}
}
}
}
here's an updated answer for just changing the color of the cells
using System.Windows.Forms;
using System.Drawing;
namespace DatagridViewSelectAllCells_58842788
{
public partial class Form1 : Form
{
static Color originalColor;
static int highlightedCol = -1;
static DataGridView dataGridView1 = new DataGridView();
public Form1()
{
InitializeComponent();
Controls.Add(dataGridView1);
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
for (int i = 0; i < 10; i++)
{
dataGridView1.Rows.Add($"col1_{i}", $"col2_{i}");
}
dataGridView1.CellMouseEnter += DataGridView1_CellMouseEnter;
dataGridView1.CellMouseLeave += DataGridView1_CellMouseLeave;
dataGridView1[1, 4].Style.BackColor = Color.DarkOliveGreen;
}
private void DataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
//WithCellStyle(sender, e, "Leave");
WithDefaultStyle(sender, e, "Leave");
}
private void DataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
//WithCellStyle(sender, e, "Enter");
WithDefaultStyle(sender, e, "Enter");
}
/*
* to handle cells that have specific style attributes
* like cell dataGridView1[1, 4]
* there would be more to this to keep track of multiple different cell styles on the Leave side of thing
* but that's up to you to handle if you have a need for it.
*/
private static void WithCellStyle(object sender, DataGridViewCellEventArgs e, string eType)
{
switch (eType)
{
case "Enter":
if (originalColor == null)
{
originalColor = ((Control)sender).BackColor;//store the color for leave event
}
highlightedCol = e.ColumnIndex;//set which column is highlighted for leave event
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1[highlightedCol, i].Style.BackColor = Color.Aquamarine;
}
break;
case "Leave":
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1[highlightedCol, i].Style.BackColor = originalColor;
}
break;
default:
break;
}
}
/*
* if you only have default styles
*/
private static void WithDefaultStyle(object sender, DataGridViewCellEventArgs e, string eType)
{
switch (eType)
{
case "Enter":
if (originalColor == null)
{
originalColor = ((Control)sender).BackColor;//store the color for leave event
}
highlightedCol = e.ColumnIndex;//set which column is highlighted for leave event
//change color of all the cells
dataGridView1.Columns[highlightedCol].DefaultCellStyle.BackColor = Color.Aquamarine;//set the new color
break;
case "Leave":
//per defaultstyle
dataGridView1.Columns[highlightedCol].DefaultCellStyle.BackColor = originalColor;
break;
default:
break;
}
}
}
}

How to scroll flow layout panel with button click event

I am adding dyanmic controls(textboxes) in flowlayout panel.I want the controls to scroll when I click on a button(up or down button).The scroll buttons should not be visible.How can I achieve this.Any suggestions?? It is windows application using c#.net
try following code
int yLoc = 0;
private void Button1_Click(object sender, EventArgs e)
{
if (flowPanel.Location.Y <= yLoc && flowPanel.Location.Y >= flowPanel.VerticalScroll.Minimum)
{
yLoc -= 50;
flowPanel.Location = new Point(0, yLoc);
}
}
private void Button2_Click(object sender, EventArgs e)
{
if (flowPanel.Location.Y <= yLoc && flowPanel.Location.Y < flowPanel.VerticalScroll.Maximum)
{
yLoc += 50;
flowPanel.Location = new Point(0, yLoc);
}
}

How to determine click event for userdefine control array

I have created an Array of UserControls which have 1 PictureBox and 1 Button. Now I want to know which Button is pressed from the Array of UserControl.
UserControl u=new UserControl[20];
for (int j = 0; j < 20; j++)
{
u[j] = new UserControl();
u[j].BringToFront();
flowLayoutPanel1.Controls.Add(u[j]);
u[j].Visible = true;
u[j].button1.Click+=new EventHandler(sad);
}
private void sad(object sender, EventArgs e)
{
//how to determine which button from the array of usercontrol is pressed?
}
The sender parameter contains the Control instance that generated the event.
something like this:
private void sad(object sender, EventArgs e) {
var buttonIndex = Array.IndexOf(u, sender);
}
This should do close to what you want. I can modify as needed to suit your case.
FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();
void LoadControls()
{
UserControl[] u= new UserControl[20];
for (int j = 0; j < 20; j++)
{
u[j] = new UserControl();
u[j].BringToFront();
flowLayoutPanel1.Controls.Add(u[j]);
u[j].Visible = true;
u[j].button1.Click +=new EventHandler(sad);
}
}
private void sad(object sender, EventArgs e)
{
Control c = (Control)sender;
//returns the parent Control of the sender button
//Could be useful
UserControl parent = (UserControl)c.Parent; //Cast to appropriate type
//Check if is a button
if (c.GetType() == typeof(Button))
{
if (c.Name == <nameofSomeControl>) // Returns name of control if needed for checking
{
//Do Something
}
}
//Check if is a Picturebox
else if (c.GetType() == typeof(PictureBox))
{
}
//etc. etc. etc
}
I think this gets you what you want:
if (sender is UserControl)
{
UserControl u = sender as UserControl();
Control buttonControl = u.Controls["The Button Name"];
Button button = buttonControl as Button;
}

How to automatically create pictureboxes?

I would like to automatically create a PictureBox. How to change this in the code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox[] box = new PictureBox[textBox1.Text.Length];
for(int j=0;j<textBox1.Text.Length;j++)
box[0] = pictureBox1;
box[1] = pictureBox2;
box[2] = pictureBox3;
for (int i = 0; i < textBox1.Text.Length; ++i)
box[i].Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
}
You should probably use a FlowLayoutPanel control to hold your PictureBox controls. Then the code would look something like this:
void button1_Click(object sender, EventArgs e) {
while (flowLayoutPanel1.Controls.Count > 0) {
flowLayoutPanel1.Controls[0].Dispose();
}
for (int i = 0; i < textBox1.Text.Length; ++i) {
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
flowLayoutPanel1.Controls.Add(pb);
}
}

Changing properties of the sender object without knowing its name

I am trying to change the BackColor of a PictureBox in a grid. The PictureBox is part of an array and the array has a chared event handler. I am having difficulty changing different PictureBox's depending on which one is clicked.
This is what I have so far:
private PictureBox[,] GameGrid = new PictureBox[20, 20];
public frmGame()
{
int x = 10;
int y = 10;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
GameGrid[i, j] = new System.Windows.Forms.PictureBox();
setUpPicBox(x, y, i, j);
x += 11;
}
y += 11;
x = 10;
}
InitializeComponent();
}
public void setUpPicBox(int x, int y, int i, int j)
{
this.GameGrid[i, j].Location = new System.Drawing.Point(x, y);
this.GameGrid[i, j].Size = new System.Drawing.Size(10, 10);
this.GameGrid[i, j].BackColor = Color.Black;
this.GameGrid[i, j].Name = "btnGrid" + i + "-" + j;
this.GameGrid[i, j].Visible = true;
this.GameGrid[i, j].CreateGraphics();
this.GameGrid[i, j].Click += new System.EventHandler(this.picturebox_Click);
this.Controls.Add(GameGrid[i, j]);
}
private void picturebox_Click(object sender, EventArgs e)
{
}</code>
Any help would be appreciated
The event handler's sender parameter is the instance that raised the event. Here it is the PictureBox instance that the user has clicked. If you want to change its BackColor, you just cast the sender object to correct type and set the new color.
private void picturebox_Click(object sender, EventArgs e)
{
var pictureBox = sender as PictureBox;
if (pictureBox != null) {
pictureBox.BackColor = Color.Blue;
}
}
On your event handler, sender contains the object that caused the event handler to fire. So by casting it to the correct type, we can then access all the properties as per this example:
private void picturebox_Click(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender;
MessageBox.Show(pic.Name);
}
Note: code untested, not got access to VS to test

Categories