I am building a simple drag and drop game using windows forms. Its like a jigsaw puzzle. I want to drag pictures from one form to another to complete the puzzle. Is there a way to do this with a PictureBox. It seems to be the only property without the AllowDrop function. Source code below.
I want to drag images from the form on the left to the form on the right.
I was following this tutorial but I cannot get my head around it.
C# Drag and Drop from one Picture box into Another
Form Right
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DragDropGame
{
public partial class ChildFormTwo : Form
{
public ChildFormTwo()
{
InitializeComponent();
pictureBox3.DragEnter += pictureBox3_DragEnter;
pictureBox3.DragDrop += pictureBox3_DragDrop;
}
private void pictureBox3_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
private void pictureBox3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
}
}
Form Left
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DragDropGame
{
public partial class ChildFormTwo : Form
{
public ChildFormTwo()
{
InitializeComponent();
pictureBox3.DragEnter += pictureBox3_DragEnter;
pictureBox3.DragDrop += pictureBox3_DragDrop;
}
private void pictureBox3_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
private void pictureBox3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
}
}
Related
i have typed like 2 questions based on 1 thing, Making a button follow the cursor, But anyways, Is it possible to make the button right in the center of the cursor instead of being
in the corner left of the cursor? If yes, then can you show me the code?
What i don't want:
What i want:
And here is all the code.
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
button1.Location = Cursor.Position;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
as title, i have a button that load an image in a picturebox, then when i click on it i want it to display the coordinates in two textbox, only proble...it display the coordinates when i click outside of the picturebox and not when i click in.
i already tried the solution in this site but it doesn't seems to work so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
private void BtnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "All jpg files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
}
}
}
}
what I'm trying to do is to change the text of the "last" lost focussed TxtBox by using a button. I know about the LostFocus method, but what are the actual paramaters that we have to give to that fuction?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.ActiveControl = textBox2;
}
private Control _focusedControl;
private void button1_Click(object sender, EventArgs e)
{
SubmitData();
}
private void SubmitData()
{
if (_focusedControl != null)
{
_focusedControl.Text = "hi";
}
}
private void TextBox2_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("Got focus.");
_focusedControl = (Control)sender;
}
}
}
So, what is happening is, no messagebox is popping and the _focusedCOntrol varable doesn't contain the last focus.
I used openTK control to draw something,but it wass always displaying a white background. WHY?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
bool loaded = false;
public Form1()
{
InitializeComponent();
}
private void glControl1_Load(object sender, EventArgs e)
{
loaded = true;
GL.ClearColor(Color.SkyBlue); // Yey! .NET Colors can be used directly!
}
private void glControl1_Resize(object sender, EventArgs e)
{
if (!loaded)
return;
}
private void glControl1_Paint(object sender, PaintEventArgs e)
{
if (!loaded) // Play nice
return;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
glControl1.SwapBuffers();
}
}
}
Simply, u forgot to add call of glControl1_Paint,
goto properties of that control, open events, on the top inside Appearance add a call.
While using GLControl instead of GameWindow, you should call glControl1.Invalidate() every time you need to display changes.
I have created class in which i have defined methods which i need to call in forms .I need to call this methods from class Test in many forms.Below is my code how i try, but without success.I dont see here where i'm wrong.
//class Test.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace Restaurant
{
public class Test : Form
{
public void MouseMove(object sender, MouseEventArgs e)
{
TextBox txt = sender as TextBox;
foreach (TextBox text in this.Controls.OfType<TextBox>())
{
if (e.Button == MouseButtons.Left)
{
if (txt.Name == text.Name)
{
txt.Left = e.X + txt.Left - MouseDownLocation.X;
txt.Top = e.Y + txt.Top - MouseDownLocation.Y;
}
}
}
}
public void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
}
}
// form in which i need to call methods from Test class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace Restaurant
{
public partial class Form1 : Test
{
public Form1()
{
InitializeComponent();
}
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
Test b = new Test();
b.MouseMove1(sender,e);
}
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
Test b = new Test();
b.MouseDown(sender,e);
}
}
}
Since Form1 inherits from Test, you should be able to change Form1 as follows:
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
MouseMove1(sender,e);
}
This way you are calling the method on the form you are on, rather than a new form that you are instantiating and acting against in your code sample.