Control flicker while dragging in run time - c#

Stack Overflow users. In a C# VS 2010 Windows Form project I have a problem regarding control flicker when dragging a user created control around on a tab page during run time. I used the following code:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).Refresh();
isDragged = false;
}
private void createButton_PB_Click(object sender, EventArgs e)
{
int ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Text == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Name += "u";
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
lastBtnClicked = newButton;
}
SetupClickEvents(tabControl1.SelectedTab);
}
So, the problem is that I can add a button and drag it around in run time. But, when I add another Button and drag it around...after I've done that, and go back to trying to drag the first button, that button flickers and acts as if it is trying to move all over the place. Sometimes it disappears. I feel like this has something to do with the fact that the controls are inside a tab page. Perhaps I am not properly calculating the "newPoint" variable. Any ideas guys?

Ok, so I found a few fundamental flaws related to the button creation and the events that were being added at the time of creation. I made some considerable changes and the issue seems to have gone away. Following is the updated code.
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
isDragged = true;
((Control)sender).MouseMove += new
MouseEventHandler(control_MouseMove);
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).MouseMove -= control_MouseMove;
((Control)sender).Refresh();
isDragged = false;
}
private void SetupClickEvents(Control control)
{
control.Click += new EventHandler(StoreLastClick);
control.MouseDown += new MouseEventHandler(control_MouseDown);
//control.MouseMove += new MouseEventHandler(control_MouseMove);
control.MouseUp += new MouseEventHandler(control_MouseUp);
}
private void createButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
SetupClickEvents(newButton);
}
}
private void deleteButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
if (lastCtrlClicked != null)
{
string btnName = lastCtrlClicked.Name;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
}
if (ctrlExists == 1 && lastCtrlClicked != null)
{
tabControl1.SelectedTab.Controls.Remove(lastCtrlClicked);
lastCtrlClicked.Dispose();
ctrlExists = 0;
}
lastCtrlClicked = null;
}

Related

How to identify dynamically created panels in c#?

I am doing a Windows Form Application in c# in which I create a panel for every click i do in "Panel_Inside".
But my problem is when I have to select one especific panel. I do not know how can I identify each panel.
Also I'm having problems when Dragging and Dropping for the same reason.
public void Coloco_Figura(string figura, int Punto_X, int Punto_Y)
{
panel_foto = new Panel();
panel_foto.BackColor = Color.Transparent; //saco fondo
panel_foto.BackgroundImage = Image.FromFile(figura); //asigno imagen al panel
panel_foto.BackgroundImageLayout = ImageLayout.Stretch;
panel_foto.Size = new Size(45, 45);
panel_foto.Location = new Point(Punto_X - 10, Punto_Y - 10);
panel_foto.BringToFront();
panel1.SendToBack();
panel_inside.Controls.Add(panel_foto);
dame_x = Punto_X;
this.panel_foto.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDown);
this.panel_foto.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUp);
this.panel_foto.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMove);
}
here are mouse events
private void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
# region Borrar Notas
if (Estado_Borro == true)
{
foreach (Panel p in panel_inside.Controls)
{
if (p is Panel)
{
panel_inside.Controls.Remove(p);
if (panel_inside.Controls.Count == 0)
{
listanotas.Clear();
}
else
{
for (int i = 0; i < listanotas.Count; i++)
{
Notas nota = listanotas[i];
if (nota.posX == dame_x)
{
listanotas.Remove(nota);
}
}
}
}
}
Estado_Borro = false;
}
if (e.Button == MouseButtons.Left)
{
drag = true;
x = e.X;
y = e.Y;
}
}
public void MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
drag = false;
}
public void MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (drag)
{
this.panel_foto.Location = new Point(Cursor.Position.X-this.Left, Cursor.Position.Y-this.Top);
}
To identify each panel, first you must set the Name or Tag property:
panel_foto.Name = "panel_foto1";
Then in your event handler,do something like this:
Panel p=(Panel)sender;
if (p.Name == "panel_foto1")
{
//Do your staff
}

DataGrid auto scrolling when doing selection

I have a custom Datagrid, in that datagrid I change the way we select element.
I have some feature added to the selection, like :
When we select element, it's like the "Ctrl" key was press.
When we click on a selected row, the row become unselected.
When we do a multiple selection, all the row change the selectedValue for the one that the first row is going to have.
When we do a multiple selection (mouse down, move, then mouse up) with Right click it's reversing the selected value of the rows.
It's a DataGrid extension, so I am coding only in C#.
For doing that I added event handle on PreviewMouseDown and MouseUp of for the datagridrow.
private enum ButtonClicked {Left, Middle, Right};
private ButtonClicked m_oMouseButtonClicked;
private void PreviewMouseDownHandler(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
if (e.LeftButton == MouseButtonState.Pressed)
{
row.IsSelected = !row.IsSelected;
m_oMouseButtonClicked = ButtonClicked.Left;
}
else if (e.RightButton == MouseButtonState.Pressed)
{
//row.IsSelected = !row.IsSelected;
m_oMouseButtonClicked = ButtonClicked.Right;
}
row.CaptureMouse();
row.MouseMove += row_MouseMove;
e.Handled = true;
}
void row_MouseMove(object sender, MouseEventArgs e)
{
Point oPosFromThis = e.GetPosition(this);
if (oPosFromThis.Y > this.ActualHeight)
{
}
else if (oPosFromThis.Y < 0)
{
}
}
void Row_MouseUp(object sender, MouseButtonEventArgs e)
{
int nStart;
int nEnd;
DataGridRow row = sender as DataGridRow;
row.ReleaseMouseCapture();
row.MouseMove -= row_MouseMove;
int nStartRowIndex = ItemContainerGenerator.IndexFromContainer(row);
Point oPosFromRow = e.MouseDevice.GetPosition(row);
int nEndRowIndex = nStartRowIndex + (int)Math.Floor(oPosFromRow.Y / row.ActualHeight);
if (nStartRowIndex < nEndRowIndex)
{
nStart = Math.Max(nStartRowIndex, 0);
nEnd = Math.Min(nEndRowIndex, Items.Count - 1);
}
else
{
nStart = Math.Max(nEndRowIndex, 0);
nEnd = Math.Min(nStartRowIndex, Items.Count - 1);
}
for (; nStart <= nEnd; ++nStart)
{
DataGridRow oTmp = ((DataGridRow)ItemContainerGenerator.ContainerFromIndex(nStart));
if (m_oMouseButtonClicked == ButtonClicked.Left)
{
oTmp.IsSelected = row.IsSelected;
}
else if (m_oMouseButtonClicked == ButtonClicked.Right)
{
oTmp.IsSelected = !oTmp.IsSelected;
}
}
e.Handled = true;
}
I give the mouse capture to my row i clicked, to be able to catch the mouseUp even if i go outside the datagrid.
But with my code, I lost a feature that i would like to have. The auto scrolling when I do a multiple selection and i go under or upper the datagrid. I know that iI will have to add MouseMove Handler to do it, but for now i am stuck cause I don't know how to do it.
I finally found a solution by try-error attempts. I added method to get the scrollviewer element, then i am starting a Timer to execute the scroll alone.
public claa AAA
{
private enum ButtonClicked {Left, Middle, Right};
private ButtonClicked m_oMouseButtonClicked;
private DispatcherTimer m_oTimer;
private double m_nScrollOffset;
private ScrollViewer m_oScrollBar;
public IcuAlertGrid()
{
this.Initialized += IcuAlertGrid_Initialized;
this.Loaded += IcuAlertGrid_Loaded;
m_oTimer = new DispatcherTimer();
m_oTimer.Tick += m_oTimer_Tick;
m_oTimer.Interval = new TimeSpan(2500000);
}
void IcuAlertGrid_Initialized(object sender, EventArgs e)
{
setStyle0(true);
//throw new NotImplementedException();
}
void IcuAlertGrid_Loaded(object sender, RoutedEventArgs e)
{
m_oScrollBar = GetScrollViewer(this);
}
void m_oTimer_Tick(object sender, EventArgs e)
{
if (m_oScrollBar != null)
{
m_oScrollBar.ScrollToVerticalOffset(m_oScrollBar.VerticalOffset + m_nScrollOffset);
}
}
private void PreviewMouseDownHandler(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
if (e.LeftButton == MouseButtonState.Pressed)
{
row.IsSelected = !row.IsSelected;
m_oMouseButtonClicked = ButtonClicked.Left;
}
else if (e.RightButton == MouseButtonState.Pressed)
{
//row.IsSelected = !row.IsSelected;
m_oMouseButtonClicked = ButtonClicked.Right;
}
row.CaptureMouse();
row.MouseMove += row_MouseMove;
e.Handled = true;
}
private void row_MouseMove(object sender, MouseEventArgs e)
{
DataGridRow oRow = sender as DataGridRow;
Point oPosFromThis = e.GetPosition(this);
if (oPosFromThis.Y < 0)
{
m_nScrollOffset = -1.0;
m_oTimer.Start();
}
else if (this.ActualHeight < oPosFromThis.Y)
{
m_nScrollOffset = 1.0;
m_oTimer.Start();
}
else
{
m_oTimer.Stop();
}
}
private void Row_MouseUp(object sender, MouseButtonEventArgs e)
{
int nStart;
int nEnd;
m_oTimer.Stop();
DataGridRow row = sender as DataGridRow;
row.ReleaseMouseCapture();
row.MouseMove -= row_MouseMove;
int nStartRowIndex = ItemContainerGenerator.IndexFromContainer(row);
Point oPosFromRow = e.MouseDevice.GetPosition(row);
int nEndRowIndex = nStartRowIndex + (int)Math.Floor(oPosFromRow.Y / row.ActualHeight);
if (nStartRowIndex < nEndRowIndex)
{
nStart = Math.Max(nStartRowIndex, 0);
nEnd = Math.Min(nEndRowIndex, Items.Count - 1);
}
else
{
nStart = Math.Max(nEndRowIndex, 0);
nEnd = Math.Min(nStartRowIndex, Items.Count - 1);
}
for (; nStart <= nEnd; ++nStart)
{
DataGridRow oTmp = ((DataGridRow)ItemContainerGenerator.ContainerFromIndex(nStart));
if (m_oMouseButtonClicked == ButtonClicked.Left)
{
oTmp.IsSelected = row.IsSelected;
}
else if (m_oMouseButtonClicked == ButtonClicked.Right)
{
oTmp.IsSelected = !oTmp.IsSelected;
}
}
e.Handled = true;
}
private static ScrollViewer GetScrollViewer(DependencyObject p_oParent)
{
ScrollViewer child = default(ScrollViewer);
int numVisuals = VisualTreeHelper.GetChildrenCount(p_oParent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(p_oParent, i);
child = v as ScrollViewer;
if (child == null)
{
child = GetScrollViewer(v);
}
if (child != null)
{
break;
}
}
return child;
}
}

