Wrong selected Item with surselection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madita1212
    New Member
    • Nov 2015
    • 2

    Wrong selected Item with surselection

    *curselection

    So I am very new to Ruby and Tk and I have a Problem, which solution I didn't find after hours of research:

    I have a directory which contains many projects.
    With the following code I insert the names of the existing projects into a Listbox:

    Code:
    $projects = Dir.entries("#{Dir.pwd}/projects")[2..-1] 
    $projects.each {|f| $list.insert 'end', f}
    I created a searchbar with a button which allows to find a specific entry:

    Code:
    def searchprojects
    	searchedprojects = []
    	$list.clear()
    	if $spec_ent.to_s.empty?
    	   $projects.each {|f| $list.insert 'end', f}
    	else 
    	   $projects.each {|i|
               i = i.to_s
    searchedproject s.insert(search edprojects.leng th(), i) if i.downcase.incl ude? $spec_ent.to_s. downcase}
    searchedproject s.each {|f| $list.insert 'end', f}
    end
    end



    And with the following code I chose the selected project (which shall be filtered before):

    Code:
    def show_project
      # updates the status of the chosen project 
      idx           = $list.curselection[0]
      $proj_name    = $projects[idx]
      $status.value = "Project chosen: #{$proj_name}"
    end
    My Problem: After I filtered the Listbox with my searchbar, and I chose a project in the Listbox, I get the wrong drectory(name), because of the index. For instance, when i filter the Listbox, it contains only 3 entries, and I chose the third, it will give me the third entry of the unfiltered Listbox.

    Thanks for helping.
    Last edited by madita1212; Nov 10 '15, 07:11 AM. Reason: *Typo
  • madita1212
    New Member
    • Nov 2015
    • 2

    #2
    Solution:

    Code:
    def show_project
      # updates the status of the chosen project 
      chosenList    = $list.curselection()
      chosenItem    = chosenList[0]
      $proj_name    = $list.get(chosenItem)
      $status.value = "Project chosen: #{$proj_name}"
    end

    Comment

    Working...