I am comparing a list of strings with a reference list and storing the strings which are different from the reference list in another list. Now how can i display all the contents of new list using C# coding.
Please help me.
The simplest option would be to just create a large string with one line per item in the list:
var newLineSepratedString = string.Join(Environment.NewLine, myListOfStrings);
This can then be used as the message in your messageBox, or concatenated to some description.
Note that this works fine if the list is small. If you try to display to many items your messagebox will start to become larger than your screen, and that is just not useful. If you need to handle larger sections of text you should create your own dialog with a textbox that can be scrolled.
You only have to build a string with all the list items. Then, you can put this new string into the MessageBox:
List<string> list = new List<string>()
{
"Item 1",
"Item 2",
"Item 3"
};
string allItems = string.Empty;
foreach (string item in list)
{
if (allItems.Length > 0)
{
allItems += "\n";
}
else { } //TODO Nothing
allItems += item;
}
MessageBox.Show($"My Items:\n{allItems}", "Items", MessageBoxButtons.OK, MessageBoxIcon.Information);
This will print all items one down last one.
Related
i want to know how i can write the text of my String Array in multiple textboxes. For example i have an array of the length 5 so it should write inside the first five textboxes the text that is stored. The maximum is 14 textboxes that can be written. The string is filled with filenames that i read out of the given path and i want to display every filename in an textbox so the user can select which one he wants to use.
Create a list/array of the textboxes
myTextboxes = new []{
textbox1,
textbox2,
....
}
and use .Zip to combine the lists so they can be looped over:
foreach(var (myTextbox, myString ) in myTextboxes.Zip(myStrings){
myTextbox.Text = myString ;
}
Can you try this code, using some linq.
Assumption
Your textboxes are directly put in Form, no other panels.
foreach (var pair in strings.Take(14).Zip(
this.Controls.OfType<TextBox>()))
{
pair.Second.Text = pair.First;
}
If you want the user to select a line of text, i would recommend to use a ListBox.
The array should also be a List of strings.
The List for string would look like that:
List<string> texts = new List<string>();
Use to add sth to the List:
texts.Add("some text");
Finally to place every item in the list, use a foreach loop:
foreach (string item in texts)
{
listBox1.Items.Add(item);
}
Let me know if it works
I am using a existing web service which does a postcode search its then stored In a list box the values: "ID", "Text", "Highlight", "Cursor", "Description", "Next". I need to try and access a particular string value which is the ID & Next param and use it for validation later on. When I click on the list box I want the particular data to be taken stored then access the two pieces of information I need. How do I access the information on a particular row of the list box and use that later on?
try
{
int myMaxResultValue = (int)nud_MaxResults.Value;
int myMaxSuggestValue = (int)nud_MaxSuggestions.Value;
findResults = objBvSoapClient.CapturePlus_Interactive_Find_v2_10("Dak4-KZ62-AAdd87-X55", txt_Search.Text, txt_LastId.Text, cb_SearchFor.Text, text_Country.Text, text_LanguagePreference.Text, myMaxResultValue, myMaxSuggestValue);
if (txt_Search.Text.Length <= 2)// if less than two letters are entered nothing is displayed on the list.
{
ls_Output.Items.Clear();// Clear LstBox
ls_Output.Items.Add(String.Format(allDetails, "ID", "Text", "Highlight", "Cursor", "Description", "Next"));
MessageBox.Show("Please enter more than 2 Chars!!");
}
else if (txt_Search.Text.Length >= 3)// if greater than or equal to 3 letters in the search box continue search.
{
// Get Results and store in given array.
foreach (var items in findResults)
{ //Loop through our collection of found results and change resulting value.
ls_Output.Items.Add(String.Format(allDetails, items.Id, items.Text.ToString(), items.Highlight, items.Cursor, items.Description, items.Next));
}
}
}
As a side note your string.Format missing the variables in the string. It should be more like this
int id = 30;
string text = "Hello";
string.Format("This is the ID {0}. Here is some text {1}.", id, text);
The output will be "This is the ID 30. Here is some text Hello.".
To answer your question, you'll have to parse it to pull out the parts you want. You could use regex.split to do this. For example, if it's delimited on space you could do something like this
string[] data = Regex.Split(operation, #"\s+");
Then you can access it like this
string required = data[3];
I have got a list box called lstPTLNameDHOD which has multiple PTL names which gets selected using Ctrl. I want to display the selected names in a label or some way that the person submitted the form can see who exactly they are submitting it for.
My problem is I can only get one name to display on the label.
// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
if (item.Selected)
{
lbl1stPTL.Text = item.Value.ToString();
}
}
This is being called on post back on the reason dropdown being changed.
You are only getting one name to display because your current code would always display name of the last item that is selected.
I don't have a visual studio handy but you could try this:
StringBuilder sbText = new StringBuilder();
// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
if (item.Selected)
{
lbl1stPTL.Text = sbText.Append(item.Value.ToString()).ToString();
}
}
You can probably refine this by adding a space or a comma between two item names but I hope you get the idea and it helps!
Again, sorry for not having VS handy!
sampleList.RemoveAll(a=>a.reference.Contains("123"));
This line of code does not remove any item from the list whereas
sampleList.RemoveAll(a=>!a.reference.Contains("123"));
removes all the items.
I have currently resorted to making another list and going through a for loop and adding stuff to the second list, but I dont really like this approach.
Is there a cleaner way to achieve what I am trying ?
The fact that the second example "removes all the items" and the first removes none, leads me to conclude that none of the item's reference property in the list contains the string "123".
Elementry my dear watson ;)
I would guess your sampleList contains no elements containing "123". This is proven by the fact that the first attempt removes nothing, and the second attempt (which is the reverse of the first) removes everything.
Here is a sample console application I wrote to test out what I think your trying to achieve, and it works:
static void Main(string[] args)
{
List<string> sampleList = new List<string>(new string[]
{
"Some String", "Some Other String", "Hello World", "123456789", "987654123"
});
Console.WriteLine("Items:");
foreach (string item in sampleList)
{
Console.WriteLine(item);
}
Console.WriteLine("\nRemoving items containing \"123\"...");
int itemsRemoved = sampleList.RemoveAll(str => str.Contains("123"));
Console.WriteLine("Removed {0} items.", itemsRemoved);
Console.WriteLine("\nItems:");
foreach (string item in sampleList)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
Start by checking the values of the items in your collection. Once you've made sure the values contain what they should, check the return value of RemoveAll(...) to check the right number of elements have been removed.
Hey guys, i figured out how to add items to a listbox one line at a time:
try
{
if (nameTxtbox.Text == "")
throw new Exception();
listBox1.Items.Add(nameTxtbox.Text);
nameTxtbox.Text = "";
textBox1.Text = "";
nameTxtbox.Focus();
}
catch(Exception err)
{
MessageBox.Show(err.Message, "Enter something into the txtbox", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
But I wont to be able to add multiple items to the same line. Like have first_name |
last_name | DoB all on the same line. When I do
listBox1.Items.Add(last_name.Text);
It adds the last name to a new line on the listbox, I need to add it to the same line as the first name.
It sounds like you still want to add one "item", but you want it to contain more than one piece of text. Simply do some string concatenation (or using string.Format), eg.
listBox1.Items.Add(string.Format("{0} | {1}", first_name.Text, last_name.Text));
Usually you don't want to include multiple columns into a ListBox, because ListBox is meant to have only one column.
I think what you're looking for is a ListView, which allows to have multiple columns. In a ListView you first create the columns you need
ListView myList = new ListView();
ListView.View = View.Details; // This enables the typical column view!
// Now create the columns
myList.Columns.Add("First Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Last Name", -2, HorizontalAlignment.Left);
myList.Columns.Add("Date of Birth", -2, HorizontalAlignment.Right);
// Now create the Items
ListViewItem item = new ListViewItem(first_name.Text);
item.SubItems.Add(last_name.Text);
item.SubItems.Add(dob.Text);
myList.Items.Add(item);
Here has a solution to add multiples items at the same time.
public enum itemsEnum {item1, item2, itemX}
public void funcTest2(Object sender, EventArgs ea){
Type tp = typeof(itemsEnum);
String[] arrItemEnum = Enum.GetNames(tp);
foreach (String item in arrItemEnum){
ListBox1.Items.Add(item);
}
}
Hope this can help.