Managing two combobox of which only one must have a value - c#

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

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

How to make 2 pictureboxes invisible if 2 images match

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

How to disable a combobox on clicking on another combox list data in C sharp?

I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.

c# error cant understand what i should do to solve it

hi i have this code(and i get the ERROR that i must declare a body because it is not not marked abstract,extern or partial...by the way i couldnt get it to post it here but right over the public partial class Form1 : Form i also have written bool play1 = true;bool play2 = false; int x=1;int o=10; )when i click on it it goes up to that area that i marked with fat letters
could someone please show me what is wrong and how to solve it step by step
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
værdier();
int[] status = new int[9];
zeihne();
}
private string[] status;
private void value()
{
int[] status = new int[9];
zeihne();
}
private void zeihne()
{
button1.Text = status[0];
button2.Text = status[1];
button3.Text = status[2];
button4.Text = status[3];
button5.Text = status[4];
button6.Text = status[5];
button7.Text = status[6];
button8.Text = status[7];
button9.Text = status[8];
}
// private void label1_Click(object sender, EventArgs e)
{
}
**private void Form1_Load(object sender, EventArgs e);**
private void button1_Click(object sender, EventArgs e)
{
if (play1 == true)
{
play1 = true;
button1.Text = "X";
play1 = false;
}
else
{
play2 = true;
button1.Text = "O";
play2 = false;
play1 = true;
}
You haven't defined the method body for
private void Form1_Load(object sender, EventArgs e);
That's an abstract method and they are only allowed on abstract classes
Something like this would do the trick
private void Form1_Load(object sender, EventArgs e)
{
}
Or you can just get rid of the method if you don't plan to have any code on it.
EDIT
Something else that looks weird on your code is
// private void label1_Click(object sender, EventArgs e)
{
}
What are you trying to do? Two options:
// Option1: All the method commented
*/private void label1_Click(object sender, EventArgs e)
{
}*/
// Option2: Nothing commented
private void label1_Click(object sender, EventArgs e)
{
}
Hello Hwoarang H i tryed your code in my form and its working fine. You have to just follow step.
You have difine your method like this
private void Form1_Load(object sender, EventArgs e);
This is not right way to define method like this.You must declare like bellow
private void Form1_Load(object sender, EventArgs e)
{ }
Now remove your lable click event and brackets
// private void label1_Click(object sender, EventArgs e)
//{
//}
Now you got warning like x is issigned to but never used, o is assigned to but never used and status is assigned to but never used.No warry about that bcos you assign x and o as integer and used as string so this warning is come,i dont know whats your logic but if you want to remove this warning than use this int x,and o.Also play2 is never used so put condition like bellow
if (play1 == true)
{
btnSendNotification.Text = "x";
play1 = false;
}
else if(play2==true)
{
btnSendNotification.Text = "o";
play2 = false;
play1 = true;
}
Hope this is help you if not get solution than comment me.

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