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)
{
}
}
}
Related
I build a method that hold BackColor for a panel, if i call the method in a new method the color will not show
this is my code
''''''''''''''''''''''''''''''''''''''''''''''''''''
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace WindowsFormsApplication24
{
class theme
{
public static void dark()
{
Form1 f = new Form1();
Color c= f.panel1.BackColor = Color.Black;
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
theme.dark();
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
The new Form1() you instantiated is an instance of the Form1 class, but many Form1 instances can exist simultaneously. You want to pass the instance of Form1 you are trying to change. Try:
private void Form1_Load(object sender, EventArgs e)
{
theme.dark(this);
}
And:
public static void dark(Form1 f)
{
f.panel1.BackColor = Color.Black;
}
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 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;
}
}
}
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 2 forms with a main window and a second window. The main window (Form1) shall get text from the second window (Form2)
My second window (Form2) can write text from form 2 to form 1. In the class I can choose color for my text but my problem is that when I push the button who shall send the text it just comes the text without the color I choose so its just black text in Form1 when I send example yellow.
I'm not a C# expert since I'm pretty new at this. I'm sure its a pretty simple problem to fix but for me its not so easy.
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.Windows.Forms;
namespace tester
{
public partial class Form1 : Form
{
public string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 NewForm2 = new Form2(this);
NewForm2.Show();
}
internal void populate()
{
richTextBox1.Text = text;
}
}
Form2.cs:
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;
namespace Tester
{
public partial class Form2 : Form
{
Form1 texting;
public Form2(Form1 iForm)
{
texting = iForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
texting.text = richTextBox1.Text;
texting.populate();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( MyColorDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.ForeColor = MyColorDialog.Color;
}
}
}
Just as you are making public string text in your Form1, you could make a public Color rictTextBoxColor property. Then setting it as well, and referring to it in your populate method