Replacing listBox items during repopulation - c#

I'm building a contact manager program and I have a button that when pressed, updates a listBox of names. Unfortunately, I'm expecting my user to not always be putting the names in before closing the program. I have decided to make new contacts in the list appear as random numbers before a name is entered, so some of my items will be numbers and some will be names. I can't seem to get the names to show up, though. I've been storing the names as strings inside of text files contained inside folders of unique random numbers. What I want is to be able to load all the folder names into the list, then check to see if a name is associated with the item, and if it is, replace the number with the respective name. The following code is as far as I have gotten before I got stumped.
private void button5_Click(object sender, EventArgs e)
{
//populate the list of people
listBox1.Items.Clear();
string[] dirs = Directory.GetDirectories(#"C:\PersonalManager\");
foreach (string dir in dirs)
{
//replace numbered items with names
for (int i = 0; i < listBox1.Items.Count; i++)
{
String Text = Convert.ToString(listBox1.Items[i]);
Text = Text.Replace(File.ReadAllText(#"C:\PersonalManager\"+listBox1.Items[i]+#"\first.txt")); //first.txt is the file containing the name
listBox1.Items[i] = Text;
}
listBox1.Items.Add(Path.GetFileName(dir));
}
}
I'm also pretty sure there is an overload bug in there somewhere, as Visual Studio refuses to compile it. Maybe I should go about this in a different way? What would any of you suggest? I have searched all through Google, and Bing but have yet to figure out what I'm doing wrong. Sorry if my code is a mess, it's my first time.
P.S. I'm a beginner, so I can't wrap my head around too much code without comments every now and then.

I'm pretty sure that this line is breaking your code:
Text = Text.Replace(File.ReadAllText(#"C:\PersonalManager\"+listBox1.Items[i]+#"\first.txt"));
The string.Replace does not have a overload with just one parameter. It has always two.
Just to give you a starting point:
private void button5_Click(object sender, EventArgs e)
{
//populate the list of people
listBox1.Items.Clear();
string[] dirs = Directory.GetDirectories(#"C:\PersonalManager\");
foreach (string dir in dirs)
{
var filePathToRead = Path.Combine(dir, "first.txt");
var allTextOfTheFile = File.ReadAllText(filePathToRead);
// now you can work with the content of the file
listBox1.Items.Add(Path.GetFileName(dir));
}
}

Related

Search for word in ListBox written on InputBox (C# VS)

I have a ListBox with words and I need to click a button that opens an InputBox where I can search for a word and the program will run the ListBox and highlight the word I wrote in the InputBox if it's there. If the program reaches the end of the list and doesn't find the word then I'll get a MessageBox saying the word I'm looking for isn't there. I need to use some sort of cycle for this program.
I know how to make the button, InputBox and the error MessageBox, but I don't know how to do the searching and cycle.
I've read a lot of similar questions here but I don't think any of them return the result I'm looking for.
Can anyone help me? Or redirect me to a post with the answer?
This is for Winforms.
That should get you on track, it's pretty much self-explanative:
whenever text changes
find matching items in list
select them
Code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox ?? throw new InvalidOperationException();
var text = textBox.Text;
if (string.IsNullOrWhiteSpace(text))
return; // nothing to search for
const StringComparison comparison = StringComparison.InvariantCultureIgnoreCase; // maybe change this
// find items matching text
var indices = new List<int>();
for (var i = 0; i < listBox1.Items.Count; i++)
{
var item = listBox1.Items[i];
if (string.Equals(item?.ToString(), text, comparison))
indices.Add(i);
}
// select them in list
if (!indices.Any())
return;
listBox1.SelectedIndices.Clear();
foreach (var index in indices)
listBox1.SelectedIndices.Add(index);
}
Of course, list selection mode has to be multiple for it to work properly.
Also, you will need to clear selection if there are no matches so as to not leave the UI in an ambiguous state (not done).

How to fix ListView.LargeImageList showing images twice

I am working on a piece of software, which compares memes and helps users organize memes on their computer. As a part of this I am using Windows.Forms to build a UI. This UI lets the user add folders to be checked for images, which can be compared to a set of known meme templates.
My issue arises when I try to show the user the found images. To do this I am using a ListView and the property LargeImageList to contain a tuple of the image and the name of the image file.
Here is the piece of code in question:
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (var file in files)
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(Image.FromFile(file));
}
foreach (var file in files)
{
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
This is an example of what the user sees when they first load in a folder. When the user tries to load in another folder this happens. It shows the images from the first folder, with the names of the image files from the second folder.
Does anyone know a fix for this issue? I have tried a variety of options in order to get around the issue. All from trying to clear the ImageList used to contain the images, to trying my hand at controlling when the ListView updates. None of this has worked. I have also tried googling the issue, but with no luck of finding a fix.
Thank you in advance.
If you want to show the content of a single folder at the time, then dispose of the objects in your ImageList.
If you instead want to show the content of more than one folder, you need to specify the new index of the image added. You're instead adding a new Item in the ListView using the same index reference:
int i = 0;
//(...)
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
The indexer (i) always starts from 0, thus the ListView Item will use the images in your Imagelist starting from the Image at Index[0] each time. The new images won't ever be shown.
You can use the ImageList.Images.Count value, representing the number of Images already added to the ImageList, as base and increment the indexer starting from this value:
private void button1_Click(object sender, EventArgs e)
{
int i = imageList1.Images.Count;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
foreach (var file in Directory.GetFiles(fbd.SelectedPath))
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(new Bitmap(file, true));
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
If you allow to remove an Image from the ListView, you should also remove it from the ImageList: this implies that you need to re-index all the ListView Items starting from the Item that follows the one removed.
Remember to dispose of the Images you remove from the ImageList.

how to go next line in ListBox?

hey guys i wanna to load a File path in my listBox.
everything is ok
i just have a problem that is when i Close the app and then open it again
loaded files are in the one lane and it recognize them as one item in listBox
i tried to use "\n" , "\r" none of these works...
so what u guys suggest?
(i save user changes in App Setting to read them later)
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.FileList != string.Empty)
{
fileListBox.Items.Add(Properties.Settings.Default.FileList);
}
UnlockForm f2 = new UnlockForm();
if (Properties.Settings.Default.PasswordCheck == true)
f2.ShowDialog();
else
return;
}
private void button1_Click_1(object sender, EventArgs e)
{
op = new OpenFileDialog();
op.Title = "Select your stuff";
op.Filter = "All files (*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
fileName = op.FileName;
fileListBox.Items.Add(fileName);
}
Properties.Settings.Default.FileList += fileName+"\n";
Properties.Settings.Default.Save();
}
When creating the property in settings designer:
Set the name to whatever you want, for example Files
Set the type as System.Collections.Specialized.StringCollection
Set the scope as User
If you want to have some default values, use ... in Value cell to edit default value.
Then you can easily set it as DataSource of the ListBox.
listBox1.DataSource = Properties.Settings.Default.Files;
Also to add some values:
Properties.Settings.Default.Files.Add("something");
Properties.Settings.Default.Save();
If you added something to the Files, if you want the ListBox shows the changes, set DataSource to null and then to Files again.
It looks like you have defined your FileList as String in our App Settings. There are two ways you can approach this.
a) Using FileList as Collection.
You can change the Type of FileList to StringCollection in your App Settings. Then, you can add items to your list as follows
fileListBox.Items.AddRange(Properties.Settings.Default.FileList.Cast<string>().ToArray());
b) Using FileList as String.
If you really want to retain Properties.Settings.Default.FileList as string, you would need to split it on the run using your delimiter character (let's say ';')
fileListBox.Items.AddRange(Properties.Settings.Default.FileList.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries));
In your case, a collection might be a better approach unless you have specific reasons outside the scope of OP to use string.

How do I replace a part of a line in a text file with user input?

Here's the code.
private void SaveButton_Click(object sender, EventArgs e)
{
string textBoxText = TextBox.Text;
var lines = File.ReadAllLines(#"F:\Bioshock2SP.ini");
foreach(string line in lines)
{
if (line.Contains("VoVolume="))
{
//This is where I get confused.
string settingLine = line;
string replaceline = (line.Replace(line, textBoxText));
File.WriteAllText(#"F:\Bioshock2SP.ini", settingLine);
}
break;
}
MessageBox.Show("Setting saved!");
}
The idea is to replace part of a setting in a Settings.ini file for a game I play, using the user input of a textbox in my form. The user types in a number for example, "1.56" and then hit the Save button to replace the existing line with their input. In this case that setting is the volume.
The application runs completely fine, but after hitting save and going into the settings file my input isn't saved.
There should be a change in the way you save the file.
Save each line of the file as you get it, editing if required.
void SaveButton_Click(object sender, EventArgs e)
{
var textBoxText = TextBox.Text;
var lines = File.ReadAllLines(#"F:\Bioshock2SP.ini");
using (var file = new StreamWriter(#"F:\Bioshock2SP.ini"))
foreach(string line in lines)
{
if (line.Contains("VoVolume="))
file.WriteLine(line.Substring(0, 9) + textBoxText); // Writes something like 'VoVolume=1.56'
else file.WriteLine(line); // No editing required
}
MessageBox.Show("Setting saved!");
}
I think there are a couple of separate issues with your code:
Replace Function
string replaceline = (line.Replace(line, textBoxText));
Replace accepts a string to look for and a string to replace it with. Your first argument is 'line', so it would replace the whole line with the value in textBoxText. I assume you only want to replace a portion of the line with that value. In that case, you need to use something like line.Replace(searchString, textBoxText) where you have previously defined searchString as the text you want to replace. If you don't know what that value is, but there is a pattern, you might want to look into using regular expressions which will let you define a pattern to search and replace.
WriteAllText Function
File.WriteAllText(#"F:\Bioshock2SP.ini", settingLine);
This line will replace the entire contents of BioShock2SP.ini with the value in settingLine. There are two problems here.
One is that settingLine was the saved value before you did the replacement - so it has not included the results of your replace operation. You should use replaceline (assuming it has been correctly modified).
Even if you do that, though, the other is that File.WriteAllText will replace the whole file with the value in settingLine - which is probably not what you want. You'd be better off modifying the line in the array and using File.WriteAllLines to re-output the whole array - assuming the file has multiple lines in it.
The hints above may help you resolve this - to properly answer the question though, I'd need to see a sample of what the file looks like, and the patterns you are trying to replace.

Strip leading characters from a directory path in a listbox in C#

So i am attempting to teach myself C#, I have a program that I originally wrote in batch and am attempting to recreate in C# using WPF. I have a button that allows a user to set a directory, that directory selected is then displayed in a text box above a listbox which adds every subfolder, only first level, to the listbox. Now all this works fine but it writes out the entire directory path in the listbox. I have been trying to figure out how to strip the leading directory path off the list box entries for over an hour to no avail. Here is what I have so far:
private void btn_SetDirectory_Click(object sender, RoutedEventArgs e)
{
//Create a folder browser dialog and set the selected path to "steamPath"
var steamPath = new FolderBrowserDialog();
DialogResult result = steamPath.ShowDialog();
//Update the text box to reflect the selected folder path
txt_SteamDirectory.Text = steamPath.SelectedPath;
//Clear and update the list box after choosing a folder
lb_FromFolder.Items.Clear();
string folderName = steamPath.SelectedPath;
foreach (string f in Directory.GetDirectories(folderName))
{
lb_FromFolder.Items.Add(f);
}
}
Now I tried changing the last line to this, and it did not work it just crashed the program:
foreach (string f in Directory.GetDirectories(folderName))
{
lb_FromFolder.Items.Add(f.Substring(f.LastIndexOf("'\'")));
}
I am fairly certain that the LastIndexOf route is probably the right one but I am at a dead end. I apologize if this is a dumb question but this is my first attempt at using C#. Thanks in advance.
This can solve your issue
string folderName = steamPath.SelectedPath;
foreach (string f in Directory.GetDirectories(folderName))
{
// string[] strArr = f.Split('\\');
lb_FromFolder.Items.Add(f.Split('\\')[f.Split('\\').Length-1]);
}
You can use this code:
string folderName = steamPath.SelectedPath;
foreach (string f in Directory.GetDirectories(folderName))
{
lb_FromFolder.Items.Add(f.Remove(0,folderName.Length));
}

Categories