In my program I need to copy a Label in Windows Forms, and I implemented it the following way:
public partial class Resizer : Form
{
public Resizer()
{
InitializeComponent();
}
private Control toChange = new Control();
private Control selected = new Control();
private bool readyToMove = false;
private Point offset;
private void pictureBox4_Click(object sender, EventArgs e)
{
selected.Location = ((Control)sender).Location;
selected.Size = ((Control)sender).Size;
}
private void backClick(object sender, EventArgs e)
{
selected = null;
}
private void Resizer_Load(object sender, EventArgs e)
{
pictureBox4.MouseDown += new MouseEventHandler(this.dynamicMouseDown);
pictureBox4.MouseMove += new MouseEventHandler(this.dynamicMouseMove);
pictureBox4.MouseUp += new MouseEventHandler(this.dynamicMouseUp);
}
private void dynamicMouseDown(object sender, MouseEventArgs e)
{
toChange = (Control)sender;
selected.Location = ((Control)sender).Location;
selected.Size = ((Control)sender).Size;
readyToMove = true;
offset = new Point(PointToClient(MousePosition).X - selected.Location.X,
PointToClient(MousePosition).Y - selected.Location.Y);
}
private void dynamicMouseMove(object sender, MouseEventArgs e)
{
if (readyToMove)
{
selected.Location = new Point(PointToClient(MousePosition).X-offset.X,
PointToClient(MousePosition).Y- offset.Y);
this.Update();
}
}
private void dynamicMouseUp(object sender, MouseEventArgs e)
{
toChange.Location = selected.Location;
readyToMove = false;
}
private void button1_Click(object sender, EventArgs e)
{
PictureBox pBox = (PictureBox)CloneObject(this.pictureBox4);
this.Controls.Add(pBox);
//pBox.MouseDown += new MouseEventHandler(this.dynamicMouseDown);
//pBox.MouseMove += new MouseEventhandler(this.dynamicMouseMove);
//pBox.MouseUp += new MouseEventHandler(this.dynamicMouseUp);
//these eventhandlers did not help...
}
private object cloneObject(object o)
{
Type t = o.GetType();
System.Reflection.PropertyInfo[] properties = t.GetProperties();
Object p = t.InvokeMember("",
System.Reflection.BindingFlags.CreateInstance,
null,
o,
null);
foreach (PropertyInfo pi in properties)
{
if (pi.CanWrite)
pi.SetValue(p, pi.GetValue(o, null), null);
}
return p;
}
}
old problem:
The problem is that as soon as I click on the labelOrigin, the Window freezes and I can't close the form anymore. Why is that and how can I fix it?
Now:
As you can see, this implements a drag&drop where by pressing a button, you can duplicate a form..
the problem is, that the new pBox is not movable and does lead to graphical errors..
this code should work on a simple form after adding a button1 and a pictureBox4
Related
I am doing a memory matching game in c#
when the user matches 2 images i want them to disappear or make them invisible
i am still new to coding and this is what i did so far but the images wont be invisible
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
}
As others have stated you'll need to store all of the Resources just once. Here is a possible example:
class MyForm
{
private Dictionary<String, Image> images = new Dictionary<String, Image>();
public void Init()
{
images["apple"] = Properties.Resources.apple;
}
public void Dispose()
{
foreach(var item in myDictionary.Values)
{
item.Dispose();
}
}
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = images["apple"];
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = images["apple"];
}
}
When you set apple1.Image = Properties.Resources.apple; Note that a copy of the value (Image) of the Properties.Resources.apple is set to apple1.Image, same thing happens when you assign to apple2.Image, so although visually they seem to have the same image, but they are pointing to different images (in memory).
You may do one of following:
1-Set your images to a dictionary and then load apple1.Image and apple2.Image from it:
Dictionary<string, Image> Images = new Dictionary<string, Image>();
Images.Add("apple", Properties.Resources.apple);
Image apple = Properties.Resources.apple;
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Images["apple"];
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Images["apple"];
}
2-Just use another property to compare equality:
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
apple1.Tag = "apple";
if ((string)apple1.Tag==(string)apple2.Tag)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
apple2.Tag = "apple";
}
I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}
Here is my code:
public Form1()
{
InitializeComponent();
pBox1.AllowDrop = true;
}
private void pBox1_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pBox1.Image = bmp;
pBox1.Size = new Size(100, 100);
}
private void pBox2_MouseDown(object sender, MouseEventArgs e)
{
if (DoDragDrop(pBox2.Image, DragDropEffects.Move) == DragDropEffects.Move)
{
pBox2.Image = null;
}
}
pBox1 is the pictureBox that I would like to drag into, and pBox2 is the pictureBox I would like to drag from. The error I get is an object reference not set to instance of object error, on the line "if(DoDragDrop...." within the MouseDown method.
If what you have listed is the entire code listing, you are never setting pBox2.Image to an Image, which would cause the exception. May want to add:
private void pBox2_MouseDown(object sender, MouseEventArgs e)
{
if(pBox2.Image != null)
{
if (DoDragDrop(pBox2.Image, DragDropEffects.Move) == DragDropEffects.Move)
{
pBox2.Image = null;
}
}
}
To initialize the pBox2 to an image of some sort...
public Form1()
{
InitializeComponent();
pBox1.AllowDrop = true;
pBox2.Image = Image.FromFile(#"YourFilePath");
}
Edit
Just a note, this gets rid of your exception, but still does not implement drag drop properly. I am playing with it and will get back with you if I find a proper solution.
Edit
Possible duplicate to the following link:
Stack Overflow Thread
I got it working using the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
pictureBox2.Image = Image.FromFile(#"C:\TitleBar.jpg");
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox2.Image != null)
{
pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Move);
pictureBox2.Image = null;
}
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
}
When I am making a lot of buttons, is this the best way, or is there a better way? This code feels sort of clunky.
Button button = new Button();
button.MouseEnter += Button_MouseEnter;
button.MouseLeave += Button_MouseLeave;
Button button2 = new Button();
button2.MouseEnter += Button2_MouseEnter;
button2.MouseLeave += Button2_MouseLeave;
void Button_MouseEnter(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("buttonHover");
}
void Button_MouseLeave(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button");
}
void Button2_MouseEnter(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button2Hover");
}
void Button2_MouseLeave(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button2");
}
I think there isn't better way. I would create a custom control with properties "button" and "buttonHover".
Something like this (not tested yet):
public class MyBytton : Button
{
public Image MainImage { get; set; }
public Image HoverImage { get; set; }
protected override void OnMouseEnter (EventArgs e)
{
if (HoverImage != null)
{
this.BackgroundImage = HoverImage;
}
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (MainImage != null)
{
this.BackgroundImage = MainImage;
}
base.OnMouseLeave(e);
}
}
Use:
MyBytton my = new MyBytton();
my.Location = new Point(10, 10); ;
my.Name = "button1";
my.Size = new System.Drawing.Size(141, 61);
my.TabIndex = 0;
my.Text = "test";
my.UseVisualStyleBackColor = true;
my.BackgroundImage = Image.FromFile("img1.jpg");
my.MainImage = Image.FromFile("img1.jpg");
my.HoverImage = Image.FromFile("img2.jpg");
I have this simple code in my DevExpress LookUp control (should be identical with a normal combobox)
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
lookUpUsers.EditValue = null;
}
The problem is that when I select a value in lookUpUsers, is resets the other lookup which then resets lookUpUsers. So when I pick a value, both combobox become null. What I want is that when you pick a value in combobox 1, combobox 2 resets its value.
How about this:
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if(lookUpUsers.EditValue != null)
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if(lookUpRolesPréÉdit.EditValue != null)
lookUpUsers.EditValue = null;
}
There might be an easier way than this, as my knowledge of C# is limited (especially their libraries like you are using them here). Nevertheless, this is an answer that uses no magic provided by libraries:
private bool localEdit = false;
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpRolesPréÉdit.EditValue = null;
localEdit = false;
}
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpUsers.EditValue = null;
localEdit = false;
}
}
Here's a solution I've come up with
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpRolesPréÉdit.EditValue = null;
}
isEditFinished = false;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpUsers.EditValue = null;
}
isEditFinished = false;
}