Taking next item in a listbox (youtube videos) in c# - c#

Just a quick question. How to take a next item in a listbox of c#? Just a simple code to go through the items in a listbox:
for (int i = 0; i < listBox1.Items.Count; i++)
{
axShockwaveFlash1.Movie = (listBox1.Items[i].ToString());
}
But the problem is a last item is showing (youtube videos). How to automatically play next item when the previous ends?

int i = 0;
foreach (var listBoxItem in listBox1.Items)
{
// Do stuff using "var" to represent the listbox item, I believe it comes out as a string.
i++; // Increment i so we know what index we are at in the array.
}
Apologies, I may have misread the question. You wanted to make the next item play when the current item finishes? If so, take a look through the aShockwaveFlash1 object to try find a time remaining variable or similar.
Sorry if the first post wasted your time.

Related

Trying to delete an entry from a structure array in C#. Getting conflicting information?

I have been researching this topic and got conflicting answers. Any help is appreciated.
For an assignment, I am instructed to create a structure of arrays. I am trying to delete an entry in one case, and change in another. This is supposed to be by one of the fields and not by index number.
var countOfEmployees = 0;
var employees = new EmployeeData[100];
Console.Write("Please enter employee you want to delete:");
string deleteEmployee = Console.ReadLine();
bool deleted = false;
for (int x = 0; x < countOfemployees; x++)
{
if (items[x].ItemNumber == deleteItemNumber)
{
deleted = true;
}
if (true)
{
//code goes here
}
Any help is appreciated!
In an array, you might be able to delete the value from the place holder, but you will not be able to remove the actual item without some footwork.
Basically, what you can do is: Find the value you've searched for, and replace it with a null and treat it orrespondingly in every other function.
Option 2 would be to remove the item and then use a function that would "shift" all the values with a higher index upwards.
Option 3 is to use the advanced functions C#/.NET has to offer with collections, create a List, remove the item and cast back to an array.
var employees = new EmployeeData[100];
var list = employees.ToList();
int index=-1;
//search through in a loop (won't write it here)...
index = x; //get the index in your list then break the loop
break; //end the loop
list.RemoveAt(index); //delete the spot outside the loop
employees=list.ToArray();
Of course there's checks needed if the search didn't find anything, but i think you can manage to implement that without my help.
And this is of course me assuming that there's only ONE entry with that kind of value in the array.
If it's not, you're gonna have to loop through and erase them until the search doesn't find anything anymore.

How to Get the Index of the First Item Showing in ListView in C#

Is there any way to get the index of the first item currently showing, when the list is scrollable?
I'm making a CharMap with some extensions and just found that ListView can't contain 64k items (see code below)
for (var i = char.MinValue; i < char.MaxValue; i++)
{
var c = Convert.ToChar(i);
if (!char.IsControl(c))
lv1.Items.Add(""+c);
}
so decided to load chars when scroll is at some appropriate points (ie first/last 15%) but ListView doesn't give absolute position of the scrollbar.
It does feel a little hackish, but maybe it will do the job:
int getFirstVisibleItem(ListView lv)
{
ListViewHitTestInfo HI;
for (int i = 0; i < Math.Min(lv.ClientSize.Width, lv.ClientSize.Height); i += 3)
{
HI = lv.HitTest(i, i);
if (HI.Item != null) return HI.Item.Index;
}
return -1;
}
This does not directly help with your scrolling issue but should find the first visible Item as you have asked. If your Items have extremely weird (ie non-square) shapes you may want to change the travesal code a little..
For your requirement implement ListView with custom scrollbar. So you have more control over your scroll position. You decide when to what action based on scroll position. This might be helpful Code

C#: ArgumentOutOfRangeException was unhandled by usercode

I have an if statement saying that if a webpage has a certain text to remove the selected item on a listBox and iterate down to the next one.
I made some code but when I try it I keep getting:
ArgumentOutOfRangeException was unhandled by usercode
This is the error in more detail:
{"InvalidArgument=Value of '1' is not valid for
'SelectedIndex'.\r\nParameter name: SelectedIndex"}
This is my code:
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.SelectedIndex = + 1;
EDIT:
Thanks for all the help guys! I removed the issue by not removing the items and just making it iterate down.
You have to test if the item which you are trying to select acutally exists.
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
If (index < listBox1.Items.Count) {
listBox1.SelectedIndex = index;
}
EDIT: If you want to delete items in a loop, it is a good idea to start at the end, since removing an item changes the position of the following items. Looping upwards would make you skip an item each time you remove an item.
for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
if (listBox1.Items[i].ToString() == "whatever") {
listBox1.Items.RemoveAt(i);
}
}
I'd have to see the full code sample (with the if statement) to know for sure. But it is pretty obvious you are setting the selected item to an index that isn't in the list box.
Be careful, the selectedIndex is zero based, not one based.
MSDN says of your error:
ArgumentOutOfRangeException: The assigned value is less than -1 or
greater than or equal to the item count.
Since the error indicates it is happening when you set the SelectedIndex to 1, I am assuming that you only have a single item in the listbox (index=0) when this code is called.
Since indices are 0-based, setting SelectedIndex to 1 is selecting the second value in the list. I'm guessing it's failing when you have removed all but one of the values and are trying to set SelectedIndex to the second one.
Do you want to select the first item in the listBox? If so the code would be:
if(listBox1.Items.Count > 0) listBox1.SelectedIndex = 0;

C#: For loop not iterating

I have a for loop that for each item in a listbox it will perform a sub. Whats weird is the for loop didnt iterate at all. I had to force it to iterate to the first item but after if finished performing all the subs for that item it just stopped.
How can I make it keep iterating for each item in the listbox?
Heres my code:
listBox7.SelectedIndex = 0;
for (int i = 0; i < listBox7.Items.Count; i++)
{
sub1();
sub2();
listBox1.Items.Add(listBox7.SelectedItem.ToString() + "\r");
while (listBox7.SelectedItems.Count > 0)
{
listBox7.Items.Remove(listBox7.SelectedItems[0]);
}
webBrowser1.Navigate("http://www.google.com");
}
You are most likely removing all of the items in the inner while statement, leaving nothing to iterate on in the second pass.
If your listBox1 contains more than 1 item, an exception will always be thrown on the following line:
listBox1.Items.Add(listBox7.SelectedItem.ToString() + "\r");
because there is no longer a selected item.
This is why exception handling, even if you are just testing something, is critical.
If your goal is to process every item in listBox7, then you can accomplish this by moving the following line:
listBox7.SelectedIndex = 0;
inside the for loop, above the line
sub1();

Iterating over ListView data

I have a ListView which is bound to a DataTable. I would like to iterate over the DataTable's rows and access their data. I figured, to do this, I would simply iterate over the ListViewDataItems in the ListView. To test that I am properly accessing the data, I tried the following code, which should simply print the string at column 0 for each row.
for (int i = 0; i < MyListView.Items.Count; i++)
{
ListViewDataItem item = MyListView.Items[i];
DataRow row = (DataRow) item.DataItem;
Response.Write(row[0]);
}
However, nothing is printed. To verify that the ListView is not empty (which it shouldn't be as the data is properly rendered on my aspx page), I tried this:
Response.Write(MyListView.Items.Count);
This prints the number 16, which is correct as there are 16 rows in my ListView. I'm guessing I'm just not accessing the data correctly. I'd appreciate some insight on this.
The best way is to stop on breakpoint (at line DataRow row = (DataRow) item.DataItem;) and simply to check what you have .
for example like here :http://msdn.microsoft.com/en-us/library/ms173083(v=VS.90).aspx
I decided the best solution was to just iterate over the data directly in the DataTable rather than the ListViewDataItems.
for (int i = 0; i < myTable.Rows.Count; i++)
{
for (int j = 0; j < myTable.Columns.Count; j++)
{
object data = data.Rows[i][j];
// do stuff with data
}
}
For anyone still seeking the correct answer to this question, the following code will work (VB.NET):
Dim di as ListViewDataItem
For Each di in MyListView.Items
Response.Write(CType(di.FindControl("ExampleLabel"), Label).Text)
Next
Just substitute the Response.Write line with whatever you wanted to do to each list item. The example line is looking for a control called 'ExampleLabel', casts it back to a label then writes the text value onto the page.
Easily adapted to C# for anyone proficient (not I alas).

Categories

Resources