C# display arraylist contents on main application with selection - c#

I have a form with a text box for a user to enter a search string for folder names.
The app then finds matching folder(s) on the network.
If a single folder is returned it opens in explorer.
If multiple folders are returned in the search they are added to an array as a unc path.
I need to know the best way or which object to use to populate the contents of the array with on the main form.
I then need to be able to double click on the desired result to open the containing folder in explorer to handle multiple matches.
searching for match1
array could contain something like: H:\match1, G:\Match1, K:\folder1\Match1

If I understood your intentions correctly, a simple ListBox should do. You can handle each path in your array as one list entry and listen to the double click to open the explorer window.
To fill the listbox you could utilize the datasource attribute:
string[] paths = new string...
// fill array
yourListBox.DataSource = paths;
Addendum: To react to the double click simply listen to the double click event of the list box and in the event handler, do something like this:
private void yourListBox_DoubleClick(object sender, EventArgs e)
{
openExplorerWindow((string)yourListBox.SelectedItem);
}

public Form2()
{
InitializeComponent();
ArrayList paths = new ArrayList();
paths.Add("path1");
paths.Add("path2");
paths.Add("path3");
paths.Add("path4");
foreach (string test in paths)
{
listBox1.Items.Add(test);
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string path = listBox1.SelectedItem.ToString();
}

Related

How pass information between forms involving listView item being changed?

I have file manager program that displays folder in a treeView on the left hand side of a form (frmMain) and files in listView on the left side. I want to be able to select a file (item) from the listView then display the file name in a text on another form with the label = 'Enter a file name.' then rename the file with that new name.
Code from the second form.
public frmRename(string oFile)
{
InitializeComponent();
textBox1.Text = oFile;
}
private void bntOK_Click(object sender, EventArgs e)
{
string nFileName;
nFileName = textBox1.Text;
frmMain fm = new frmMain();
fm.re_nameFile(nFileName);
}
This code runs without any errors; however, when uncommented that is presently commented I get error 'Value of zero is not a valid for index'. I know that this error has talked about a lot; however, I am concern with a different aspect of this error. If I use a line of in private function I don't get this error; whereas, if I use it public function I do. First of all I want to understand why this happens? Second can you tell me how to fix the problem?
So in frmMain you create a new frmRename and in frmRename you create a new frmMain. Bad idea. The new frmMain knows nothing about the original frmMain (including its populated listView).
Solution: in frmMain.bntRename_Click call
rename.ShowDialog();
newFileName = rename.nFileName;
do whatever you want to do with the new name and in frmRename define
public string nFileName {get; private set;}
and further change
private void bntOK_Click(object sender, EventArgs e)
{
nFileName = textBox1.Text;
Close();
}

Hide/modify string in listView

I am making a program in which users can modify remote files. I put the selected files (depending on some predefined criteria) in a listView, but I display only the file names, not full filepaths.
The problem I get however, is that when a user would double-click on an item, it should open another window to modify that item.
private void listView1_DoubleClick(object sender, EventArgs e)
{
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Text+".txt");
Form3 passForm = new Form3();
passForm.ShowDialog();
}
private void Form2_Load(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("\\\\myremotserver\\ftp\\","*.txt", System.IO.SearchOption.AllDirectories);
foreach (string s in files)
{
listView1.Items.Add(Path.GetFileNameWithoutExtension(s));
}
}
The problem is, that the files are all in different subfolders, so if I leave the code as is, it will not display the correct content of the file. For example, the file is called test1.txt, it is placed in myremoteserver\ftp\testfolder\test1.txt, but with my program, it will try to find the file in myremoteserver\ftp\test1.txt.
What I am asking is, if it is possible to modify the listView in such a way, that the full file path is always saved, but only the file names are displayed? I do not want the user to see the complete file path of the files, just the file names.
Use the Tag property of the ListViewItem
So to create items...
foreach (string s in files)
{
ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(s));
lvi.Tag = s;
listView1.Items.Add(lvi);
}
Then in event handler...
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Tag +".txt);

save and load data from .txt to the right texbox

