Text - one label for every single word - c#

My goal is to display text with feature, that you can tap on any word of displayed text and it will do something with this selected word.
I choose the label approach. It means that every word in text will be in it's own label. And then I can rise tapped event on that label.
But I can't find out how to format that "group" of labels into "nice" formated text.
My idea was:
private void ReadPageViewModel_ReloadPageEvent(string text)
{
MyLayout.Spacing = 2; //MyLayout is StackLayout
MyLayout.Orientation = StackOrientation.Horizontal;
//MyLayout.HorizontalOptions = LayoutOptions.EndAndExpand;
var splitedtText = text.Split(';');
foreach(var item in splitedtText)
{
MyLayout.Children.Add(new Label {Text = item , HorizontalOptions = LayoutOptions.FillAndExpand });
}
}
Every try results in wrong formated text.
Could you give me some advice? Am I using wrong layout? Or is there some easy trick that I don't know?
EDIT:
Screenshots:
This is just text inserted into label, this is what I want to achieve with 1 label for every word in this text.
One of many tries (result of sample above)

Thanks for your time. I found answer. Something like wrappanel was what I was looking for. But xamarin.forms doesn't have one so I had to help myself.
I found this wrappanel classes
https://gist.github.com/NicoVermeir/7ffb34ebd639ed958382 and
https://github.com/conceptdev/xamarin-forms-samples/blob/master/Evolve13/Evolve13/Controls/WrapLayout.cs
Pick one wrappanel class and include it in project. Then add namespace to your view and it is all.
xmlns:controls="clr-namespace:SecretProject75.Controls;assembly=SecretProject75"
<controls:AwesomeWrappanel Grid.Column="1" x:Name="MyWarappicek" Orientation="Horizontal"></controls:AwesomeWrappanel>

Related

How to change textbox value by selecting suggested value?

I'm creating a Desktop App in Visual Studio With C#.
I've a String array called Tag_Word_Box. In a textbox if I type something, then it will be show suggest words from Tag_Word_Box array.
In briefly, assume that- I've 5 words respectively [aabc] [abc] [abd] [abcg] [bcd] If I type in textbox only 'a' then it will be show all of words without 'bcd'.
If I select 'aabc' or press enter one of suggesting words, then- it will be assign whole word in textbox, that means textbox value will be changed with selecting word by 'a'.
BTW, I know that- it will be solved by trie algorithm to find out words. But I want to know that how to do that operation in visual studio respect of C# that-
1. to show suggesting words when I type something
2. and how do I change textbox value by selecting from those?
Thanks :)
All textboxs have an 'AutoCompleteSource' property. Set it to CustomSource from the Properties toolbar. Then set the 'AutoCompleteMode' property to SuggestAppend. Now, in the code, add this to the TextChanged Event of the textbox:
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
To do the complete thing in code, add this to the TextChanged event of your textbox:
textBoxName.AutoCompleteSource=AutoCompleteSource.CustomSource;
textBoxName.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
var autocomplete = new AutoCompleteStringCollection();
autocomplete.AddRange(Tag_Word_Box);
textBoxName.AutoCompleteCustomSource = autocomplete;
Remember to replace textBoxName by the name of your textbox before using this code.

How can I see "rowcount" (?) of a textlabel in xamarin forms?

I have a label in my XAML that I bind to my data and sometimes the text is longer than others and if the text is long enough to get into 2 rows, i want to adjust my code a little bit to make the UI look better.
Is there a way to solve that in xamarin forms?
XAML
<Label x:Name = "title" />
CODE
title.Text = Title; //title is a string cointaining the text.
So do something with:
if(title.text > 2 (rows?))
{
//change the ui.
}
Or if there is a solution that sees if a label doesnt fit on a the row. so if i have a absolutelayout that only allows 1 row and the label recieves the "..." because it all cannot fit. can you do something with that?
if (title.Text.Contains == "...")
{
// change size of label
}
No you can't do that like that, as xamarin forms uses "plaftorm renderers", specific to each platform, to render your string.
In fact you have no way to know if your label will split on more than one line. What you can do is count the number of chars that can fit on one line on most devices, and customize your rendering based on this value.
If you still want to compute the number of lines, you can do it using specific platform code that needs the width in point of your label. For example on iOS you will use NSString GetSizeUsingAttributes like described here: https://forums.xamarin.com/discussion/10016/measuring-string-width-getsizeusingattributes

Change ToolStripDropDownButton Text by Selecting Item

I have been stuck on this problem for awhile now, and I was hoping for some direction or an answer. How do you change the text of a ToolStripDropDownButton based off of a selected item within itself?
I have a grid that is to be displayed, but I want to change the size of the grid on the fly. By selecting the ToolStripDropDownButton and selecting a predetermined list of grid sizes, the text of the ToolStripDropDownButton should change to show the user the new grid size.
Any answer is appreciated.
I have found the answer to my question, thank you all for the answers.
private void changeGridSizeDropDownButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem != null)
{
changeGridSizeDropDownButton.Text = e.ClickedItem.Text;
// This line converts my list gridsize options like 64, 32, and 16
// and Parses that text into the new selected gridsize.
gridSize = Int32.Parse(e.ClickedItem.Text);
}
}
It's easy you'll see.
myToolStripDropDownButton.DropDownItems[0].Text = "New grid size";
The index[0] is the first item in your DropBox just change it to access other items.
This is to change the name of the items.
If you need to change Text somewhere on the ToolStrip independently from your DropDownButton check what item is selected than change
myToolStrip.Items.Add("DefautlGridSize");
myToolStrip.Items[0].Text("NewGridSize");
If you don't need the ToolStrip especially I would consider using a ComboBox with a Label which would do the job much more simply.
Hope this helped

ListBox and CustomTabOffsets.Add

I have created a listBox in a form and I'm trying to find a way to add things like name, phonenumber, city in rows but with each name, phonenumber and city i columns. After some searching I found the CustomTabOffsets.Add, but I don't get it how this work and I can't find any tutorial that explain how this work. Is there anyone here that can help me understand this? I guess there is better options for this, but listBoxs is a must in this task. Thanks!
In my UpdateGUI method I only have this to add a name to the listBox
lstSeats.Items.Add(inName);
You need to use ListView for this. Not ListBox
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("SubItem");
listView1.Items.Add(lvi);
A bit too late, but - how to make more than 2 column's in ListBox using C#?
lstSeats.CustomTabOffsets.Add(12); // "The integers represent the number of quarters of the average character width for the font that is selected into the list box."
lstSeats.UseCustomTabOffsets = true;
lstSeats.Items.Add("a\tb); // tab character between the items
The custom tab offset integers are quarter of the average character width for the selected font https://support.microsoft.com/en-us/kb/318601

ListView text is truncated -- how do I display the entire text of an item?

Here's a picture of a System.Windows.Forms.ListView using LargeIcons
The selected item shows all it's text
e.g. The top left item shows only 11 characters of its name, it's shown fully if that's selected. How can I make it show all the text(or atleast more than 11 characters), for items that's not selected ?
There is DrawItem event http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem.aspx. Inside this event you have access to System.Drawing.Graphics.DrawString method.
void view_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.Graphics.DrawString(e.Item.Text, drawFont, Brushes.Black,
new RectangleF(e.Item.Position.X,
e.Item.Position.Y,
20,
160));
}
I entered some values for width/height, but you should use MeasureString or similar method.
Also do not forget to set OwnerDraw=true on ListView, otherwise it will not work.
I had this problem a while ago and solved by inserting a space after x characters and/or before a capital letter, so the text is wrapped where I wanted it to. This is somewhat a hack but it's an easy fix. You of course need to keep track of the changed you made to the text so you can reverse it if the user selects the item for further use.
Caveat: this worked for me when the View type was Details, I haven't tried LargeIcons.
After writing output (mine was in bulk) to the listview add the following:
lstResults.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize);
lstResults.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);

Categories