ListviewtoString COnvertor

Status
Niet open voor verdere reacties.

Cornelis96

Gebruiker
Lid geworden
22 mrt 2010
Berichten
107
Ik heb een Sub gemaakt om een ListviewItem naar een String te COnverteren dit is de Code
Code:
Public Function Listviewitemtostring(ByVal ListVIewitem As ListViewItem, ByRef kolommen As String) As String
            Dim File As String = Nothing
            Dim tlrk As Integer
            Do While tlrk < kolommen

                If 0 = tlrk Then
                    File = ListVIewitem.Text & vbTab
                ElseIf tlrk = kolommen - 1 Then
                    File = File & ListVIewitem.SubItems(tlrk - 1).Text
                Else
                    File = File & ListVIewitem.SubItems(tlrk - 1).Text & vbTab
                End If

                tlrk = tlrk + 1
            Loop
            Listviewitemtostring = File
        End Function
en 1 voor een hele ListView
Code:
        Public Function listviewtoString(ByVal listview As ListView) As String
            Dim Text As String = Nothing
            Dim tlr As Integer
            Do While listview.Items.Count > tlr

                If tlr = listview.Items.Count - 1 Then
                    Text = Text & Listviewitemtostring(listview.Items(tlr), listview.Columns.Count)
                Else
                End If

                tlr = tlr + 1
            Loop
            listviewtoString = Text
        End Function
ik gebruik de onderste maar krijg een fout in de bovenste De fout
Code:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '1' is not valid for 'index'.
 
Laatst bewerkt:
Hopelijk dat ik je deze keer wel mag helpen.

Wat je hier doet:

Code:
Dim tlrk As Integer

Je maakt een integer aan, maar die heeft nog geen waarde. Dus wanneer je daar een vergelijking gaat doen, gebeurt er dit:

Do While niets < kolommen

Probeer de tlrk eens de waarde 0 mee te geven, dus

Code:
Dim tlrk As Interger = 0
 
De Code is nu ...
Code:
Public Function listviewtoString(ByVal listview As ListView) As String
            Dim Text As String = Nothing
            Dim tlr As Integer = 0
            Do While listview.Items.Count > tlr

                If tlr = listview.Items.Count - 1 Then
                    Text = Text & Listviewitemtostring(listview.Items(tlr), listview.Columns.Count)
                Else
                End If

                tlr = tlr + 1
            Loop
            listviewtoString = Text
        End Function
        Public Function Listviewitemtostring(ByVal ListVIewitem As ListViewItem, ByRef kolommen As String) As String
            Dim File As String = Nothing
            Dim tlrk As Integer = 0
            Do While tlrk < kolommen

                If 0 = tlrk Then
                    File = ListVIewitem.Text & vbTab
                ElseIf tlrk = kolommen - 1 Then
                    File = File & ListVIewitem.SubItems(tlrk - 1).Text
                Else
                    File = File & ListVIewitem.SubItems(tlrk - 1).Text & vbTab
                End If

                tlrk = tlrk + 1
            Loop
            Listviewitemtostring = File
        End Function
...maar hij geeft nog steeds de zelfde fout of hij zou in Kolommen moeten zitten...
 
Je geeft in de parameters aan ByRef Kollommen as String, en je probeert er mee te rekenen.

Dus als je in die parameter het aantal kollommen mee wilt geven, zou ik er een int van maken.
 
Prob opgelost in try... catch gezet en de Code Zodanig gewijzigt dat het dit geworden is
Code:
 Public Function listviewtoString(ByVal listview As ListView) As String
            Dim Text As String = Nothing
            Dim tlr As Integer = 0
            Do While listview.Items.Count > tlr
try

                If tlr = listview.Items.Count - 1 Then
                    Text = Text & Listviewitemtostring(listview.Items(tlr), listview.Columns.Count)
                Else
                    Text = Text & Listviewitemtostring(listview.Items(tlr), listview.Columns.Count) & vbCrLf
                End If
catch
end try
                tlr = tlr + 1

            Loop
            listviewtoString = Text
        End Function
Hij werkt!!! Dankje voor je Hulp!!
 
NEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEe

iets te vroeg gejuigt .....:(
ik word depri hiervan
maarja...
 
Code:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '-1' is not valid for 'index'.
 
Hier zowel de VB.NET als de C# oplossing:

VB.NET
Code:
Imports System.Windows.Forms

Namespace Helpmij_543769
	Class ListViewConverter

		Public Function ListViewItemToString(_item As ListViewItem) As String
			'The string that will contain the listview
			Dim _listviewitemstring As String = ""

			'Try to convert the listviewitem to a string
			Try
				'Get column count
				Dim _columns As Integer = 1 + _item.SubItems.Count - 1
				'Current column
				Dim _curcol As Integer = 0

				While _curcol < _columns
					'Check if the _curcol == 0
					If _curcol = 0 Then
						'Return the text of the listviewitem
						_listviewitemstring = _item.Text
					Else
						'Return the current column value
						_listviewitemstring += vbTab & _item.SubItems(_curcol).Text
					End If

					'+1 for the current column
					_curcol += 1
				End While
			Catch ex As Exception
				MessageBox.Show(ex.Message)
			End Try

			'Return it
			Return _listviewitemstring
		End Function

		Public Function ListViewToString(_listview As ListView) As String
			'Create a string to store the listviewstrings
			Dim _listviewstring As String = ""

			'Check if there is at least 1 item in the listview
			If _listview.Items.Count > 0 Then
				'For each listviewitem
				For Each _itm As ListViewItem In _listview.Items
					'Use the fucntion we created before
					_listviewstring += Me.ListViewItemToString(_itm) & vbLf
				Next

				'Return the string
				Return _listviewstring
			Else
				'Return the empty string
				Return _listviewstring
			End If
		End Function

	End Class
End Namespace

C#
[CPP]
using System;
using System.Windows.Forms;

namespace Helpmij_543769
{
class ListViewConverter
{

public string ListViewItemToString(ListViewItem _item)
{
//The string that will contain the listview
string _listviewitemstring = "";

//Try to convert the listviewitem to a string
try
{
//Get column count
int _columns = 1 + _item.SubItems.Count -1;
//Current column
int _curcol = 0;

while (_curcol < _columns )
{
//Check if the _curcol == 0
if (_curcol == 0)
//Return the text of the listviewitem
_listviewitemstring = _item.Text;
else
//Return the current column value
_listviewitemstring += "\t" + _item.SubItems[_curcol].Text;

//+1 for the current column
_curcol++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

//Return it
return _listviewitemstring;
}

public string ListViewToString(ListView _listview)
{
//Create a string to store the listviewstrings
string _listviewstring = "";

//Check if there is at least 1 item in the listview
if (_listview.Items.Count > 0)
{
//For each listviewitem
foreach (ListViewItem _itm in _listview.Items)
//Use the fucntion we created before
_listviewstring += this.ListViewItemToString(_itm) + "\n";

//Return the string
return _listviewstring;
}
else
{
//Return the empty string
return _listviewstring;
}
}

}
}

[/CPP]
 
ERg Bedankt
Vindt je het erg als ik je bij de Info zet???
als je nog een banner heb zou je de link dan wille geven???
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan