I have small problem. I want make program where I can drag generated labels between multiple FlowLayoutPanels. But last few days I have tried to make drag and drop working. I tried many tutorials, examples etc. but it is always something bit diferent and I am not able extract only basic code.
It is similar to this program but it is in Visual Basic and I need it in C#. I know it is maybe very simple, but I am newbie.
Thank you for help.
Real Drag&Drop ist most useful between applications and maybe also between Forms.
Assuming you want to drag Labels between FLPs on the same Form, the code below should get you going..
It takes two FlowLayoutPanels called FLP1 and FLP2 and starts by initializing them with a few Labels.
Note the three mouse events I add to each Label to emulate a Drag&Drop action!
private void Form1_Load(object sender, EventArgs e)
{
// create a few Label with varying Colors for testing..
fillFLP(FLP1, 88);
fillFLP(FLP2, 111);
}
void fillFLP(FlowLayoutPanel FLP, int cc)
{
for (int i = 0; i < 24; i++)
{
Label l = new Label();
// the next 3 lines optional and only are there for testing!
l.AutoSize = false;
l.Text = FLP.Name + " " + i.ToString("00");
l.BackColor = Color.FromArgb(255, cc * 2 - i, 255 - 5 * i, cc + 5 * i);
// add controls and set mouse events:
FLP.Controls.Add(l);
l.MouseDown += l_MouseDown;
l.MouseMove += l_MouseMove;
l.MouseUp += l_MouseUp;
}
}
// the currently moved Label:
Label mvLabel = null;
void l_MouseDown(object sender, MouseEventArgs e)
{
// keep reference
mvLabel = (Label)sender;
}
void l_MouseMove(object sender, MouseEventArgs e)
{
// if we are dragging a label:
if (mvLabel != null)
{
// mouse pos in window coords
Point mvPoint = this.PointToClient(Control.MousePosition);
// the label is still in the FLP, so we start the drg action:
if (mvLabel.Parent != this)
{
mvLabel.Parent = this;
mvLabel.Location = mvPoint;
mvLabel.BringToFront();
}
else
{
// we are already in the form, so we just move
mvLabel.Location = mvPoint;
}
}
}
void l_MouseUp(object sender, MouseEventArgs e)
{
// are we over a FLP? and if so which?
Point MP = Control.MousePosition;
FlowLayoutPanel FLP = null;
Point mLoc1 = FLP1.PointToClient(MP);
Point mLoc2 = FLP2.PointToClient(MP);
if (FLP1.ClientRectangle.Contains(mLoc1)) FLP = FLP1;
else if (FLP2.ClientRectangle.Contains(mLoc2)) FLP = FLP2;
else return; // no! nothing we can do..
// yes, now find out if we are over a label..
// ..or over an empty area
mvLabel.SendToBack();
Control cc = FLP.GetChildAtPoint(FLP.PointToClient(MP));
// if we are over the FLP we can insert at the beginning or the end:
// int mvIndex = 0; // to the beginning
int mvIndex = FLP.Controls.Count; // to the end
// we are over a Label, so we insert before it:
if (cc != null) mvIndex = FLP.Controls.IndexOf(cc);
// move the Label into the FLP
FLP.Controls.Add(mvLabel);
// move it to the right position:
FLP.Controls.SetChildIndex(mvLabel, mvIndex);
// let go of the reference
mvLabel = null;
}
This lets you drag and drop Lables to and fro between two FLPs and also within the FLPs by dropping onto Labels.
Note that you will need a few extra lines if you want to allow dropping between Labels and still position there..
Related
I have a datagridview with multiple column and I had create a custom checkbox in one of the column header . Since I want to fix the checkbox header in the column when i scroll the bar, so i create 'scroll'event to handle it.
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
chbx.Location = new Point(chbx.Location.X - (e.NewValue - e.OldValue), chbx.Location.Y);
}
if (chbx.Location.X < dataGridView1.Location.X)
{
chbx.Visible = false;
}
else
{
chbx.Visible = true;
}
}
It's works (Picture 1). However, when i click the refresh button(data in gridview refresh) and i scroll the bar again, it comes with the problem below(Picture2).
Picture 1
Picture 2.
private void btnRefresh_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
SetupDGV();
}
private void SetupDGV()
{
///.....
chbx = new CheckBox();
Point headerCellLocation = dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
chbx.Name = "enableCHBX";
chbx.Location = new Point(headerCellLocation.X + 60, headerCellLocation.Y + 8);
chbx.BackColor = Color.White;
chbx.Size = new Size(15, 15);
// Assign Click event to the Header CheckBox.
cbx.Click += new EventHandler(chbxEnable_Click);
dataGridView1.Controls.Add(chbx);
}
Any suggestion on this?
I have removed the dataGridView1_Scroll Event Handler.
Instead I have used the Paint event using: this.dataGridView1.Paint += this.Handle_Paint;
Inside the Paint Event Handler I have the following code:
private void Handle_Paint(object sender, PaintEventArgs e)
{
//// Get the header cell
var cell = this.dataGridView1.Columns[0].HeaderCell;
//// Get the Location
var headerCellLocation = dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
//// Get the Horizontal Scrolling Offset (if the User has scrolled)
var scrollOffset = this.dataGridView1.HorizontalScrollingOffset;
//// Get the Cell width (minus) the checkbox width (minus) any scrolling there is
var widthVal = cell.Size.Width - this.chbx.Size.Width - scrollOffset;
//// Get 1/4 of the Cells height (so the checkbox sits in the center)
var quarterCellHeight = cell.Size.Height / 4;
//// Calculate the new Location for the Checkbox
var newLocation = new Point(headerCellLocation.X + widthVal, headerCellLocation.Y + quarterCellHeight);
//// Update the Location
this.chbx.Location = newLocation;
}
I have also added the following if statement inside of the SetupDGV method:
if (this.dataGridView1.Controls.Contains(this.chbx))
{
this.dataGridView1.Controls.Remove(this.chbx); //// Remove so we don't have a duplicate
}
Which ensures we won't create a duplicate checkbox inside of the dataGridView when we refresh the Data for the grid.
Here is a quick little video of the result:
https://gyazo.com/e544b38b1ca065bb16e4f279fc5aa24e
This question already has answers here:
How to move and resize a form without a border?
(7 answers)
Closed 7 years ago.
I have created a border-less form using c# but could make the custom title bar movable so I search the internet and found this code:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
After applying this code can click and drag my form in every inch of the form except for the title bar .
This is a good example of the movable title bar.
This is a full example
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Custom_Title_Bar
{
public partial class MainForm : Form
{
private PictureBox title = new PictureBox(); // create a PictureBox
private Label minimise = new Label(); // this doesn't even have to be a label!
private Label maximise = new Label(); // this will simulate our this.maximise box
private Label close = new Label(); // simulates the this.close box
private bool drag = false; // determine if we should be moving the form
private Point startPoint = new Point(0, 0); // also for the moving
public MainForm()
{
this.FormBorderStyle = FormBorderStyle.None;// get rid of the standard title bar
this.title.Location = this.Location; // assign the location to the form location
this.title.Width = this.Width; // make it the same width as the form
this.title.Height = 50; // give it a default height (you may want it taller/shorter)
this.title.BackColor = Color.Black; // give it a default colour (or load an image)
this.Controls.Add(this.title); // add it to the form's controls, so it gets displayed
// if you have an image to display, you can load it, instead of assigning a bg colour
// this.title.Image = new Bitmap(System.Environment.CurrentDirectory + "\\title.jpg");
// if you displayed an image, alter the SizeMode to get it to display as you want it to
// examples:
// this.title.SizeMode = PictureBoxSizeMode.StretchImage;
// this.title.SizeMode = PictureBoxSizeMode.CenterImage;
// this.title.SizeMode = PictureBoxSizeMode.Zoom;
// etc
// you may want to use PictureBoxes and display images
// or use buttons, there are many alternatives. This is a mere example.
this.minimise.Text = "Minimise"; // Doesn't have to be
this.minimise.Location = new Point(this.Location.X + 5, this.Location.Y + 5); // give it a default location
this.minimise.ForeColor = Color.Red; // Give it a colour that will make it stand out
// this is why I didn't use an image, just to keep things simple:
this.minimise.BackColor = Color.Black; // make it the same as the PictureBox
this.Controls.Add(this.minimise); // add it to the form's controls
this.minimise.BringToFront(); // bring it to the front, to display it above the picture box
this.maximise.Text = "Maximise";
// remember to make sure it's far enough away so as not to overlap our minimise option
this.maximise.Location = new Point(this.Location.X + 60, this.Location.Y + 5);
this.maximise.ForeColor = Color.Red;
this.maximise.BackColor = Color.Black; // remember, we want it to match the background
this.maximise.Width = 50;
this.Controls.Add(this.maximise); // add it to the form
this.maximise.BringToFront();
this.close.Text = "Close";
this.close.Location = new Point(this.Location.X + 120, this.Location.Y + 5);
this.close.ForeColor = Color.Red;
this.close.BackColor = Color.Black;
this.close.Width = 37; // this is just to make it fit nicely
this.Controls.Add(this.close);
this.close.BringToFront();
// now we need to add some functionality. First off, let's give those labels
// MouseHover and MouseLeave events, so they change colour
// Since they're all going to change to the same colour, we can give them the same
// event handler, which saves time of writing out all those extra functions
this.minimise.MouseEnter += new EventHandler(Control_MouseEnter);
this.maximise.MouseEnter += new EventHandler(Control_MouseEnter);
this.close.MouseEnter += new EventHandler(Control_MouseEnter);
// and we need to do the same for MouseLeave events, to change it back
this.minimise.MouseLeave += new EventHandler(Control_MouseLeave);
this.maximise.MouseLeave += new EventHandler(Control_MouseLeave);
this.close.MouseLeave += new EventHandler(Control_MouseLeave);
// and lastly, for these controls, we need to add some functionality
this.minimise.MouseClick += new MouseEventHandler(Control_MouseClick);
this.maximise.MouseClick += new MouseEventHandler(Control_MouseClick);
this.close.MouseClick += new MouseEventHandler(Control_MouseClick);
// finally, wouldn't it be nice to get some moveability on this control?
this.title.MouseDown += new MouseEventHandler(Title_MouseDown);
this.title.MouseUp += new MouseEventHandler(Title_MouseUp);
this.title.MouseMove += new MouseEventHandler(Title_MouseMove);
}
private void Control_MouseEnter(object sender, EventArgs e)
{
if (sender.Equals(this.close))
this.close.ForeColor = Color.White;
else if (sender.Equals(this.maximise))
this.maximise.ForeColor = Color.White;
else // it's the minimize label
this.minimise.ForeColor = Color.White;
}
private void Control_MouseLeave(object sender, EventArgs e)
{
// return them to their default colors
if (sender.Equals(this.close))
this.close.ForeColor = Color.Red;
else if (sender.Equals(this.maximise))
this.maximise.ForeColor = Color.Red;
else // it's the minimise label
this.minimise.ForeColor = Color.Red;
}
private void Control_MouseClick(object sender, MouseEventArgs e)
{
if (sender.Equals(this.close))
this.Close(); // close the form
else if (sender.Equals(this.maximise))
{
// maximise is more interesting. We need to give it different functionality,
// depending on the window state (Maximise/Restore)
if (this.maximise.Text == "Maximise")
{
this.WindowState = FormWindowState.Maximized; // maximise the form
this.maximise.Text = "Restore"; // change the text
this.title.Width = this.Width; // stretch the title bar
}
else // we need to restore
{
this.WindowState = FormWindowState.Normal;
this.maximise.Text = "Maximise";
}
}
else // it's the minimise label
this.WindowState = FormWindowState.Minimized; // minimise the form
}
void Title_MouseUp(object sender, MouseEventArgs e)
{
this.drag = false;
}
void Title_MouseDown(object sender, MouseEventArgs e)
{
this.startPoint = e.Location;
this.drag = true;
}
void Title_MouseMove(object sender, MouseEventArgs e)
{
if (this.drag)
{
// if we should be dragging it, we need to figure out some movement
Point p1 = new Point(e.X, e.Y);
Point p2 = this.PointToScreen(p1);
Point p3 = new Point(p2.X - this.startPoint.X,
p2.Y - this.startPoint.Y);
this.Location = p3;
}
}
} // end of the class
} // end of the namespace
If you want you can extract just the moving code and integrate it with your code, the movable Title code is just in the following Event Handlers
Title_MouseUp
Title_MouseDown
Title_MouseMove
Here is the original article for this code, you can read it for more explanation about the code
http://www.dreamincode.net/forums/topic/64981-designing-a-custom-title-bar/
The link to original article is broken
I have been attempting to understand the tiled listView in c#.net for weeks now with no succession.
I have shrunk my form's height/width (154x154) to the same tile size as the picture(s) (items with pictures) in my listView (which is docked in the form2); when my form2 is shown, you see item[0] in the listView...that is it! (which I want so far, so good!)
I have set the scrollable property to false to do away with both scrollbars (works perfectly fine so far...)
I have hooked both the left and right arrow keys GLOBALLY (which works as it has been thoroughly debugged) and when clicking the RIGHT arrow key, it should VERTICALLY bring up item[1], and so fourth.
The LEFT arrow key would bring up the previous item till it hits 0.
I have tried the following, but it does NOTHING.
private void HotkeyHandler(int i)
{
{
if (this.listView1.InvokeRequired)
{
SetHotkeyCallback p = new SetHotkeyCallback(HotkeyHandler);
this.Invoke(p, new object[] { i });
}
else
{
switch (i)
{
case 1:
listView1.View = View.List;
if (listView1.TopItem.Index > 0)
{
listView1.TopItem = listView1.Items[listView1.TopItem.Index - 1];
}
listView1.View = View.Tile;
break;
case 2:
listView1.View = View.List;
if (listView1.TopItem.Index < listView1.Items.Count)
{
listView1.TopItem = listView1.Items[listView1.TopItem.Index + 1];
}
listView1.View = View.Tile;
break;
}
}
}
}
Please help me, I been loosing my mind for weeks now.
EDIT: The switch within the function above does go off, I have debugged it; therefore, it is not the invoking that is the problem...
I don't think you can do it directly.
The way to get rid of the ScrollBars is indeed to set Scrollable = false;.
But that means what it says: Now the ListView will not scroll.
Here is a common workaround for many scrolling issues:
Place the Listview in a Panel and make it as large as needed to show all Items.
Then in order to scroll simply move the LV up and down:
private void prepare_Click_1(object sender, EventArgs e)
{
// we sit inside a Panel
listView1.Parent = panel1;
// initially they have the same size
listView1.Size = panel1.Size;
listView1.Location = Point.Empty;
// a few test items
for (int i = 0; i < 100; i++)
listView1.Items.Add("Item " + i);
// now grow the height to display all items:
int cols = listView1.Width / listView1.TileSize.Width;
listView1.Height = (listView1.Items.Count / cols) * listView1.TileSize.Height;
}
// moving the LV up looks like scrolling down..
private void scrollDown_Click(object sender, EventArgs e)
{
listView1.Top -= listView1.TileSize.Height;
if (listView1.Bottom < panel1.Height)
listView1.Top = -listView1.Height + panel1.Height;
}
// moving the LV down looks like scrolling up..
private void scrollUp_Click_1(object sender, EventArgs e)
{
listView1.Top += listView1.TileSize.Height;
if (listView1.Top > 0) listView1.Top = 0;
}
The code:
pbs = new PictureBox[8];
for (int i = 0; i < pbs.Length; i++)
{
pbs[i] = new PictureBox();
pbs[i].MouseEnter += Form1_MouseEnter;
pbs[i].MouseLeave += Form1_MouseLeave;
pbs[i].Size = new Size(100, 100);
pbs[i].Margin = new Padding(0, 0, 0, 60);
pbs[i].Dock = DockStyle.Top;
pbs[i].SizeMode = PictureBoxSizeMode.StretchImage;
Panel p = i < 4 ? panel1 : panel2;
p.Controls.Add(pbs[i]);
pbs[i].BringToFront();
}
I did:
pbs[i].MouseEnter +=
And when i clicked TAB it did: Form1_MouseEnter
It's not what i wanted.
I want that when i move with the mouse over each of the pictureBoxes area it will do something.
One event for all the pictureBoxes.
If i moved over pictureBox1 do something...pictureBox2 the same...
How can i do it ? I don't want to create 8 events for each pictureBox but one enter event for all.
You need simply write
pbs[i].MouseEnter += globalMouseEnterEvent;
of course you need to have a globalMouseEnterEvent somewhere in your code
public void globalMouseEnterEvent(object sender, System.EventArgs e)
{
....
}
However, another piece of information is needed when your work with event shared between numerous controls. You need to recognize the control that triggers the event. The control instance is passed using the sender parameter that you can cast to your appropriate control type, but what is needed is to give a unique identifier to your control. Like setting the Tag or Name properties when you build the control
for (int i = 0; i < pbs.Length; i++)
{
.....
pbs[i].Tag = "PB" + i.ToString()
...
}
so in the MouseEnter code you could write
public void globalMouseEnterEvent(object sender, System.EventArgs e)
{
PictureBox p = sender as PictureBox;
if(p.Tag.ToString() == "PB1")
.....
else if ......
}
dont use form1_event , copy it's code and rename it
pbs[i].MouseEnter += yourEventName
it's enough
What you're doing is absolute correct, you attach the handler to the event of each control in
turn, so that the same handler works for every PictureBox.
I guess your problem is that the method VS creates is named Form1_MouseEnter. This is completely irrelevant, what determines what a method will handle is the += operator, not its name. Just try to run your original code and it will do what you want.
It seems to be a bug in the C# editor though, as it should have named the automatically generated handler something more appropriate, but anyway, you can rename the method afterwards to reflect its true meaning.
I've tried to apply the tips from the others to your code:
pbs = new PictureBox[8];
for (int i = 0; i < pbs.Length; i++)
{
pbs[i] = new PictureBox();
pbs[i].MouseEnter += Picturebox_MouseEnter;
pbs[i].MouseLeave += PictureBox_MouseLeave;
pbs[i].Name = string.Concat("PB", i); //Added to identify each picturebox
pbs[i].Size = new Size(100, 100);
pbs[i].Margin = new Padding(0, 0, 0, 60);
pbs[i].Dock = DockStyle.Top;
pbs[i].SizeMode = PictureBoxSizeMode.StretchImage;
Panel p = i < 4 ? panel1 : panel2;
p.Controls.Add(pbs[i]);
pbs[i].BringToFront();
}
And the handlers:
private void Picturebox_MouseEnter(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
if (pb != null)
{
if (pb.Name == "PB2")
{
//Do PB2 specific task
}
//Your code when mouse enters one of the pictureboxes
//Use Name property to determine wich one, if needed
}
}
private void PictureBox_MouseLeave(object sender, EventArgs e)
{
//Your code when mouse leaves one of the pictureboxes
//Use Name property to determine wich one, if needed
}
Note : In WinForm
I facing problem in TableLayoutPanel. I adding and removing the control dynamicall using TableLauoutPanel. My problem is, if i remove the control in the middle (ie i added TextBox1, TextBox2 and TextBox3 and now i removed TextBox2) at this time other controls are not resized properlly. When the last control (ie TextBox3 ) is removed, its resizing properlly. I attached my code here. What is the problem?
///HERE tbl is the TableLayoutPanel
private void AddText_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
txt.MouseDoubleClick += new MouseEventHandler(txt_MouseDoubleClick);
txt.Multiline = true;
txt.Dock = DockStyle.Fill; NEWADD(txt);
}
private void NEWADD(Control ctrl)
{
tbl.RowCount += 1;
tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 100F / tbl.RowStyles.Count));
tbl.Controls.Add(ctrl, 0, tbl.RowStyles.Count - 1);
foreach (RowStyle row in tbl.RowStyles)
{
row.SizeType = SizeType.Percent;
row.Height = 100F / (tbl.RowStyles.Count);
}
}
private void RemoveControl(Control ctrl)
{
tbl.RowCount -= 1;
tbl.Controls.Remove(ctrl);
tbl.RowStyles.Clear();
for (int i = 0; i < tbl.Controls.Count; i++)
{
tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 100F /( tbl.RowStyles.Count +1)));
}
foreach (RowStyle row in tbl.RowStyles)
{
row.SizeType = SizeType.Percent;
row.Height = 100F / (tbl.RowStyles.Count);
}
tbl.Refresh();
}
void txt_MouseDoubleClick(object sender, MouseEventArgs e)
{
RemoveControl(sender as Control);
}
Can't test it at the moment, but it looks like your problem is the logic for removing the row that the control is in.
tbl.RowCount -= 1
is always going to remove the last row. You need to move through each control that is in a row after the the you want to remove and shift your controls up one row. Maybe something like this...
foreach(Control control in tbl.Controls)
{
if(control.Row > controlToRemove.Row)
control.Row--;
}
Since it looks like you're also trying to dynamically create a new table row for each control, you might do a little better with a FlowLayoutPanel. You should be able to style it to get a similar effect and it will be able to handle "row creation" automatically.