Visual Studio listbox text is too long - c#

So I'm making an application for a friend of mine. This app contains a winforms listbox. The listbox contains tweets. Everything works fine but the problem is the length of the tweets. When a tweet is too long, the listbox cuts parts away. Example:
tweetListBox:
Thow tweeted: jalskjdkljasdljlasjkdlasjdlkjaslkjaskljdlasjkd...
It adds dots when the text is too long. I can't resize the listbox because then it's too big. Is there a way to make the text that's bigger then 100 pixels split into different lines?

If you don't need the selection capabilities of a ListBox, you could switch to a multi line line text box. Use a regular text box and set the multi line property = true. Stretch the control to the appropriate size to match that of the ListBox. Add a new tweet using :
multiLineTweetBox.Append(newTweet + Environment.NewLine);

It is not possible by just setting a property like WordWrap = true, it simply doesn't exists. A solution already has been given on StackOverflow here:
Winforms DotNet ListBox items to word wrap if content string width is bigger than ListBox width?

Related

How to set the scroll position of a WPF RichTextBox to view a specific word or line

I want to write a TTS program that automatically scrolls to the spoken word.
I found a solution for paragraphs. I am not using paragraphs, but I adapt this solution to use the characterIndex of the current word:
TextPointer startTextRange = richtTextselectionView.Document.ContentStart.GetPositionAtOffset(characterIndex);
verticalOffset = startTextRange.GetCharacterRect(LogicalDirection.Forward).Y;
richtTextselectionView.ScrollToVerticalOffset(verticalOffset);
This is the Result:
The Problem is that the text box is scrolling too slow.
I already tried to multiply the verticalOffset with a constant but the behavior seems to be nonlinear...
I am using .Net6.0 and MaterialDesignThemes in the latest version.

Limiting size of input to the size of a TextBox in C#

I've seen quite a few questions about scaling a TextBox to the size of the text, but only found a single question which talked about the reverse here. That question is also from 2010, and I believe the language has evolved since then, and there might be better solutions.
I'll clarify that I do not want to limit my input to an arbitrary number of characters, as the input may include newlines/vertical space. (Which, if it contained a lot of vertical space, could stretch the text beyond the bounds.)
Here's the situation:
Form 1 has a textbox. I want this textbox to remain a fixed size. Any data beyond the size should be cut off from the input.
I want to save Form 1's textbox's contents to a file.
In Form 2, I want to open the file and pull what was Form 1's textbox's contents. These will be saved in a separate textbox local to Form 2.
My strategy right now is to find a way to limit the input to the dimensions of the textbox, so that the textboxes in Form 1 and Form 2 are equivalent, and do not overflow their respective dimensions.
So my question is: How would I go about doing that?
Edit: Sorry, it appears my question didn't provide enough information. I also mis-typed the situation, so I'll re-write it below.
I'll step back and describe more of what I'm trying to do.
Let's assume I have a single Form, with the following two objects:
Textbox
Label
Anything I type into the Textbox I want to see on the Label's text. Input can be any letters, numbers, or special characters, including spaces and newline characters.
For the sake of consistent sizing, I want the label to be of a fixed graphical size; regardless of the number of characters in the label's text, the label object should be no larger than (x, y), where x and y are arbitrary height and width sizes.
I do not want data in the Textbox that cannot fit within the bounds of the label's size. The user should be prevented from entering data into the Textbox that would extend beyond the label's size bounds.
Are there any strategies not mentioned in the linked question that can determine whether a Textbox's text meets or extends past an arbitrary width and height?
The properties of the textbox give you a max length, you can also do it in code like so
var tb = new TextBox();
tb.MaxLength = 10
if you don't want to use that then you should use
var str = tb.Text.Remove(0, 10)
this will only give the str variable the first 10 characters no matter the length of whats actually in the textbox.
For what you want on form two, you need to give me more information about what you want.
edit after OP's edit
if you want the text in the textbox to be matched at the label level you want add a TextChanged Event to the property of the textbox and then have something like this
private void TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
}
having the label to a fixed size no matter what is isn't going to be good, ive done it, but this also begs the question, why are you duplicating your textbox information into a label. Why not just have it in a textbox

How to have a fixed line spacing in a textbox?

I am creating a software for note-taking. I created an image of a notebook with the lines, and etc. Now, I want to create a textbox (or rich text box, or whatever element that works) that will add the text exactly on top of each line.
However, as the user types more than a few lines, the texts gets shifted a little bit on each line, ending up very weird.
I looked through many forums and questions in trying to have this fixed. Nothing helped. I'm developing it in Visual Studio, WPF project, C#. I want to add this control to the C# code, no XAML.
Try Setting the LineStackingStrategy property.
p.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
If you need some spacing before the line also, like on starting of a paragraph then you can try setting the property as:
p.LineStackingStrategy = LineStackingStrategy.MaxHeight;
Check this Post: WPF- "LineSpacing" in a TextBlock
The following should work for line height or paragraph margins:
RichTextBox rtb = new RichTextBox();
Paragraph p = rtb.Document.Blocks.FirstBlock as Paragraph;
p.LineHeight = /* Your height here */;
p.Margin = new Thickness(/* Your height here */);

How to control the space between two labels during run time

Im using Visual Studio 2015 Community C#.
I have two labels on a Windows form suppose Label1 and Label2.
These labels will get filled up with user input namely first name and last name.
How to put even space between them so that during runtime the first name doesn't over lap the last name.
AbrahLincoln Abraham Lincoln
(Label1^)(^Label2) (^Label1) (^Label2)
For example: how to make this ^ INTO that >>>>>>>>>>>>^^
Because if I put space in the Form Design before runtime then for other names It will come like this: John(unnecessary space)Doe
Hope you have understood my problem.
Thanks for your time. :D
Controls are located in a form based on coordinates. Luckily for you these controls have properties that tell you the coordinate for the top, left, right, bottom of a control. So you could dynamically move the right label after setting the text.
Label2.Point = new Point(Label1.Right + 5, Y-coord);
An easier way would be to play about with the labels properties in the designer.
You could also try to anchor label1 to the right and label2 to the left. That way you should have a clean middle line, and as the text grows larger it pushes outwards on does not overlap inwards over each other.
However you need an object to anchor to and luckily the SplitContainer works excellent for this.
Also consider setting the autosize property to off and maxing the widths of the labels. Large enough for the string you expect.
Have you considered making it one label?
As in
theOnlyLabel.Text = $"{dataObject.FirstName} {dataObject.LastName}";
or, if you're using textboxes, something like
theOnlyLabel.Text = $"{txtFirstName.Text} {txtLastName.Text}";
Otherwise, I'm afraid, you'd have to realign these two labels every time your first or last name changes.

Counting lines displayed by richtextbox in C#

In my project I want to count lines displayed by richtextbox in C# with word wrap property set true. For example, in richtextbox, I write a text, and when text reaches the width of the richtextbox, then it automatically goes to second line. So what happens is the text contains actually one line but it is displayed in two lines. So I want to know how can I count this as two lines instead of one? Help will be really appreciated.
You may go for RichTextBox.GetLineFromCharIndex Method
Retrieves the line number from the
specified character position within
the text of the RichTextBox control. If WordWrap is set to false, no portion of the line wraps to the next, and the method returns 0 for the specified character index.
Also, check out this link.

Categories