listbox seach delete and add
hello first sorry for my bad english.I have two listbox I want to place randomly on other listbox i want to keep the searchString out of random i want to add first listbox.
private void Form1_Load(object sender, EventArgs e)
{
string names = new string[12];
names[0] = "Item 0";
names[1] = "Item 1";
names[2] = "Item 2";
names[3] = "Item 3";
names[4] = "Item 4";
names[5] = "Item 5";
names[6] = "Item 6";
names[7] = "Item 7";
names[8] = "Item 8";
names[9] = "Item 9";
names[10] = "Item 10";
names[11] = "Item 11";
this.LB_1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
int number = LB_1.Items.Count;
for (int i = 1; i <= number; i++)
{
//if (LB_1.Items[i].ToString().Contains(searchString))
//{
// LB_2.Items.Add(searchString);
// //i cant add and delete LB_1 seached item
//}
Random rdn = new Random();
int rnd = rdn.Next(0, LB_1.Items.Count);
LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
}
}
c# listbox
add a comment |
hello first sorry for my bad english.I have two listbox I want to place randomly on other listbox i want to keep the searchString out of random i want to add first listbox.
private void Form1_Load(object sender, EventArgs e)
{
string names = new string[12];
names[0] = "Item 0";
names[1] = "Item 1";
names[2] = "Item 2";
names[3] = "Item 3";
names[4] = "Item 4";
names[5] = "Item 5";
names[6] = "Item 6";
names[7] = "Item 7";
names[8] = "Item 8";
names[9] = "Item 9";
names[10] = "Item 10";
names[11] = "Item 11";
this.LB_1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
int number = LB_1.Items.Count;
for (int i = 1; i <= number; i++)
{
//if (LB_1.Items[i].ToString().Contains(searchString))
//{
// LB_2.Items.Add(searchString);
// //i cant add and delete LB_1 seached item
//}
Random rdn = new Random();
int rnd = rdn.Next(0, LB_1.Items.Count);
LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
}
}
c# listbox
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35
add a comment |
hello first sorry for my bad english.I have two listbox I want to place randomly on other listbox i want to keep the searchString out of random i want to add first listbox.
private void Form1_Load(object sender, EventArgs e)
{
string names = new string[12];
names[0] = "Item 0";
names[1] = "Item 1";
names[2] = "Item 2";
names[3] = "Item 3";
names[4] = "Item 4";
names[5] = "Item 5";
names[6] = "Item 6";
names[7] = "Item 7";
names[8] = "Item 8";
names[9] = "Item 9";
names[10] = "Item 10";
names[11] = "Item 11";
this.LB_1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
int number = LB_1.Items.Count;
for (int i = 1; i <= number; i++)
{
//if (LB_1.Items[i].ToString().Contains(searchString))
//{
// LB_2.Items.Add(searchString);
// //i cant add and delete LB_1 seached item
//}
Random rdn = new Random();
int rnd = rdn.Next(0, LB_1.Items.Count);
LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
}
}
c# listbox
hello first sorry for my bad english.I have two listbox I want to place randomly on other listbox i want to keep the searchString out of random i want to add first listbox.
private void Form1_Load(object sender, EventArgs e)
{
string names = new string[12];
names[0] = "Item 0";
names[1] = "Item 1";
names[2] = "Item 2";
names[3] = "Item 3";
names[4] = "Item 4";
names[5] = "Item 5";
names[6] = "Item 6";
names[7] = "Item 7";
names[8] = "Item 8";
names[9] = "Item 9";
names[10] = "Item 10";
names[11] = "Item 11";
this.LB_1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
int number = LB_1.Items.Count;
for (int i = 1; i <= number; i++)
{
//if (LB_1.Items[i].ToString().Contains(searchString))
//{
// LB_2.Items.Add(searchString);
// //i cant add and delete LB_1 seached item
//}
Random rdn = new Random();
int rnd = rdn.Next(0, LB_1.Items.Count);
LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
}
}
c# listbox
c# listbox
edited Nov 27 '18 at 18:58
foradream
asked Nov 27 '18 at 18:51
foradreamforadream
44114
44114
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35
add a comment |
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35
add a comment |
1 Answer
1
active
oldest
votes
This is what you are looking for:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
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%2f53506315%2flistbox-seach-delete-and-add%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 what you are looking for:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
add a comment |
This is what you are looking for:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
add a comment |
This is what you are looking for:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
This is what you are looking for:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
edited Nov 28 '18 at 18:24
answered Nov 27 '18 at 21:09
AldertAldert
1,4421212
1,4421212
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
add a comment |
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
thank you for the answer. it wasn't exactly what I wanted I don't want to out of the "item 3" on list. I want it in the first place
– foradream
Nov 28 '18 at 16:17
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%2f53506315%2flistbox-seach-delete-and-add%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
Don't use an array. Use a BindingList<T> as a DataSource of the ListBox. If you delete more than one item, you would have to loop backwards to preserve the indexes.
– LarsTech
Nov 27 '18 at 18:55
Create one and only one Random instance for your entire app - not inside a loop
– Make StackOverflow Good Again
Nov 27 '18 at 19:35