C# Using compare to change the visability of objects

Afternoon Experts!

Im making a little application to store various passwords in.

I have 'form' screen where the suer fills out the data and can add fields to the form they deem nessessary, once they fill in the data the data is added to a List<>.

I then have a 'summary' page with a listbox and textboxes which display the data. Currently if  an item from the listbox  is selected it will compare the data in the List<> and show the data in each of the relavent textboxes.

However I also want it to Show ONLY  textboxes that should have data in them from the List<> when the items are selected from the Listbox.....basically avoiding blank textboxes.

Ive first of all added each textbox as an object to my class (cut the full list  of items/objecst down  for forum sake)...

      [Serializable]
        public class PassDetails
        {

            public string RefName;
            public string Storedusername;
            public string StoredPassword;

            public object tbSecretNumfield1;
            public object tbSecretNumfield2;
         
        }

 List<PassDetails> passwordlist = new List<PassDetails>();

Open in new window



Ive then Assigned a name to each of the objects in the class when the user submits the form:-

     
    String thisref = tbNamefield.Text;
                    String thisusername = tbUsernamefield.Text;
                   String thisPW = tbPasswordfield.Text;

                Object thisNumBox01TB = tbSecretNumfield1;
                Object thisNumBox02TB = tbSecretNumfield2;

 if (thisref !="")
            {
                PassDetails p = new PassDetails();
                
                p.RefName = thisref;
                p.Storedusername = thisusername;
                p.StoredPassword= thisPW;

                p.tbSecretNumfield1 = thisNumBox01TB;
                p.tbSecretNumfield2= thisNumBox02TB;
          
                passwordlist.Add(p);
               
                // this updates the listbox.
                UpdatelbPassnames();
            }

Open in new window


Then I have the Listbox Selection Change function

private void lbPassnames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
              
              if (lbPassnames.SelectedIndex != -1)
              {
                  String compareName = lbPassnames.Items[lbPassnames.SelectedIndex].ToString();
         
                  foreach (PassDetails p in passwordlist)
                  {
                      if (p.RefName == compareName)
                      {
                                 tbName.Text = p.RefName;
                          tbUsername.Text = p.Storedusername;
                          tbPassword.Text = p.StoredPassword;
                          tbSecretnum1.Text = p.SecretNumber01;
                          tbSecretnum2.Text = p.SecretNumber02; 
                        }

                  }
              }

Open in new window


Oviously this just compares strings, im a bit stuck/ confused on how to compare objects....if that is even possible?

Just want my list to only show textboxes with data and collapse those that don't when an item is selected from the list.

Im using Net 3.5 and WPF in VS 2010

Many Thanks,

John



Bronco78thAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
At line #19, you added the actual instance of PassDetails to the ListBox:

    passwordlist.Add(p);

Thus you don't need to go searching for a match in passwordlist, as you already have a reference to it!
private void lbPassnames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
              
              if (lbPassnames.SelectedIndex != -1)
              {
                  PassDetails p = (PassDetails)lbPassnames.Items[lbPassnames.SelectedIndex];
         
                  // ... now do something with "p" ...
              }

        }

Open in new window


To answer your question, though, to compare objects you override the Equals() function:
http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx
Bronco78thAuthor Commented:
Cheers,

will have a look at that link.

Got an error on your code adjustment though...

"Invalide CastExceptionWasUnhandled Unable to cast object of type 'System.String' to type 'PassDetails'."

Regards,

John
Bronco78thAuthor Commented:
Thanks jkr.

Sorry about that,I did notice that id hit the C++ zone by accident but had already submitted and couldn't find a way to change it.

Regards,

John
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hmm...looking at line #19 again, that was actually adding it to the List (not the ListBox).  =\

Are you setting the data source of the ListBox to your List?...or adding individual strings?

If you add the instance itself to the ListBox you can control the way it is displayed by overriding the ToString() property in your class.
Bronco78thAuthor Commented:
Hey again Idle mind.

 I came up with a solution to my OP, using a reference from one of my previous problems.

Solution here:-


private void lbPassnames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
              
              if (lbPassnames.SelectedIndex != -1)
              {
                  String compareName = lbPassnames.Items[lbPassnames.SelectedIndex].ToString();
         
                  foreach (PassDetails p in passwordlist)
                  {
                      if (p.RefName == compareName)
                      {
                                 tbName.Text = p.RefName;
                          tbUsername.Text = p.Storedusername;
                          tbPassword.Text = p.StoredPassword;
                          tbSecretnum1.Text = p.SecretNumber01;
                          tbSecretnum2.Text = p.SecretNumber02; 
                        }

                      List<TextBox> tbs = new List<TextBox>();
                      tbs.AddRange(new TextBox[] {
                         tbSecretnum1,  
                         tbSecretnum2,  
                         tbSecretword1,
                         tbSecretword2,
                         tbsecretAnswer1,
                         tbsecretQuest1,
                         tbsecretQuest2,
                         tbsecretAnswer2,
                         tbsecretQuest3,
                         tbsecretAnswer3,
                         tbsecretQuest4,
                         tbsecretAnswer4,});

                         foreach(TextBox tb in tbs)
                         {
                             if (tb.Text.Trim().Length != 0)
                             {
                                 tb.Visibility = Visibility.Visible;
                             }
                             else
                             {
                                 tb.Visibility = Visibility.Collapsed;
                             }

                         }

                  }
              }

Open in new window



Yeah, Line 19 adds the data to the List<>.


To Make the data show up in the listbox I use  another method as follows.

        //UPDATING FUNCTION FOR LISTBOX 
          
private void UpdatelbPassnames()
          {
              lbPassnames.Items.Clear();

              if (passwordlist.Count == 0)
              {
                  btnEditpassword.IsEnabled = true;
                  btnDelete.IsEnabled = true;

              }
              else
              {
                  foreach (PassDetails p in passwordlist)
                  {
                      lbPassnames.Items.Add(p.RefName);

                      
                  }
              }
 }

Open in new window


I can call this method whenever I need it
UpdatelbPassnames();

Open in new window



Many Thanks for tyour time Idle mind.

John






Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Right...so you can add the instance to the ListBox instead of just the name:

    lbPassnames.Items.Add(p); // add "p" instead of "p.RefName"

Now, in your class, override the ToString() method to make the name the default value:

    [Serializable]
    public class PassDetails
    {
        public string RefName;

        public override string ToString()
        {
            return this.RefName;
        }
    }

Thus when you access the items, you're actually getting the instance back already:

    PassDetails pd = (PassDetails)lbPassnames.Items[lbPassnames.SelectedIndex];

Then you don't have to loop thru everything looking for a match.
Bronco78thAuthor Commented:
As said previously I came up with a completily different solution to the initial problem..using the solution to one of my previous questions.

Idle mind gets points for pointing out abit of optermisation which ive just got going though!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.