How to make 2 pictureboxes invisible if 2 images match - c#

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";
}

Related

Xamarin - Morse code app usuing the flaslight

I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}

ArgumentOutOfRangeException on an object listview when hitting tab

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;
}
}

Cloning Label leads to freezing C#

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

Managing two combobox of which only one must have a value

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;
}

C# How do I use a value from one function in another?

How can I use a value from the reading function in the button1_Click function?
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
string[] files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
...
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (string file in files_in_folder) // How do I access files_in_folder?
{
ListViewItem li = new ListViewItem(file);
}
}
You need to store it somehow, for example as a private member:
string some_value = null;
public void reading(object sender, EventArgs e)
{
some_value = "Foobar";
}
private void button1_Click(object sender, EventArgs e)
{
if (some_value != null)
{
// ...
}
}
// Make it a member variable
private string[] mFilesInFolder = null;
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
mFilesInFolder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
}
private void button1_Click(object sender, EventArgs e)
{
DoFileInFolderOperation();
}
private void DoFilesInFolderOperation()
{
if(mFilesInFolder != null)
{
foreach (string file in mFilesInFolder)
{
ListViewItem li = new ListViewItem(file);
}
}
}

Categories

Resources