I have a behavior for my autosuggestbox where I have to order all the suggested list items by ascending order, and this behavior will is being applied to 1 common style of AutoSuggestBox across whole app. When I try ordering simply with the object itself it works fine as the items are just a list of strings. But when the items are a list of objects, and I want to sort with 1 specific property, then it is not working for me. I am using DisplayMemberPath to tell it which property it should look for. Below is the code I tried with :
Behavior
public class AutoSuggestSortBehavior : IBehavior
{
public void Attach(DependencyObject associatedObject) => ((AutoSuggestBox) associatedObject).TextChanged += AutoSuggestSortBehavior_TextChanged;
private void AutoSuggestSortBehavior_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var autoSuggestBox = sender;
if(autoSuggestBox?.Items?.Count > 0 && args.Reason == AutoSuggestionBoxTextChangeReason.UserInput && !string.IsNullOrWhiteSpace(sender.Text))
{
if (!string.IsNullOrWhiteSpace(autoSuggestBox.DisplayMemberPath))
{
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x.GetType().GetProperty(autoSuggestBox.DisplayMemberPath).Name).ToList();
}
else
{
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x).ToList();
}
}
}
public void Detach(DependencyObject associatedObject) => ((AutoSuggestBox) associatedObject).TextChanged -= AutoSuggestSortBehavior_TextChanged;
}
Xaml
<AutoSuggestBox
Header="AutoSuggest"
QueryIcon="Find"
Text="With text, header and icon"
TextChanged="AutoSuggestBox_TextChanged" />
<AutoSuggestBox
DisplayMemberPath="Name"
Header="AutoSuggest2"
QueryIcon="Find"
Text="With text, header and icon"
TextChanged="AutoSuggestBox_TextChanged2" />
TextChanged events
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var abcc = new List<string>();
abcc.Add("xyz");
abcc.Add("321");
abcc.Add("123");
abcc.Add("lopmjk");
abcc.Add("acb");
sender.ItemsSource = abcc;
}
private void AutoSuggestBox_TextChanged2(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var persons = new List<Person>();
persons.Add(new Person { Name = "xyz", count = 1 });
persons.Add(new Person { Name = "321", count = 2 });
persons.Add(new Person { Name = "123", count = 3 });
persons.Add(new Person { Name = "lopmjk", count = 4 });
persons.Add(new Person { Name = "acb", count = 5 });
sender.ItemsSource = persons;
}
So I found the solution with a little experiment, we can use the GetValue() method with passing in the object itself and then it works as expected :
autoSuggestBox.ItemsSource = autoSuggestBox.Items.ToList().OrderBy(x => x.GetType().GetProperty(autoSuggestBox.DisplayMemberPath).GetValue(x)).ToList();
Here i am using this code for copy one list to another
public class Person
{
public string Name { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
var originalList = new List<Person>();
originalList.Add(new Person { Name = "name 1" });
originalList.Add(new Person { Name = "name 2" });
// var newList = originalList.ToList();
var newList = new List<Person>(originalList);
newList[0].Name = "New name";
Console.WriteLine(originalList[0].Name);
}
My result in console is 'New name', why this is happen? When i am updating my new list it also updates my original one. How can i fix this?
Do not worry, this is normal behavior, you have the original list, then you have another list filled in by the original, in your case, both lists point to the same items, which means that you have two references that point to the same memeory case , reason why you change an element from the original one the same element change in the second one and vice-versa .
Case 1 :
public class Person
{
public string Name { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
//here you have the original list, you create your list
//you add element to your list .
var originalList = new List<Person>();
originalList.Add(new Person { Name = "name 1" });
originalList.Add(new Person { Name = "name 2" });
// you create a second list , but here the contain the
// same element than the original list
var newList = new List<Person>(originalList);
newList[0].Name = "New name";
Console.WriteLine(originalList[0].Name);
}
So my program is going to end up being fairly large and I don't want a whole lot of code that could just be shortened. Here is one instance I am looking for some tips on:
private void bookComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string books = null;
// sets books to the clicked item
books = bookComboBox.SelectedItem.ToString();
selectedPictureBox.Visible = true;
// Loads string to list box and image to selectedPictureBox when programming is selected
if (books == "Programming")
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile("programming.png");
bookListBox.Items.Add("Visual Basic");
bookListBox.Items.Add("Java");
bookListBox.Items.Add("C#");
}
// Loads string to list box and image to selectedPictureBox when Networking is selected
else if (books == "Networking")
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile("networking.png");
bookListBox.Items.Add("LAN Networks");
bookListBox.Items.Add("Windows Networking");
bookListBox.Items.Add("More About Networking");
}
// Loads string to list box and image to selectedPictureBox when Web is selected
else if (books == "Web")
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile("html.png");
bookListBox.Items.Add("Web Programming");
bookListBox.Items.Add("JavaScript");
bookListBox.Items.Add("ASP");
}
}
The code works fine but I was just hoping to get some tips on shortening this code if necessary, any input is appreciated.
Assuming you can use C# 7's new Tuples:
private Dictionary<string, (string image, List<string> books)> books = new Dictionary<string, (string image, List<string> books)>
{
{ "Programming", ("programming.png", new List<string> { "Visual Basic", "Java", "C#"} ) },
{ "Networking", ("networking.png", new List<string> {"LAN Networks", "Windows Networking", "More About Networking"}) },
{ "Web", ("html.png", new List<string> {"Web Programming", "Javascript", "ASP"}) }
};
private void bookComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// sets books to the clicked item
string book = bookComboBox.SelectedItem.ToString();
selectedPictureBox.Visible = true;
if (books.Keys.Contains(book))
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile(books[book].image);
foreach(var b in books[book].books)
{
bookListBox.Items.Add(b);
}
}
}
But a class is likely even better:
public class BookGroup
{
public string ImagePath {get;set;}
public List<string> Books {get;}
public BookGroup(string imagePath, params string[] books)
{
ImagePath = imagePath;
Books = new List<string>(books.Length);
Books.AddRange(books);
}
}
Which isn't all that different to use right now, but it formalizes some things that might make this code easier to work with down the road, and it's possible if you can't use Tuples yet. I might also have a Book class by itself, just for fun, but for now:
private Dictionary<string, BookGroup> books = new Dictionary<string, BookGroup>
{
{ "Programming", new BookGroup("programming.png", "Visual Basic", "Java", "C#")},
{ "Networking", new BookGroup("networking.png","LAN Networks", "Windows Networking", "More About Networking") },
{ "Web", new BookGroup("html.png", "Web Programming", "Javascript", "ASP") }
};
private void bookComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// sets books to the clicked item
string book = bookComboBox.SelectedItem.ToString();
selectedPictureBox.Visible = true;
if (books.Keys.Contains(book))
{
bookListBox.Items.Clear();
BookGroup bg = books[book];
selectedPictureBox.Image = Image.FromFile(bg.ImagePath);
foreach(var b in bg.Books)
{
bookListBox.Items.Add(b);
}
}
}
Regardless, I'd definitely have a way to load these from a text file... likely a csv, or maybe even a small in-process database, so that I could update this listing without having to recompile or distribute new program code. And, with that in mind, in order to fit this data in a single structure in a single file, I'd likely also repeat the image and type with each book, so that my csv data looks like this:
Topic,Image,Title
Programming,programming.png,"Visual Basic"
Programming,programming.png,"Java"
Programming,programming.png,"C#"
Networking,networking.png,"LAN Networks"
Networking,networking.png,"Windows Networking"
Networking,networking.png,"More About Networking"
Web,html.png,"Web Programming"
Web,html.png,"Javascript"
Web,html.png,"ASP"
That changes the whole character of the code. I'm a bit biased, but I'd likely use this CSV Parser, and again assuming Tuples I'd produce something like this:
private List<(string Topic, string ImagePath, string Title)> books;
//In the form load code:
books = EasyCSV.FromFile("bookData.csv").Select(b => (b[0], b[1], b[2])).ToList();
//and finally, in the original selectindexchanged method:
private void bookComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string topic = bookComboBox.SelectedItem.ToString();
selectedPictureBox.Visible = true;
var items = books.Where(b => b.Topic == topic).ToArray();
if(items.Length > 0)
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile(items[0].ImagePath);
bookListBox.Items.AddRange(items);
}
}
Make objects and use databindings.
public class Book
{
public BookType BookType { get; set; }
public string Name { get; set; }
public string Image { get; set; }
}
public enum BookType
{
Programming,
Networking,
Web,
}
public partial class Form1 : Form
{
private readonly List<Book> _books = new List<Book>
{
new Book { Image = "programming.png", BookType = BookType.Programming, Name = "VB" },
new Book { Image = "programming.png", BookType = BookType.Programming, Name = "Java" },
new Book { Image = "programming.png", BookType = BookType.Programming, Name = "C#" },
new Book { Image = "networking.png", BookType = BookType.Networking, Name = "LAN Networks" },
new Book { Image = "networking.png", BookType = BookType.Networking, Name = "Windows Networking" },
new Book { Image = "networking.png", BookType = BookType.Networking, Name = "More About Networking" },
new Book { Image = "html.png", BookType = BookType.Web, Name = "Web Programming" },
new Book { Image = "html.png", BookType = BookType.Web, Name = "Javascript" },
new Book { Image = "html.png", BookType = BookType.Web, Name = "ASP" },
};
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var bookTypes = _books.GroupBy(b => b.BookType).Select(g => g.Key).ToList();
this.cboBookTypes.DataSource = bookTypes;
}
private void cboBookTypes_SelectedIndexChanged(object sender, EventArgs e)
{
var bookType = (BookType)this.cboBookTypes.SelectedItem;
var books = _books.Where(b => b.BookType == bookType).ToList();
var img = books.First().Image;
this.imgBook.Image = Image.FromFile(img);
this.lstBooks.DataSource = books;
this.lstBooks.DisplayMember = "Name";
}
}
If you are talking about the length of the code, I would suggest using switch-case-break-default construct
Switch the books variable.
This wont improve the performance though
I think you should create a class that represents book category. Then, you could simply iterate through all the category lists and extract the necessary information, like this:
string books = null;
books = bookComboBox.SelectedItem.ToString();
selectedPictureBox.Visible = true;
for (int i = 0; i < categories.Count; i++) {
if (books == categories[i].Name) {
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile(categories[i].ImagePath);
for (int j = 0; j < categories[i].Items.Count; j++) {
bookListBox.Items.Add(categories[i].Items[j]);
}
}
}
I would suggest to keep all the data in a configuration object and then iterate through that data when performing checks and assignments.
I would create a separate class to hold data for each book: name, picture file name and check box items string array.
Then I would create a list of that object and assign all the data manually on form initialization.
After that, in SelectedIndexChanged event handler, I would iterate (for loop) on each item and check if the book name matched. If it did, then I would use data from that object and then break; the loop.
I do not have visual studio, so giving you the points/suggestions to improve on.
switch should be preferred over if-elseif.
bookListBox.Items.Clear(); and selectedPictureBox.Image out of if block. Use a variable to set the image file name.
Create a class to represent a book list:
public class BookList
{
public string ImageName { get; set; }
public List<string> Items { get;set; }
}
Then create a dictionary to hold these items:
Dictionary<string, BookList> bookLists = new Dictionary<string, BookList>
{
{
"Programming",
new BookList { ImageName = "programming.png", Items = new List<string> { ... } }
}
...
};
Then modify your if statements to:
if (bookLists.ContainsKey(books))
{
bookListBox.Items.Clear();
selectedPictureBox.Image = Image.FromFile(bookLists[books].ImageName);
foreach (var b in bookLists[books].Items)
{
bookListBox.Items.Add(b);
}
}
I'm getting a few errors and also my code is unfinished. I was using another Stackoverflow question to set this up to begin with but it wasn't fit to my needs.
I have three text files which the data is split by commas such as "Name,25,25.6" so string, int, decimal. I have all three text files that have three columns like that, same data types, but just different names/numbers.
I have three different list boxes that I want to split them into but I'm having trouble getting the three different split list items to get into three different list boxes. I'll copy and paste all the code I have. I am also using a combo box to allow the user to select the file they want to load into the combo box which I believe I got it right.
The errors I get are in the displayLists(), it says on the lstItemName.DataSource = Inventory; line that Inventory does not exist in the current context. There are also a plenitude of other errors.
Any help will be appreciated, I'll copy and paste my code. I have a Windows Form and I'm using Visual Studio Express 2012 in C#
namespace TCSCapstone
{
public partial class frmInventory : Form
{
public frmInventory()
{
InitializeComponent();
}
string cstrItemName;
int cintNumberOfItems;
decimal cdecPrice;
decimal cdecTotalPrices;
string selectedList = "";
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedList = this.cmbList.GetItemText(this.cmbList.SelectedItem);
if (selectedList == "Creative Construction")//if the selected combo
box item equals the exact string selected
{
selectedList = "creative"; //then the string equals creative,
which is creative.txt but I add the .txt in the btnLoadInfo method
} else if (selectedList == "Paradise Building")
{
selectedList = "paradise";//this is for paradise.txt
}
else if (selectedList == "Sitler Construction")
{
selectedList = "sitler";//this is for sitler.txt
}
else
{
MessageBox.Show("Please select one of the items.");
}
}
private void btnLoadInfo_Click(object sender, EventArgs e)
{
List<frmInventory> Inventory = new List<frmInventory>();
using (StreamReader invReader = new StreamReader(selectedList +
".txt"))
{
while (invReader.Peek() >= 0)
{
string str;
string[] strArray;
str = invReader.ReadLine();
strArray = str.Split(',');
frmInventory currentItem = new frmInventory();
currentItem.cstrItemName = strArray[0];
currentItem.cintNumberOfItems = int.Parse(strArray[1]);
currentItem.cdecPrice = decimal.Parse(strArray[2]);
Inventory.Add(currentItem);
}
}
displayLists();
}//end of btnLoadInfo
void displayLists()
{
int i;
lstItemName.Items.Clear();
lstNumberOfItems.Items.Clear();
lstPrice.Items.Clear();
lstTotalPrices.Items.Clear();
lstItemName.DataSource = Inventory;
lstItemName.ValueMember = "cstrItemName";
lstItemName.DisplayMember = "cintNumberOfItems";
}
}//end of frmInventory
}//end of namespace
I do not know if this is exactly what you need, but try something like this:
public partial class Form2 : Form
{
List<Inventory> inventory;
public Form2()
{
InitializeComponent();
}
public void ReadFiles()
{
if (inventory == null)
inventory = new List<Inventory>();
using (TextReader r = new StreamReader("file.txt"))
{
string line = null;
while ((line = r.ReadLine()) != null)
{
string[] fields = line.Split(',');
Inventory obj = new Inventory();
obj.Name = fields[0];
obj.Qtd = Convert.ToInt32(fields[1]);
obj.Price = Convert.ToInt32(fields[2]);
inventory.Add(obj);
}
}
SetDataSourceList();
}
public void SetDataSourceList()
{
listBox1.DisplayMember = "Name";
listBox2.DisplayMember = "Qtd";
listBox3.DisplayMember = "Price";
listBox1.DataSource =
listBox2.DataSource =
listBox3.DataSource =
inventory;
}
}
public class Inventory
{
public string Name { get; set; }
public int Qtd { get; set; }
public decimal Price { get; set; }
}
Here is the list item Class:
class ListItem
{
public string Key;
public string Value;
public ListItem()
{
}
public string key
{
get { return Key; }
set { key = value; }
}
public string value
{
get { return Value; }
set { Value = value; }
}
public override string ToString()
{
return Key;
}
public string getvalue(string blabla)
{
return Value;
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
string[] Folders = Directory.GetDirectories(txtFolder.Text);
foreach (string f in Folders)
{
ListItem n = new ListItem();
n.Value = f;
n.Key = Path.GetFileName(f);
listBoxSidra.Items.Add(n);
}
}
private void listBoxSidra_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
lblmsg.Text = null;
comboBoxSeason.Items.Clear();
string[] seasons = Directory.GetDirectories(listBoxSidra.SelectedValue.ToString());
for (int i = 0; i < seasons.Length; i++)
{
comboBoxSeason.Items.Add(seasons[i]);
}
comboBoxSeason.SelectedIndex = 0;
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
}
First Method : I open class named it ListItem , its contains foldername(key) and folder location (value).
Secound Method: I created an array wich contains all the subdirectories from the the directory I set on the text box .
I also created ListItem object named it 'n' , then I set Values to 'n' ,n.Value(represnt the directory location) and n.Key(represnt the directory name).
next step is to add the 'n' object to the list box, now on the listbox i can see the directory name , and each object contains his location .
Thrid Method : thats where im stuck , i created an array, the array suppoed to contain the subdirectory from the chosen listbox item , I mean ,when I will click on a listbox item I want to get his value(the value represent the location ) and by that add the subdirectories to the array , what should i write instead of listBoxSidra.SelectedValue.ToString() ??
Thanks!
In order to make your code work make following changes
private void btnOpen_Click(object sender, EventArgs e)
{
string[] Folders = Directory.GetDirectories(txtFolder.Text);
var dataSource = new List<ListItem>();
foreach (string f in Folders)
{
ListItem n = new ListItem();
n.Value = f;
n.Key = Path.GetFileName(f);
dataSource.Add(n);
}
listBoxSidra.DataSource = dataSource;
listBoxSidra.DisplayMember = "key";
listBoxSidra.ValueMember = "value";
}