ListView.FindItemWithText inside WHILE loop fails after first ListView line
I want change the content of 2nd column of each line of ListView
with diferents data according with is found via FindItemWith
.
My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:
Already when i want change the content searching a text that stays on second line this happens:
This is the code:
public void fillData(string search, string data, ListView haystack)
if (haystack.Items.Count > 0)
int idx = 0;
ListViewItem found;
while (idx < haystack.Items.Count)
found = haystack.FindItemWithText(search, true, idx);
if (found != null)
haystack.Items[idx].SubItems[1].Text = data.ToString();
idx++;
private void button3_Click(object sender, EventArgs e)
int i = 0;
while (i < 3)
ListViewItem item = new ListViewItem();
item.Text = i.ToString();
item.SubItems.Add("192.168.0." + i.ToString());
listView1.Items.Add(item);
i++;
private void button1_Click(object sender, EventArgs e)
fillData("192.168.0.0", "AAA", listView1);
private void button2_Click(object sender, EventArgs e)
fillData("192.168.0.1", "BBB", listView1);
c# listview visual-studio-2013
add a comment |
I want change the content of 2nd column of each line of ListView
with diferents data according with is found via FindItemWith
.
My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:
Already when i want change the content searching a text that stays on second line this happens:
This is the code:
public void fillData(string search, string data, ListView haystack)
if (haystack.Items.Count > 0)
int idx = 0;
ListViewItem found;
while (idx < haystack.Items.Count)
found = haystack.FindItemWithText(search, true, idx);
if (found != null)
haystack.Items[idx].SubItems[1].Text = data.ToString();
idx++;
private void button3_Click(object sender, EventArgs e)
int i = 0;
while (i < 3)
ListViewItem item = new ListViewItem();
item.Text = i.ToString();
item.SubItems.Add("192.168.0." + i.ToString());
listView1.Items.Add(item);
i++;
private void button1_Click(object sender, EventArgs e)
fillData("192.168.0.0", "AAA", listView1);
private void button2_Click(object sender, EventArgs e)
fillData("192.168.0.1", "BBB", listView1);
c# listview visual-studio-2013
add a comment |
I want change the content of 2nd column of each line of ListView
with diferents data according with is found via FindItemWith
.
My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:
Already when i want change the content searching a text that stays on second line this happens:
This is the code:
public void fillData(string search, string data, ListView haystack)
if (haystack.Items.Count > 0)
int idx = 0;
ListViewItem found;
while (idx < haystack.Items.Count)
found = haystack.FindItemWithText(search, true, idx);
if (found != null)
haystack.Items[idx].SubItems[1].Text = data.ToString();
idx++;
private void button3_Click(object sender, EventArgs e)
int i = 0;
while (i < 3)
ListViewItem item = new ListViewItem();
item.Text = i.ToString();
item.SubItems.Add("192.168.0." + i.ToString());
listView1.Items.Add(item);
i++;
private void button1_Click(object sender, EventArgs e)
fillData("192.168.0.0", "AAA", listView1);
private void button2_Click(object sender, EventArgs e)
fillData("192.168.0.1", "BBB", listView1);
c# listview visual-studio-2013
I want change the content of 2nd column of each line of ListView
with diferents data according with is found via FindItemWith
.
My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:
Already when i want change the content searching a text that stays on second line this happens:
This is the code:
public void fillData(string search, string data, ListView haystack)
if (haystack.Items.Count > 0)
int idx = 0;
ListViewItem found;
while (idx < haystack.Items.Count)
found = haystack.FindItemWithText(search, true, idx);
if (found != null)
haystack.Items[idx].SubItems[1].Text = data.ToString();
idx++;
private void button3_Click(object sender, EventArgs e)
int i = 0;
while (i < 3)
ListViewItem item = new ListViewItem();
item.Text = i.ToString();
item.SubItems.Add("192.168.0." + i.ToString());
listView1.Items.Add(item);
i++;
private void button1_Click(object sender, EventArgs e)
fillData("192.168.0.0", "AAA", listView1);
private void button2_Click(object sender, EventArgs e)
fillData("192.168.0.1", "BBB", listView1);
c# listview visual-studio-2013
c# listview visual-studio-2013
asked Nov 14 '18 at 1:01
DavisonDavison
83110
83110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is because the overload function you used for FindItemWithText
, keeps searching all the items from the index you passed in.
When the loop has idx = 0 then FindItemWithText
will try to search all three items 0,1,2.
When the loop has idx = 1 then FindItemWithText
will try to search two items 1,2.
When the loop has idx = 2 then FindItemWithText
will try to search only one item 2.
So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.
Here is the documentation link
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_
Any how FindItemWithText
returns the System.Windows.Forms.ListViewItem
. Just search once from zero. Use that item to update.
1
thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"
– Davison
Nov 14 '18 at 1:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291704%2flistview-finditemwithtext-inside-while-loop-fails-after-first-listview-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because the overload function you used for FindItemWithText
, keeps searching all the items from the index you passed in.
When the loop has idx = 0 then FindItemWithText
will try to search all three items 0,1,2.
When the loop has idx = 1 then FindItemWithText
will try to search two items 1,2.
When the loop has idx = 2 then FindItemWithText
will try to search only one item 2.
So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.
Here is the documentation link
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_
Any how FindItemWithText
returns the System.Windows.Forms.ListViewItem
. Just search once from zero. Use that item to update.
1
thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"
– Davison
Nov 14 '18 at 1:51
add a comment |
This is because the overload function you used for FindItemWithText
, keeps searching all the items from the index you passed in.
When the loop has idx = 0 then FindItemWithText
will try to search all three items 0,1,2.
When the loop has idx = 1 then FindItemWithText
will try to search two items 1,2.
When the loop has idx = 2 then FindItemWithText
will try to search only one item 2.
So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.
Here is the documentation link
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_
Any how FindItemWithText
returns the System.Windows.Forms.ListViewItem
. Just search once from zero. Use that item to update.
1
thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"
– Davison
Nov 14 '18 at 1:51
add a comment |
This is because the overload function you used for FindItemWithText
, keeps searching all the items from the index you passed in.
When the loop has idx = 0 then FindItemWithText
will try to search all three items 0,1,2.
When the loop has idx = 1 then FindItemWithText
will try to search two items 1,2.
When the loop has idx = 2 then FindItemWithText
will try to search only one item 2.
So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.
Here is the documentation link
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_
Any how FindItemWithText
returns the System.Windows.Forms.ListViewItem
. Just search once from zero. Use that item to update.
This is because the overload function you used for FindItemWithText
, keeps searching all the items from the index you passed in.
When the loop has idx = 0 then FindItemWithText
will try to search all three items 0,1,2.
When the loop has idx = 1 then FindItemWithText
will try to search two items 1,2.
When the loop has idx = 2 then FindItemWithText
will try to search only one item 2.
So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.
Here is the documentation link
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_
Any how FindItemWithText
returns the System.Windows.Forms.ListViewItem
. Just search once from zero. Use that item to update.
answered Nov 14 '18 at 1:41
NaiduNaidu
5,2973818
5,2973818
1
thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"
– Davison
Nov 14 '18 at 1:51
add a comment |
1
thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"
– Davison
Nov 14 '18 at 1:51
1
1
thank you. This worked! => "
Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"– Davison
Nov 14 '18 at 1:51
thank you. This worked! => "
Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.
"– Davison
Nov 14 '18 at 1:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291704%2flistview-finditemwithtext-inside-while-loop-fails-after-first-listview-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown