I create a playlist for media player.
Follow my code:
Xaml:
<MediaElement x:Name="mePlayer" Margin="64,0,90,61" ></MediaElement>
<ListBox x:Name="listbox4" Background="Salmon" BorderBrush="Black" BorderThickness="3" Drop="listbox4_Drop" >
</ListBox>
<Button x:Name="load" Content="Load" HorizontalAlignment="Left" VerticalAlignment="Top" Width="76" Click="load_Click" Margin="184,285,0,0"/>
Xaml.cs:
private Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
private void load_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".mp3";
ofd.Filter = "All|*.*";
ofd.Multiselect = true;
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{
for (int i = 0; i < ofd.FileNames.Length; i++)
{
var filePath = ofd.FileNames[i];
var fileName = System.IO.Path.GetFileName(filePath);
fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
private void listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
for (int i = 0; i < droppedFilePaths.Length; i++)
{
var filePath = droppedFilePaths[i];
var fileName = System.IO.Path.GetFileName(filePath);
fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
}
It's working single file drop but while I drop multiple file then it's not added on listbox.
Multiple loaded file is loaded but multiple file will not dropped.
How can I drop multiple file on listbox?
Since I am not able to replicate the problem you listed I currently can not help you in that regard. Though, I can help you where I see fit.
Your current Drop method has an extra loop that multiples the number of items you add to the listbox.
Your current method:
private void listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
//if you keep this loop, you will all the dropped files for each dropped file
//therefore, if I dropped 3 files, I'd get 9 entries in the listbox
//if I dropped 4 files, I'd get 16 entries and so on...
for (int i = 0; i < droppedFilePaths.Length; i++)//this has to go
{//this has to go
var filePath = droppedFilePaths[i];//this needs to be a different variable since "i" will no longer exist
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}//this has to go
}
}
}
Refactored (using the ForEach)
private void blaze_125_listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
var filePath = droppedFilePath;
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
This would also work (using the ForLoop)
private void blaze_125_listbox4_Drop_anotherSpin(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
for (int i = 0; i < droppedFilePaths.Length; i++)
{
var filePath = droppedFilePaths[i];
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
Slimmer
private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));
}
}
}
Using the dictionary to store items, and to update the list
Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
//listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));//don't need this anymore
//Check if the file is already in the dictionary.
//Check by looking up the key, and by looking up the value too.
if (fileDictionary.ContainsKey(System.IO.Path.GetFileName(droppedFilePath)) || fileDictionary.ContainsValue(droppedFilePath))
{
//no need to add this file, it's already in the dictionary
//if you try to add a file with a KEY identical to a KEY that already exists in the dictionary,
//it will throw an exception
//A dictionary can contain the same value multiple times, but it can not contain the same key more than once.
}
else
{
//the file is not listed in the dictionary, so lets add it
fileDictionary.Add(System.IO.Path.GetFileName(droppedFilePath), droppedFilePath);
}
}
}
//Now lets call the method in charge of updating the listbox
UpdateTheListbox(fileDictionary, listbox4);
}
private void UpdateTheListbox(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
}
Updated method to maintain the selected item if there is a selected item
private void UpdateTheListboxMaintainExistingSelection(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
//Maintain the selected item if there was one
if (preSelectedItem != null)
{
listboxToModify.SelectedItem = preSelectedItem;
}
}
To maintain the selection or select the last item if there was no selection
private void UpdateTheListboxMaintainExistingOrSelectLastAdded(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
if (preSelectedItem != null)
{
//Maintain the selected item if there was one
listboxToModify.SelectedItem = preSelectedItem;
}
else
{
//select the last item in the listbox if nothing was pre-selected
listboxToModify.SelectedItem = listboxToModify.Items[listboxToModify.Items.Count - 1];
}
}
Related
I've managed to open a txt file to a ComboBox where it shown only the second element"100", and if I select one of the items in ComboBox will show me the the first element witch is "firstName", the problem is I want to modify the first element from the textbox and save it to the main file or new file on the way that I replace the old element with new one from the textbox and save the file as it was at the beginning
TXT FILE
firstName;;;100;;;0;
firstName;;;100;;;1;
firstName;;;100;;;2;
firstName;;;100;;;3;
firstName;;;0100;;;4;
firstName;;;0100;;;5;
firstName;;;0100;;;6;
firstName;;;0100;;;7;
lastName;;;0100;;;0;
lastName;;;0100;;;1;
lastName;;;0100;;;2;
lastName;;;0100;;;3;
lastName;;;0100;;;4;
lastName;;;0100;;;5;
lastName;;;0100;;;6;
lastName;;;0100;;;7;
i want to change the first elements from the textbox and replace it with user input and save the file as in it were,
example:
//output
john;;;100;;;0;
Patrick;;;100;;;1;
firstName;;;100;;;2;
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public string[] lines;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt file (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//string[] lines = File.ReadAllLines(openFileDialog.FileName);
lines = File.ReadAllLines(openFileDialog.FileName);
List<string> result = new List<string>();
string[] par = new string[1];
par[0] = ";;;";
for (int i = 0; i < lines.Length; i++)
{
string[] row = lines[i].Split(par, StringSplitOptions.None);
if (row.Length > 2)
{
if (!result.Contains(row[1]))
result.Add(row[1]);
}
}
foreach (string line in result)
{
comboBox1.Items.Add(line);
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var d = lines;
var t = d.Where(g => g.Contains(comboBox1.Text));
string allItems = "";
foreach (string item in t)
{
string[] r = item.Split(new string[] { ";;;" }, StringSplitOptions.None);
allItems += r[0] + Environment.NewLine;
}
textBox1.Text = allItems;
}
}
}
I've read your code carefully and my understanding may not be perfect but it should be close enough that I can help. One way to achieve your objectives is with data binding and I'll demonstrate this step by step.
"open a txt file to a combobox"
In your main form, you'll move the OpenFileDialog so that it is now a member variable:
private OpenFileDialog openFileDialog = new OpenFileDialog
{
InitialDirectory = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Files"),
FileName = "Data.txt",
Filter = "txt file (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 2,
RestoreDirectory = true,
};
In the main form CTor
Handle event buttonLoad.Click to display the dialog
Handle event openFileDialog.FileOk to read the file.
The DataSource of comboBox will be set to the list of lines read from the file.
Initialize
public MainForm()
{
InitializeComponent();
buttonLoad.Click += onClickLoad;
openFileDialog.FileOk += onFileOK;
comboBox.DataSource = lines;
.
.
.
Disposed += (sender, e) => openFileDialog.Dispose();
}
BindingList<Line> lines = new BindingList<Line>();
private void onClickLoad(object? sender, EventArgs e) =>
openFileDialog.ShowDialog();
In a minute, we'll look at those three things step by step. But first...
Serialize and Deserialize
You have a lot of loose code doing string splits to decode your serializer format. Try consolidating this in a class that is also suitable to be used in a BindingList<Line>. This will be the data source of your combo box. Also consider using a standard serialization format like JSON instead!
class Line : INotifyPropertyChanged
{
// Make a Line from serialized like firstName;;;100;;;0;
public Line(string serialized)
{
// Convert to a stored array
_deserialized = serialized.Split(new string[] { ";;;" }, StringSplitOptions.None);
}
// Convert back to the format used in your file.
public string Serialized => string.Join(";;;", _deserialized);
private string[] _deserialized { get; } // Backing store.
// Convert a list of Lines to a collection of strings (e.g. for Save).
public static IEnumerable<string> ToAllLines(IEnumerable<Line> lines) =>
lines.Select(_ => _.Serialized);
// Determine how a Line will be displayed in the combo box (e.g. "0100").
public override string ToString() => _deserialized[1].ToString();
// Use array syntax to access elements of the split array.
public string this[int index]
{
get => _deserialized[index];
set
{
if(!Equals(_deserialized[index],value))
{
// Send event when any array value changes.
_deserialized[index] = value;
OnPropertyChanged($"{index}");
}
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Load
Once the data file is selected, the raw text file will be shown and the combo box will be populated.
private void onFileOK(object? sender, CancelEventArgs e)
{
lines.Clear();
foreach (var serialized in File.ReadAllLines(openFileDialog.FileName))
{
lines.Add(new Line(serialized));
}
textBoxMultiline.Lines = lines.Select(_=>_.Serialized).ToArray();
comboBox.SelectedIndex = -1;
}
Replacement
Going back to the main form CTor there are three more events we care about:
public MainForm()
{
.
.
.
comboBox.SelectedIndexChanged += onComboBoxSelectedIndexChanged;
textBoxEditor.TextChanged += onEditorTextChanged;
lines.ListChanged += onListChanged;
.
.
.
}
When a new item is selected in the combo box, put it in the text editor.
private void onComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
var item = (Line)comboBox.SelectedItem;
if (item != null)
{
textBoxEditor.Text = item[0];
}
}
When the textEditor text changes, modify the item.
private void onEditorTextChanged(object? sender, EventArgs e)
{
var item = (Line)comboBox.SelectedItem;
if (item != null)
{
item[0] = textBoxEditor.Text;
}
}
When the item changes, update the file display in the big textbox.
private void onListChanged(object? sender, ListChangedEventArgs e)
{
switch (e.ListChangedType)
{
case ListChangedType.ItemChanged:
textBoxMultiline.Lines = lines.Select(_=>_.Serialized).ToArray();
break;
}
}
Save
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Files"),
FileName = "Data.txt",
Filter = "txt file (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 2,
RestoreDirectory = true,
};
private void onClickSave(object? sender, EventArgs e) =>
saveFileDialog.ShowDialog(this);
private void onSaveFileOK(object? sender, CancelEventArgs e)
{
File.WriteAllLines(saveFileDialog.FileName, Line.ToAllLines(lines));
}
I hope this is "close enough" to what you have described that if will give you some ideas to experiment with.
I am trying to come up with a solution where I can search items inside a combo box that contain certain word/phrase. I tried using the AutoComplete text box functionality, but that only searches for the first word which is no good to me.
I have followed the example provided at https://social.msdn.microsoft.com/Forums/vstudio/en-US/4c229a73-cdad-4fa3-95db-97f9ff7810c1/autocomplete-match-on-contains-not-startswith?forum=netfxbcl
I have initiated 2 lists
public List<string> listOnit = new List<string>();
public List<string> listNew = new List<string>();
I then load the data into a comboBox
if (rdr.HasRows == true)
{
// var source = new List<string>();
while (rdr.Read())
{
// myCollectionSales.Add(rdr[0].ToString());
listOnit.Add(rdr[0].ToString());
}
rdr.Close();
//textBox1.AutoCompleteCustomSource = myCollectionSales;
comboBox1.Items.AddRange(listOnit.ToArray());
}
and have a TextUpdate event handler to filter the list when text has changed
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.Items.Clear();
listNew.Clear();
foreach (var item in listOnit)
{
if (item.Contains(this.comboBox1.Text))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
}
I am coming across a problem where the search results don't return what I expect. For example, I search for the string "Bud" and I only get the following results
http://prntscr.com/ppkatd
While in the database, there is also Budweiser 33cl and Keg Budweiser (http://prntscr.com/ppkbu4), for example, which is fetched on the first list.
Should I be using a different method, rather than "Contains"?
Perhaps you are using different cases?
Try with .ToLower():
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.Items.Clear();
listNew.Clear();
foreach (var item in listOnit)
{
if (item.ToLower().Contains(this.comboBox1.Text.ToLower()))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
}
I have a textbox that I want to use to autosearch my text file and display the results in the listbox. the listbox already contains the first item of each line in the text file, so I basically want to search using only the first item of every line in the text file.
The code I currently have does nothing.
private void custsearchbox_TextChanged(object sender, EventArgs e)
{
string[] autosource = File.ReadAllLines(#"data\Suppliers.txt");
for (int g = 0; g < autosource.Length; g++)
{
custsearchbox.AutoCompleteCustomSource.Add(autosource[g]);
}
custsearchbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
I want to type the first item in my text box and search my listbox, as I enter my text I want the list to filter out the items that does not match. Please help me achieve this.
I tried this:
private void supsearchtxt_TextChanged(object sender, EventArgs e)
{
listsup.Items.Clear();
Supfile = System.AppDomain.CurrentDomain.BaseDirectory + "data\\Suppliers.txt";
List<string> proName = new List<string>();
using (StreamReader rdr = new StreamReader(Supfile))
{
string line;
while ((line = rdr.ReadLine()) != null)
{
if (line.Contains(supsearchtxt.Text))
{
string[] val = line.Split(',');
listsup.Items.Add(val[0]);
}
}
}
}
and it works great.
So I have this listview and I can add items to it through a openfiledialog and then I do File.ReadLine and read through all the lines of the textfile I just selected.
So let's say I selected a textfile with 3 lines in it.
Bob
Cat
Human
then what it does is it adds the items to the listview.
Now for every item it adds I want to increment the label (add from 0 > 3).
private void btnAddItems_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Names|*.txt";
if(ofd.ShowDialog() == DialogResult.OK)
{
string[] recipients = File.ReadAllLines(ofd.FileName);
foreach(string name in recipients)
{
lvRecipient.Items.Add(name);
//increment the number of items in the list
foreach(int item in lvRecipient.Items)
{
int i = 0;
i++;
lbCount.Text = i.ToString();
}
}
}
I tried that but got an error as soon as I ran it, I was pretty sure it wouldnt work because there is no real locig behind it, how do I make my label increment from 0 > 3 (or how ever many items there are in the textfile)?
To solve the question that you asked, it can be done like this:
private void btnAddItems_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Names|*.txt";
if(ofd.ShowDialog() == DialogResult.OK)
{
string[] recipients = File.ReadAllLines(ofd.FileName);
foreach(var name in recipients)
{
lvRecipient.Items.Add(name);
lbCount.Text = lvRecipient.Items.Count.ToString();
}
}
}
It would be better to just set the count label after all the items were added, rather then setting it every time a new one is added because the adding operation should be very quick making it unlikely a human would even detect the changing number. This could be done like so:
private void btnAddItems_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Names|*.txt";
if(ofd.ShowDialog() == DialogResult.OK)
{
string[] recipients = File.ReadAllLines(ofd.FileName);
foreach(var name in recipients)
{
lvRecipient.Items.Add(name);
}
lbCount.Text = lvRecipient.Items.Count.ToString();
}
}
using System;
using System.IO;
using System.Windows.Forms;
namespace Jonny
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddItems_Click(object sender, EventArgs e)
{
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Names|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
string[] recipients = File.ReadAllLines(ofd.FileName);
foreach (string name in recipients)
{
lvRecipient.Items.Add(name);
//increment the number of items in the list
foreach (var item in lvRecipient.Items)
{
int i = 0;
i++;
lbCount.Text = i.ToString();
}
}
}
}
}
}
}
It does work - I tried it. If you just change the "int" to "var" - because the "lvRecipient.Items" is unable to be treated as an integer in the foreach loop.
this could be what you are looking, maybe someone will find it useful, my function is running async though and i had to to invoke the label.
delegate void SetTextCallback1(string name);
private void SetTextLabel(string name)
{
if (this.label1.InvokeRequired)
{
SetTextCallback1 d = new SetTextCallback1(SetTextLabel);
this.Invoke(d, new object[] { name });
}
else
{
this.label1.Text = name;
}
}
then i used task.run :
await Task.Run(() => SetTextLabel(name));
and of course the Task.Run line is inside a loop wrapped in an async func.
I get this error when I try to add items to an array, it adds with no problem 1 items, but when there are more it stops and gives an error.
nullReferenceException
Object reference not set to an instance of an object.
public void btnZoek_Click(object sender, EventArgs e)
{
if (search == false)
{
OpenFiles[index] = new AddFileClass();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + "\\Saves");
System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");//add only .txt files
foreach (System.IO.FileInfo fi in rgFiles)
{
OpenFiles[index].setNewItem(index, fi.Name, Convert.ToString(di));//send the info to the array (Number, filename, filelocation)
index++;
}
search = true; //make sure it doens'nt add something double
}
if (search == true)
{
Form3_Zoeken_ frmSearch = new Form3_Zoeken_();
frmSearch.Show();
}
}
here is a pic to show that the fi(FileInfo) and di(DirectoryInfo) are not empty:
It looks to me that you never initialize the OpenFiles array items - that is, you only initialize the first item.
Try this:
public void btnZoek_Click(object sender, EventArgs e)
{
if (search == false)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + "\\Saves");
System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");//add only .txt files
foreach (System.IO.FileInfo fi in rgFiles)
{
OpenFiles[index] = new AddFileClass();
OpenFiles[index].setNewItem(index, fi.Name, Convert.ToString(di));//send the info to the array (Number, filename, filelocation)
index++;
}
search = true; //make sure it doens'nt add something double
}
if (search == true)
{
Form3_Zoeken_ frmSearch = new Form3_Zoeken_();
frmSearch.Show();
}
}