C# wrap text inside a listbox - c#

I want to display a list of words in a listbox, wrapped them together. Below is an example what I want to do. I was able to add words to listbox with a comma and in one line. Can you please help me to wrap this text.
For comma separation I used,
ListBox.Items.Add(string.Join(",", myList));
Expected output-
Below is my output

I do not think its possible to print multiple text lines per ListBox item with the standard ListBox. Try using a TextBox instead, with Multiline = true
this.textBox1.Text = string.Join(",", UniqueWord(myList));

Related

C# Listbox selected item text

I am working on a wpf app, c#, in visual studio. My aim is to take the selected item text from a listbox on button press and add it to a list whilst also appending it to a text block. My code thus far:
bill.BillItems.Add(lstbxVeg.SelectedItem.ToString());
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
Where bill is the class name, BillItems the List name. I can see that items are indeed added to the text block, yet instead of the desired string relating to the listbox item selected, it reads System.Data.DataRowView. Where am I going wrong?
EDIT: This is not the correct answer.
lstbxVeg.SelectedItem.Text.ToString()
I think the actual problem is this line:
txtblkBill.Text = String.Join(Environment.NewLine, bill.BillItems);
You mentioned you want to append the value, this line is not accomplishing that.
I think you want to do this:
txtblkBill.Text += lstbxVeg.SelectedItem.ToString();
If that isn't the answer you should look up String.Join() as it takes a String[] not a List.

How can I insert a newline into a TextBlock without XAML?

I have created a WPF TextBlock inside a Label in code (XAML is not possible in my case) as follows:
Label l = new Label();
TextBlock tb = new TextBlock();
l.Content = tb;
I then come to a situation where I need to set the .Text property of TextBlock containing a new line, such as:
tb.Text = "Hello\nWould you please just work?";
I have tried numerous encodings (HTML encoding, ASCII encoding, etc.) of various newline combinations (carriage return, linefeed, carriage return plus linefeed, linefeed plus carriage return, double linefeed, double carriage return, etc... ad nauseum).
Answers should contain absolutely no XAML.
Answers should assume that the original objects were created using C# code and not XAML.
Answers should not refer to binding of WPF properties. No binding is being used. The "Text" property of the "TextBlock" object is being set. The newline must be inserted there.
If this is impossible, please let me know how I can programmatically add newlines by dynamically replacing each newline in an arbitrary input String into a LineBreak object (or whatever is needed to get this working). The input String will have arbitrary (readable text) contents that I am unable to anticipate in advance because the text itself is dynamic (user-defined); the source strings will have the linefeed character (aka LF, aka \n) but I can easily replace that with whatever is needed.
Also, if it is easier to do this with a Label directly rather than a TextBlock in a Label, that is good too -- I can use that. I just need a control with automatic plain text line wrapping.
You have a few choices:
Use the Environment.NewLine property:
TextBlock tb = new TextBlock();
tb.Text = "Hello" + Environment.NewLine + "Would you please just work?";
Or, manually add Runs and LineBreaks to the TextBlock:
TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Hello"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new Run("Would you please just work?"));
Just another small note. I had a similar problem and took the string from a resource file. What I noticed is that .NET delivered the string "\r\n" as "\\r\\n". When I corrected that by replacing the double-backslashes with normal backslashes everything worked as expected.
You can store string values in "Resources.resw", and get them in code directly.
In the VS resource editor itself not possible to add empty lines to string values. To do this, just edit value in any text editor, add empty lines how many you need, and "Copy -> Paste" to Value column of Visual Studio's resource editor.
For example: value of "Farewell" with 2 line breaks: Goodbye\n\nSave changes? - will look like this in text editor (and as it is, need to be copied to VS):
Goodbye
Save changes?
To get string value of the resource in C# code, call 1 of the following lines (UWP or WinUI3), depending on your application.
UWP code:
Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView().GetString("Farewell");
WinUI3 code:
new ResourceLoader().GetString("Farewell");

dynamic search in file using c#

/*I am reading many files and getting data through File.ReadAllLines. Now I want to search in these files for a specific string written in a textbox. Whenever I put some text in the textbox it must return lines of text containing that word. I am coding in textchanged property but it is not successful as it gives me a result even when I press backspace or add any other word. */
I have successfully made it to work. I was clearing the listbox every time it runs else statement. Now I just want you people to tel me what should I do to make it work fast.
if you don't want to get the result right away when you type something in Textbox then put Button on your form and try this code:
string[] lines=File.ReadAllLines(path);
var result = lines.Where(l => l.Contains("text")).ToList();
I hope this helps

C# + Windows Phone => TextBlock, preserve whitespaces?

I am creating an application where I need to loop through a series of text lines that may contain whitespace.
I build up a string by doing my_string += the_line_to_add and update the Text property of the TextBlock with the final string.
Pretty simple actually, however, a line that looks like this:
"a b c"
will end up as follows:
"a b c"
I don't want all of those spaces to be removed though. I want the line to keep the extra spaces and remain unchanged:
"a b c"
The TextBlock is created programmatically and added into a StackPanel. I looked at the different properties but just can't figure it out.
Honestly, I'd approach this problem differently. I wouldn't use whitespaces in a string to layout the text. If you need 3 string in the screen add 3 textboxes and set Margin proprety. This depends on the input text, but if there will be too many whitespaces the text will be out of the screen.
Alternatively, you can use Run to format the text.

White squares in listbox

I have a binded ListBox that displays text. This text is coming from another multiline TextBox. Multiple lines are shown as first line□□second line in ListBox.
I want this text to be displayed as first line second line OR as first line in a ListBox row.
EDIT:
I am actually trying to create a note taking application. The 'ListBox' will show a part of text and 'TextBox' will show the detailed text.
If you check the string, returned via TextBoxt.Text with Multiline set to true, you will notice, that each line ends with "\r\n" sequence. So, depending on what you want, you can split the string from the TextBox using "\r\n" as the parameter in Split method and show these lines as different items in ListBox or replace the "\r\n" in the string with whitespace and show it as a single ListBox item.

Categories