How to handle lstbox values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vagos
    New Member
    • Apr 2011
    • 7

    How to handle lstbox values

    Hello C# Programmers, here is my question and problem. I am practicing my C# skills, and designed a simple GUI app for a library. Well, I have the class Member, and a button Search, user types the member code in a textbox, my program gets the value and finds the member in the list of "signed" members, else throws a fancy message box. Everything is fine, my problem is, a member does a Loan from library clicking the New Loan button, and within the code :
    Code:
     
    public MainWindow()
    {
    lstLoans.ItemsSource = loans;
    }
    .loans is not in the Member class, it is a field of the class MainWindow, Member class has it's own loans private field and Loans property that gets the loans returned.
    My problem is, if I type another member code as "user" of the program and press Search, it gets me the new member.Name,..e tc , but I have in my mind that each Member is a different instance. But when i press Search button and I Change Member, current loans from the previous and all members are visible to lstLoans,
    I want somehow, clearing the list when I get another member and also to show only this members Loans to the lstLoans, and not all loans done.
    If you need any code inform me, or just reply ideas.

    Thanks in advance.
  • Vagos
    New Member
    • Apr 2011
    • 7

    #2
    Hello again, I am replying in order to post a part of my code, so maybe you can understand what i ask for.
    Here is a part of the MainWindow code:
    Code:
    public partial class MainWindow : Window
        {
            List<Member> members;
            Member member;
            ObservableCollection<Loan> loans;
    
            public MainWindow()
            {
                InitializeComponent();
                members = GetMembers();
    
                //member = GetMember(textBox1.Text); 
                loans = new ObservableCollection<Loan>();
                List<Title> titles = GetAllTitles();
                
                //DataContext = member;
                lstTitles.ItemsSource = titles;
                lstLoans.ItemsSource = loans;
            }
    Here is a delete loan button :
    Code:
    private void btnDeleteLoan_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    Loan selectedLoan = (Loan)lstLoans.SelectedItem;
                    
                    if (selectedLoan == null)
                        throw new NoMoreTitlesLoanedException(member.Name);
                    
                    selectedLoan.Return();
                    member.RemoveLoan(selectedLoan);
                    //selectedLoan.Member.Loans.Remove(selectedLoan);
                    loans.Remove(selectedLoan);
                    //lstLoans.ItemsSource = loans;
                }
                catch (NoMoreTitlesLoanedException nml)
                {
                    MessageBox.Show(nml.ToString());
                }
            }
    Here is the code for search button :
    Code:
            /// <summary>
            /// Enter a code and press Search to find the member
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSearch_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    Member foundMember = null;
                    foundMember = GetMember(textBox1.Text.ToUpper());
                    
                    if (foundMember == null)
                        throw new MemberNotFoundException(textBox1.Text); 
    
                    member = foundMember;
                    DataContext = member;
                }
                catch (MemberNotFoundException mnf)
                {
                    MessageBox.Show(mnf.ToString());
                }
            }
    Now, what I want is, when the member code changes, by the search button I guess, the lstLoans box to show the specific member loans only, and not all loans done so far.

    Thanks in advance.

    Comment

    • bvrwoo
      New Member
      • Aug 2011
      • 16

      #3
      If you only want the the 1st showing up then you have to clear the list and set only the item as the source if you are displaying only one, else you will see them all inserted into the list hence the name data source. You have the observablecolle ction right but when you bind data from a list, the whole list is inserted.

      Comment

      Working...