listbox
-
Hi can anyone help with the following i use filesystemwatcher to watch a folder and add a new entry into a listbox everytime a new file is created, there is a second listbox that duplicates whats in the first but with some code allows me to change the file name via a text box, works ok as long as the item in listbox2 is highlighted when i change text, however if the listbox updates while i'm in process of changing text it removes the highlight from the item i'm editing and crashes if i press enter to confirm new file name. Ther error i get is no doubt due to it not knowing what index to change as none are highlighted. So how can i get it to keep the file i am changing name of highlighted until i enter the new text.
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.FullPath);
listBox2.Items.Add(e.FullPath);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;
listBox2.SelectedIndex = listBox2.Items.Count - 1;
listBox2.SelectedIndex = -1;
}private void listBox1\_Click(object sender, EventArgs e) { listBox2.SelectedIndex = listBox1.SelectedIndex; } private void listBox2\_Click(object sender, EventArgs e) { int itemSelected = this.listBox2.SelectedIndex; string itemText = this.listBox2.Items\[itemSelected\].ToString(); Rectangle r = this.listBox2.GetItemRectangle(itemSelected); this.textBox1.Location = new System.Drawing.Point(r.X + 217, r.Y + 520); this.textBox1.Size = new System.Drawing.Size(r.Width + 4, r.Height - 10); this.textBox1.Visible = true; this.textBox1.Text = itemText; this.textBox1.Focus(); this.textBox1.SelectAll(); } private void textBox1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { this.listBox2.Items\[this.listBox2.SelectedIndex\] = this.textBox1.Text; this.textBox1.Visible = false; } if (e.KeyChar == 27) this.textBox1.Visible = false; }
-
Hi can anyone help with the following i use filesystemwatcher to watch a folder and add a new entry into a listbox everytime a new file is created, there is a second listbox that duplicates whats in the first but with some code allows me to change the file name via a text box, works ok as long as the item in listbox2 is highlighted when i change text, however if the listbox updates while i'm in process of changing text it removes the highlight from the item i'm editing and crashes if i press enter to confirm new file name. Ther error i get is no doubt due to it not knowing what index to change as none are highlighted. So how can i get it to keep the file i am changing name of highlighted until i enter the new text.
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.FullPath);
listBox2.Items.Add(e.FullPath);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;
listBox2.SelectedIndex = listBox2.Items.Count - 1;
listBox2.SelectedIndex = -1;
}private void listBox1\_Click(object sender, EventArgs e) { listBox2.SelectedIndex = listBox1.SelectedIndex; } private void listBox2\_Click(object sender, EventArgs e) { int itemSelected = this.listBox2.SelectedIndex; string itemText = this.listBox2.Items\[itemSelected\].ToString(); Rectangle r = this.listBox2.GetItemRectangle(itemSelected); this.textBox1.Location = new System.Drawing.Point(r.X + 217, r.Y + 520); this.textBox1.Size = new System.Drawing.Size(r.Width + 4, r.Height - 10); this.textBox1.Visible = true; this.textBox1.Text = itemText; this.textBox1.Focus(); this.textBox1.SelectAll(); } private void textBox1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { this.listBox2.Items\[this.listBox2.SelectedIndex\] = this.textBox1.Text; this.textBox1.Visible = false; } if (e.KeyChar == 27) this.textBox1.Visible = false; }
The solution is very simple. Just store the selectedindex value in some global variable, and keep listbox2's item selected while some new item is added to it. Track listbox2's CollectionChanged event and write code to make the previously selected item (which is now not selected bcoz new item is added to listbox2) selected using our global variable in which we stored selectedindex of listbox2. And ya update the global variable on following event.
private void listBox1_Click(object sender, EventArgs e)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
Global_Listbox2_selectedIndex = listBox1.SelectedIndex;
}HTH
Jinal Desai - LIVE Experience is mother of sage....
-
The solution is very simple. Just store the selectedindex value in some global variable, and keep listbox2's item selected while some new item is added to it. Track listbox2's CollectionChanged event and write code to make the previously selected item (which is now not selected bcoz new item is added to listbox2) selected using our global variable in which we stored selectedindex of listbox2. And ya update the global variable on following event.
private void listBox1_Click(object sender, EventArgs e)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
Global_Listbox2_selectedIndex = listBox1.SelectedIndex;
}HTH
Jinal Desai - LIVE Experience is mother of sage....
Thanks for reply, i've removed second list box as i dont think it has any use for what i want it to do. i now use the textbox to change name of listbox1 selecteditem and i am now trying to rename the actual file with "file.move" but i'm having a problem with second part of "file.move"
private void fileSystemWatcher1\_Created(object sender, System.IO.FileSystemEventArgs e) { listBox1.Items.Add(e.FullPath); listBox1.SelectedIndex = listBox1.Items.Count - 1; listBox1.SelectedIndex = -1; } private void listBox1\_Click(object sender, EventArgs e) { int itemSelected = this.listBox1.SelectedIndex; string itemText = this.listBox1.Items\[itemSelected\].ToString(); Rectangle r = this.listBox1.GetItemRectangle(itemSelected); this.textBox1.Location = new System.Drawing.Point(r.X + 217, r.Y + 520); this.textBox1.Size = new System.Drawing.Size(r.Width + 4, r.Height - 10); this.textBox1.Visible = true; this.textBox1.Text = itemText; this.textBox1.Focus(); this.textBox1.SelectAll(); } private void textBox1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { File.Move(Path.GetFullPath(listBox1.SelectedItem.ToString()), this.textBox1.Text.ToString()+".avi" ); this.listBox1.Items\[this.listBox1.SelectedIndex\] = this.textBox1.Text; this.textBox1.Visible = false; } if (e.KeyChar == 27) this.textBox1.Visible = false;
It renames the file ok but moves file to debug directory of my project instead of leaving it where it is and just renaming it kenny