I want to use a .txt file instead of a XML and I want to keep using the WriteAllLineS/WriteAllText and ReadAllLines/ReadAllText.
I have two text boxes 1 & 2 and next to them is a "save" and "load" button - one for each textbox.
My code so far replicates the data from the first box into the second one. Here is the listing:
public partial class Form1 : Form
{
string fileName = "Cache/textBoxdata.txt";
public Form1()
{
InitializeComponent();
}
private void load1_Click_1(object sender, EventArgs e)
{
textBox1.Lines = File.ReadAllLines(fileName);
}
private void Save1_Click_1(object sender, EventArgs e)
{
File.WriteAllLines(fileName, textBox1.Lines);
}
private void load2_Click(object sender, EventArgs e)
{
textBox2.Lines = File.ReadAllLines(fileName);
}
private void save2_Click(object sender, EventArgs e)
{
File.WriteAllLines(fileName, textBox2.Lines);
}
}
I want to be able to write text in the two text boxes, click the "save" button - this should write entered text to the file. Then, once I reopen the app click the "load" button, my data should be loaded from the file and appear in the text box.
At the moment my first text box works. Second text box shows what I wrote in the first one - not the second one.
Why do you have two save/load buttons? Your question is about saving/loading both textboxes at once. So you need only one button for each operation.
To save/load the lines of a textbox into/from a file you can use WriteAllLines and ReadAllLines as you already do. Since you want to have only one file you need to know where the lines for the first textbox end and the second begins. The easiest way to do so is to write the number of lines into the file:
private void SaveTextboxes()
{
List<string> linesToSave = new List<string>();
linesToSave.Add(textBox1.Lines.Length.ToString());
linesToSave.AddRange(textBox1.Lines);
linesToSave.Add(textBox2.Lines.Length.ToString());
linesToSave.AddRange(textBox2.Lines);
File.WriteAllLines(filename, linesToSave);
}
private void LoadTextboxes()
{
string[] loadedLines = File.ReadAllLines(filename);
int index = 0;
int n = int.Parse(loadedLines[index]);
string[] lines = new string[n];
Array.Copy(loadedLines, index + 1, lines, 0, n);
textBox1.Lines = lines;
index += n + 1;
n = int.Parse(loadedLines[index]);
lines = new string[n];
Array.Copy(loadedLines, index + 1, lines, 0, n);
textBox2.Lines = lines;
}
If you add more textboxes you can repeat this for the desired number of textboxes. Build an array of the textboxes and loop through it.
If you really want to have separate save/load buttons for each textbox this might be a bit more confusing since you only want to overwrite a part of the text. Basically this means that on save you first read the whole file into two separate arrays and then write them back with the respective array being replaced by the new text.
of course is the text in the textbox the same after loading - you are using the same file ...
you can save the content into two different files, then it should work

Add textbox text to new string in list each time button is pressed

This is the code:
private void button1_Click(object sender, EventArgs e)
{
List<string> user = new List<string>();
user.Add(usertextBox.Text);
I want it where each time I press the button, whatever is in usertextBox at that point gets added to the list 'user' as a new item, so I can recall the different ones later with [1], [2], [3], etc. I also want it so the user can close the app and all the users will still be saved. I I don't know if C# does this automatically
Even if you can only answer one of my questions that's fine. Thanks!!
In your code you are making List local to Button that means every time you click button new object of List is created, You should create it out side button click method. Try this.
List<string> user = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
user.Add(usertextBox.Text);
You have to define the List out side of the method. C# does not keep the content of the list.
private List<string> user = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
user.Add(usertextBox.Text);
}
for saving the content you could use a database (http://msdn.microsoft.com/en-us/library/bb655884%28v=vs.90%29.aspx) or an xml file (http://www.codeproject.com/Articles/7718/Using-XML-in-C-in-the-simplest-way).
to save the content of the List you could create a new class containing this two methods instead of the list
public List<string> getListContent()
{
//read xml-file
}
public void Add(string t)
{
//write to xml file
}
This will just fine work in single thread applications.

How to drag & drop items in the same ListView?

Consider this is a ListView that shows files and folders, I have already wrote code for copy/move/rename/show properties ...etc and I just need one more last thing. how to drag and drop in the same ListView like in Windows Explorer, I have move and copy functions, and I just need to get the items which user drops in some folder or in other way I need to get these two parameters to call copy function
void copy(ListViewItem [] droppedItems, string destination path)
{
// Copy target to destination
}
Start by setting the list view's AllowDrop property to true. Implementing the ItemDrag event to detect the start of a drag. I'll use a private variable to ensure that D+D only works inside of the control:
bool privateDrag;
private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
privateDrag = true;
DoDragDrop(e.Item, DragDropEffects.Copy);
privateDrag = false;
}
Next you'll need the DragEnter event, it will fire immediately:
private void listView1_DragEnter(object sender, DragEventArgs e) {
if (privateDrag) e.Effect = e.AllowedEffect;
}
Next you'll want to be selective about what item the user can drop on. That requires the DragOver event and checking which item is being hovered. You'll need to distinguish items that represent a folder from regular 'file' items. One way you can do so is by using the ListViewItem.Tag property. You could for example set it to the path of the folder. Making this code work:
private void listView1_DragOver(object sender, DragEventArgs e) {
var pos = listView1.PointToClient(new Point(e.X, e.Y));
var hit = listView1.HitTest(pos);
if (hit.Item != null && hit.Item.Tag != null) {
var dragItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
copy(dragItem, (string)hit.Item.Tag);
}
}
If you want to support dragging multiple items then make your drag object the ListView.SelectedIndices property.

Categories