Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I should perform one task that there are total 6 combobox(3 camera id and 3 camera type) and 1 button.when i clicked button then below points occur:
1>when 3 id and 3 type are same the display message duplicate are not allowed.
2>when 2 out of 3 camera id are same and their camera type are diffrent thendisplay message save name.

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            if((cmbcam1Id.SelectedItem.ToString()==cmbcamera2Id.SelectedItem.ToString())&&(cam1typ.SelectedItem.ToString()==cam2typ.SelectedItem.ToString()))
            {
                if((cmbcamera2Id.SelectedItem.ToString()==cmbcamera3Id.SelectedItem.ToString())&&(cam2typ.SelectedItem.ToString()==cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if((cmbcamera2Id.SelectedItem.ToString()==cmbcamera3Id.SelectedItem.ToString())&&(cam2typ.SelectedItem.ToString()!=cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if ((cmbcam1Id.SelectedItem.ToString() != cmbcamera3Id.SelectedItem.ToString()) && (cam1typ.SelectedItem.ToString() == cam3typ.SelectedItem.ToString()))
                  {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }

            }
            else if((cmbcamera2Id.SelectedItem.ToString()==cmbcamera3Id.SelectedItem.ToString()) && (cam2typ.SelectedItem.ToString()==cam3typ.SelectedItem.ToString()))
            {
                if((cmbcam1Id.SelectedItem.ToString()==cmbcamera3Id.SelectedItem.ToString())&& (cam1typ.SelectedItem.ToString() != cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if((cmbcam1Id.SelectedItem.ToString()!=cmbcamera3Id.SelectedItem.ToString())&&(cam1typ.SelectedItem.ToString()==cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            else if ((cmbcam1Id.SelectedItem.ToString() == cmbcamera3Id.SelectedItem.ToString()) && (cam1typ.SelectedItem.ToString() == cam3typ.SelectedItem.ToString()))
            {
                if ((cmbcamera2Id.SelectedItem.ToString() == cmbcamera3Id.SelectedItem.ToString()) && (cam2typ.SelectedItem.ToString() != cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if((cmbcamera2Id.SelectedItem.ToString()!=cmbcamera3Id.SelectedItem.ToString())&&(cam2typ.SelectedItem.ToString()==cam3typ.SelectedItem.ToString()))
                {
                    MessageBox.Show("Dublicate not allow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Save Name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
         //   else if((cmbcam1Id.SelectedItem.ToString()==cmbcamera2Id.SelectedItem.ToString())&&(cam1typ.SelectedItem.ToString()==cam2typ.SelectedItem.ToString()))
           // {
               
           // }
        }
    }
Posted
Updated 16-Mar-21 2:50am

First off, why are you converting everything to a string at all, let alone multiple times?
If you can only compare items via strings - which is a bad idea - then convert them once and then compare those:
C#
string id1 = cmbcam1Id.SelectedItem.ToString();
string id2 = cmbcamera2Id.SelectedItem.ToString();
string id3 = cmbcamera3Id.SelectedItem.ToString();
string type1 = cmbcamera1Id.SelectedItem.ToString();
string type2 = cmbcamera2Id.SelectedItem.ToString();
string type3 = cmbcamera2Id.SelectedItem.ToString();
Your existing code them becomes a lot more readable:
C#
if (id1 == id2 && type1 == type2)
   {


I'd also suggest that you change the way you do it: a single "is it duplicated?" test is a lot easier to work with :
C#
if ((id1 == id2 && type1 == type2) ||
    (id1 == id3 && type1 == type3) ||
    (id2 == id3 && type2 == type3))
   {
   MessageBox.Show("Duplicate not allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
else
   {
   ...
   }

And do yourself a favour: use the same name format throughout ... cmbcam1Id , cmbcamera2Id, and cmbcamera3Id looks like it was written by a student ...
 
Share this answer
 
Comments
BillWoodruff 16-Mar-21 19:33pm    
+5 kudos for ... cmbcam1Id , cmbcamera2Id ... viva Hungarian notation !
I'm not sure i understand you well, but...
You should think about cameras as objects rather than their string representation...

Take a deep look at below example:
C#
void Main()
{
	//create a list of cameras
	List<Camera> myCameras = new List<Camera>()
	{
		new Camera("Camera#1", CamType.na),
		new Camera("Camera#2", CamType.usb), 
		new Camera("Camera#3", CamType.na)
	};
	
	for(int i = 0; i<myCameras.Count-1; i++)
	{
		Camera c1 = myCameras[i];
		for(int j = i+1; j<myCameras.Count; j++)
		{
			Camera c2 = myCameras[j];
			Console.WriteLine($"Does '{c1.CamName}' type is equal to '{c2.CamName}'? Answer: {c1.Equals(c2)}");
		}
	}

}

//Camera.cs class
public enum CamType
{
	usb = 0,
	lan = 1,
	na = 4
}

public class Camera: IEquatable<Camera>
{
	private string camName = string.Empty;
	private CamType camType = CamType.na;
	
	public Camera(string _name, CamType _type)
	{
		camName = _name;
		camType = _type;
	}
	
	public CamType CamType
	{
		get => camType;
		set => camType = value;
	}
	
	public string CamName
	{
		get => camName;
		set => camName = value;
	}
	
	public override bool Equals(object obj)
	{
		return Equals(obj as Camera);
	}

	public bool Equals(Camera other)
	{
		return other!= null && this.GetHashCode()==other.GetHashCode();
	}
	
	public override int GetHashCode()
	{
		return (int)this.CamType;
	}
}


Result:
Does 'Camera#1' type is equal to 'Camera#2'? Answer: False
Does 'Camera#1' type is equal to 'Camera#3'? Answer: True
Does 'Camera#2' type is equal to 'Camera#3'? Answer: False



For further details, please see: Generate C# Equals and GetHashCode Method Overrides - Visual Studio | Microsoft Docs[^]
 
Share this answer
 
Comments
BillWoodruff 16-Mar-21 19:34pm    
+5
Maciej Los 17-Mar-21 1:06am    
Thank you, Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900