drag and drop cell from datagridview to another

I have 2 datagridviews and i want to copy cells from the datagridview1 to datagridview2 (a cell at a time).I´m able to select the cell I want and drag it to the datagridview2 but the value is not showing...
I spent most of the night looking for a solution...Probably is a simple answer or I just need to sleep , but please help....
I have the following code
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
string cellvalue=e.Data.GetData(typeof(string)) as string;
Point cursorLocation=this.PointToClient(new Point(e.X,e.Y));
System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView2.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
And the designer.cs i have
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(299, 150);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);
//
// dataGridView2
//
this.dataGridView2.AllowDrop = true;
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2});
this.dataGridView2.Location = new System.Drawing.Point(353, 141);
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.Size = new System.Drawing.Size(240, 150);
this.dataGridView2.TabIndex = 5;
this.dataGridView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragDrop);
this.dataGridView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragEnter);
//
You can use following code. I have tested it and it is working for copying cell data from one datagridview to another.
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
/* Drag & Drop */
private Rectangle dragBoxFromMouseDown;
private object valueFromMouseDown;
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
var hittestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
{
valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value;
if (valueFromMouseDown != null)
{
// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
}
else
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView2_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView2.PointToClient(new Point(e.X, e.Y));
// If the drag operation was a copy then add the row to the other control.
if (e.Effect == DragDropEffects.Copy)
{
string cellvalue = e.Data.GetData(typeof(string)) as string;
var hittest = dataGridView2.HitTest(clientPoint.X, clientPoint.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
}
You might need to change your codes to ::
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null){
//Need to put braces here CHANGE
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
string cellvalue=e.Data.GetData(typeof(string)) as string;
Point cursorLocation=this.PointToClient(new Point(e.X,e.Y));
System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView2.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1){ //CHANGE
dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
}
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
This part dataGridView1.DoDragDrop(text, DragDropEffects.Copy); of your code should be inside the braces as you are checking for a condition just prior to it, as (texts!=null)
The error is in this line:
Point cursorLocation = this.PointToClient(new Point(e.X,e.Y));
Because this. does not refer to the DataGridView, but rather to the Form.
It should be instead:
Point cursorLocation = dataGridView2.PointToClient(new Point(e.X,e.Y));

Make previous two buttons disappear when third is clicked in C#

That's my code below and it works perfectly fine, except there is one thing that doesn't work correctly. When I click two buttons to match, they should stay visible until the user clicks a third button. How can I do that? Thank you.
namespace Memorija_Seminarska
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tableLayoutPanel1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
progressBar1.Visible = false;
label2.Text = vreme.ToString();
}
public int vreme = 150;
Random random = new Random();
Button firstClicked = null;
Button secondClicked = null;
List<string> drzava = new List<string>()
{
"Македонија","Македонија", "Бугарија","Бугарија", "Србија","Србија",
"Германија","Германија", "Канада","Канада", "Шпанија","Шпанија",
"Португалија","Португалија", "Австрија","Австрија", "Данска","Данска",
"Индија","Индија", "Италија","Италија", "Англија","Англија",
"Турција","Турција", "Грција","Грција","Хрватска","Хрватска",
"Холандија","Холандија", "Русија", "Русија", "Швајцарија","Швајцарија"
};
private void startButton_Click(object sender, EventArgs e)
{
Add();
tableLayoutPanel1.Enabled = true;
label1.Visible = true;
label2.Visible = true;
progressBar1.Visible = true;
timer2.Start();
timer3.Start();
}
private void Add()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button b = control as Button;
if (b != null)
{
int randNum = random.Next(drzava.Count);
b.Text = drzava[randNum];
b.ForeColor = b.BackColor;
drzava.RemoveAt(randNum);
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
Button clickedButton = sender as Button;
if (clickedButton != null)
{
if (clickedButton.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
Win();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked.BackColor = Color.GreenYellow;
secondClicked.BackColor = Color.GreenYellow;
firstClicked = null;
secondClicked = null;
return;
}
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void timer2_Tick(object sender, EventArgs e)
{
vreme--;
label2.Text = vreme.ToString();
if (vreme == 0)
{
tableLayoutPanel1.Enabled = false;
label3.Text = "Game over!";
label3.Visible = true;
label2.Visible = false;
timer2.Stop();
timer3.Stop();
label1.Visible = false;
progressBar1.Visible = false;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
progressBar1.Value -= 1;
if (progressBar1.Value == 0)
{
timer3.Stop();
}
}
private void Win()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button button1 = control as Button;
if (button1 != null)
{
if (button1.ForeColor == button1.BackColor)
{
return;
}
}
}
label3.Text = "Браво!!!";
label3.Visible = true;
tableLayoutPanel1.Enabled = false;
timer2.Stop();
timer3.Stop();
label2.Visible = false;
progressBar1.Visible = false;
label1.Visible = false;
}
}
}
As far as I can see, your timer1_Tick handler performs the hiding automatically when it's time period expires. In case you want this hiding to happen manually when third card is clicked, you should not hide the buttons there, but should just perform a check in the beginning of button_Click:
private void button_Click(object sender, EventArgs e)
{
//two cards are open and not matching (if they matched, they would be already null)
if ( firstClicked != null && secondClicked != null )
{
//hide the buttons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
}
And delete the timer1.Start() from the end of the event handler.
im not good at understanding other people code, but as far as i look through, you do this in here:
private void button_Click(object sender, EventArgs e)
Check for null
get reference of button 1 and return
get reference of button 2
do win() method
check for equality
here you should send step 5 to front line, check for null as MZetko said, and then check for equality, so it will be third click, else if you get reference as you did, after second button filled up, the checking will also launch
i hope i were helpful

DragDrop between controls

I have a problem with DragDrop.
private void Form0_Load(object sender, EventArgs e)
{
PictureBox panel1 = new PictureBox();
PictureBox panel2 = new PictureBox();
mainPanel.Dock = DockStyle.Fill;
this.Controls.Add(mainPanel);
panel1.Location = new Point(10, 10);
panel1.Size = new System.Drawing.Size(500, 300);
panel1.BorderStyle = BorderStyle.FixedSingle;
Button b2 = new Button();
b2.Location = new Point(10, 10);
panel2.Controls.Add(b2);
panel2.Location = new Point(10, 10);
panel2.Size = new System.Drawing.Size(200, 100);
panel2.BorderStyle = BorderStyle.FixedSingle;
foreach (Control c in panel1.Controls)
{
c.MouseDown += new MouseEventHandler(control_MouseDown);
c.MouseMove += new MouseEventHandler(control_MouseMove);
c.MouseUp += new MouseEventHandler(control_MouseUp);
c.AllowDrop = true;
}
panel1.AllowDrop = true;
panel1.DragEnter += new DragEventHandler(container_DragEnter);
panel1.DragDrop += new DragEventHandler(container_DragDrop);
panel1.DragOver += new DragEventHandler(container_DragOver);
foreach (Control c in panel2.Controls)
{
c.MouseDown += new MouseEventHandler(control_MouseDown);
c.MouseMove += new MouseEventHandler(control_MouseMove);
c.MouseUp += new MouseEventHandler(control_MouseUp);
c.AllowDrop = true;
}
panel2.AllowDrop = true;
panel2.DragEnter += new DragEventHandler(container_DragEnter);
panel2.DragDrop += new DragEventHandler(container_DragDrop);
panel2.DragOver += new DragEventHandler(container_DragOver);
mainPanel.Controls.Add(panel1);
mainPanel.Controls.Add(panel2);
mainPanel.Controls.Add(pb);
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
isDragging = true;
clickOffsetX = e.X;
clickOffsetY = e.Y;
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
Control c = sender as Control;
if (isDragging == true)
{
c.Left = e.X + c.Left - clickOffsetX;
c.Top = e.Y + c.Top - clickOffsetY;
if (c.Location.X + clickOffsetX > c.Parent.Width ||
c.Location.Y + clickOffsetY > c.Parent.Height ||
c.Location.X + clickOffsetX < 0 ||
c.Location.Y + clickOffsetY < 0)
c.DoDragDrop(c, DragDropEffects.Move);
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
void container_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void container_DragEnter(object sender, DragEventArgs e)
{
//e.Effect = DragDropEffects.Move;
//if (e.Data.GetDataPresent(typeof(Bitmap)))
//{
// e.Effect = DragDropEffects.Copy;
//}
//else
//{
// e.Effect = DragDropEffects.None;
//}
}
private void container_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
PictureBox p = sender as PictureBox;
mycontrol = c;
isDragging = false;
if (c != null)
{
c.Location = p.PointToClient(new Point(e.X, e.Y));
p.Controls.Add(c);
}
}
This is a working example. But I can't do drop Controls from parent to child control. What is a magic? How to drop control to another control (from panel1 to panel2 in my example).
There are some answers here in SO, which may help you:
See this Move controls when Drag and drop on panel in C#
this is a complete example on how to host the Form Designer:
Tailor Your Application by Building a Custom Forms Designer with .NET
Check this one also for simple lable drag drop:
Basic drag and drop in WinForms

Categories