Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. listbox

listbox

Scheduled Pinned Locked Moved C#
helpdatabasegraphicsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kennyhibs
    wrote on last edited by
    #1

    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;
        }
    
    L 1 Reply Last reply
    0
    • K kennyhibs

      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;
          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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....

      K 1 Reply Last reply
      0
      • L Lost User

        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....

        K Offline
        K Offline
        kennyhibs
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups