I am trying to create a form in winforms with a listview in it. I want to set it up so as when I select a row and press a button, the index of that row is returned.
The problem is whenever I select a row, the form returns the ListViewItem correctly via (mylistView.SelectedItems). However, it always returns index value of (-1) as if nothing is selected via (mylistView.SelectedIndices). Also (mylistView.SelectedItems.Count) returns 0.
So I have 2 columns and an input list (inputlist) which looks like [[2, 3], [3, 4], [4, 5],...] My code looks like this:
for index, c in enumerate (self.inputlist):
self.item = ListViewItem()
self.item.Text = str (self.inputlist[index][0])
self.item.SubItems.Add(str(self.inputlist[index][1]))
self._listView1.Items.Add(self.item)
self.items = self._listView1.SelectedItems
self.index = self._listView1.SelectedIndices
self.count = self._listView1.SelectedItems.Count
Related
lets say I'm making a simple dnd dice roller (cause I am), I get results and I turn it into an array.
Now I want to print the results of the array into a textView that would say:
randNumResultsDisplay.text = "Rolled " then it would spit out all results in the array, in order that they were put in.
I'm not entirely sure where you are stuck here, but if you want to convert an array into a String, one option is to use the java.utils.Arrays class:
val myArray = arrayOf(1, 2, 3)
val contents = Arrays.toString(myArray)
println(contents) // prints "[1, 2, 3]"
So to inject that in your text view as you suggested:
randNumResultsDisplay.text = "Rolled ${Arrays.toString(yourValuesArray)}"
That being said, you would get it for free if you used List instead of Array (which is very much advised):
randNumResultsDisplay.text = "Rolled $yourList"
Another option as pointed out in the comments is the joinToString method, which works on both arrays and lists and allows you to customize the formatting:
println("Rolled: ${myArray.joinToString()}")
// Rolled 1, 2, 3
println("Rolled: ${myArray.joinToString(prefix = "[", postfix = "]")}")
// Rolled [1, 2, 3]
println("Rolled: ${myArray.joinToString(separator = "|")}")
// Rolled 1|2|3
So, something is bugging me with the syntax in Swift for performing operations on Arrays of Ints.
What I wanna do is this : I have an array of Ints which is outputted from a function, its size (count) varies between say 2 and 6 for now, depending on buttons I press in my app.
For each array that is outputted and that contain n ints, I want to create n arrays on which to perform an other action later on.
These "sub" arrays are supposed to be calculated this way :
newArray1's values should be array's values - the value of the first index of newArray1
newArray2's values should be array's values - the value of the second index of newArray2
etc... (I'll automate the number of newArrays according to the array.count)
An other condition applying for those new arrays is that if at a given index the value is negative, I add 12 (so it'll occur for newArray2 at index 1, for newArray3 at indexes 1 & 2, etc... as long as those newArrays are created).
Here's how I wanted to perform that (I created this with dummy arbitrary array in the playground for the sake of testing before inserting the correct stuff in my app code) :
var array : [Int] = [2,4,6,8,9]
var newArray2 = [Int]()
var increment2 = Int()
increment2 = array[1]
newArray2 = array.map {$0 - increment2}
for i in 0..<newArray2.count {
if array[i] < 0 {
newArray2[i] = array[i] + 12
} else {
newArray2[i] = array[i]
}
}
print(array)
print(newArray2)
So of course it doesn't work because I can't seem to figure how to correctly perform operations on Arrays...
Intuitively it seems in my first if statement I'm comparing not the element at index i but i itself, not sure how to reformat that though...
Any help is most welcome, thanks in advance ! :)
[EDIT: I just edited the names of newArray1 to newArray2, same for increments, so that I have negative values and it matches the index value of 1 which is the second element of my main array]
You seem to mean this:
let arr = [2,4,6,8,9]
var results = [[Int]]()
for i in arr.indices {
results.append(arr.map {
var diff = $0-arr[i]
if diff < 0 { diff += 12 }
return diff
})
}
// results is now:
// [[0, 2, 4, 6, 7],
// [10, 0, 2, 4, 5],
// [8, 10, 0, 2, 3],
// [6, 8, 10, 0, 1],
// [5, 7, 9, 11, 0]]
I wrote the function below which accepts an array and returns a randomized version of it.
I've noticed that I sometimes end up with a nil element in randomizedArr when using list.delete(element) to remove an element from the array, but this does not happen when using list.delete_at(index) -- note that the latter is commented out in the below snippet. Am I missing something?
If there's a better way to do what I'm trying to achieve with this function then I would appreciate any suggestion. Thanks!
The array I'm passing to this function is a string array with ~2k elements. I'm passing in a clone of the original array so it doesn't become empty when the function is called. I'm using Ruby 2.1 on Windows 7.
def getRandomList(list)
randomizedArr = Array.new()
cnt = list.length
while (cnt >= 1) do
index = rand(cnt)
prod = list[index]
randomizedArr.push(prod)
list.delete(prod)
#list.delete_at(index)
cnt = cnt - 1
end
if randomizedArr.include?(nil)
puts "found nil element"
end
return randomizedArr
end #getRandomList()
I am not sure why you need to put all that logic when you can randomize the list by list.shuffle.
Refering to the Ruby documentation this is what I found to answer your question...
#To delete an element at a particular index:
arr = [2, 3, 4, 5] #I added this bit
arr.delete_at(2) #=> 4
arr #=> [2, 3, 5]
#To delete a particular element anywhere in an array, use delete:
arr = [1, 2, 2, 3]
arr.delete(2) #=> 2
arr #=> [1,3]
All of that can be found here https://ruby-doc.org/core-2.4.1/Array.html
arr.delete(2) will remove any instance of 2 in an array while delete_at(2) only removes the third value in the array.
When I run this, it correctly changes the 3 in array to "blue", but it changes all other elements to nil.
array = [1, 2, 3, 4]
array.map! do |number|
if number == 3
array[number] = "blue"
end
end
Since I introduced the if statement, I expect it to leave everything else alone. Can someone explain why that's happening and how to get it to only modify the element isolated with the if statement?
When you run map! it:
Invokes the given block once for each element of self, replacing the
element with the value returned by the block.
So when the number doesn't match 3 the block is returning nil - when it is 3 you actually don't need the assignment you're using, having simply "blue" would have the same effect.
This should accomplish what you're trying to do:
array = [1, 2, 3, 4]
array.map! do |number|
number == 3 ? "blue" : number
end
See http://ruby-doc.org/core-2.2.0/Array.html#method-i-map-21
I have a simple 2D array with multiple rows. I want to be able to completely delete a row - not just make it blank/empty.
I have a button that removes the selected row. I've tried the following methods, but all of them leave an empty row in the array, as opposed to completely removing the row. (in this example the first row, but it needs to work on any row selected).
array[0] = [];
array[0].splice();
array[0].length = 0;
array[0].splice(0);
array[0] = null;
Array.splice() is the method you need.
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
private var grid:Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
public function Main()
{
trace(this.grid); // 1,2,3,4,5,6,7,8,9
grid.splice(0, 1);
trace(this.grid); // 4,5,6,7,8,9
}
}
}