UWP - How to create a TokenAutoComplete Control - win-universal-app

I am developing UWP (Win10 - VS2015) App. I need a Token TextBox in Windows Platform. Any idea please, how to start and create this control, then when writing text inside the Textbox and put space or just tap that text, it should convert into selected Token. See the pic (its just for idea). I need such type of control.
You can also get idea from this Post TokenAutoComplete

The code which I'm posting is initial code you can start builting control with..
I used RichTextBlock and Textbox. If you put these two controls in WrapPanel inside the Gridview. You might get similar control which you wanted but I haven't tried it.
<RichTextBlock x:Name="tokenblock">
<Paragraph>
</Paragraph>
</RichTextBlock>
<TextBox TextChanged="TextBox_TextChanged"/>
Code behind is like this
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
string text = (sender as TextBox).Text;
if (text.Contains(';'))
{
Paragraph para;
text = text.Substring(0, text.LastIndexOf(';'));
if (tokenblock.Blocks.Count > 0)
para = tokenblock.Blocks[0] as Paragraph;
else
para = new Paragraph();
InlineUIContainer inline = new InlineUIContainer();
Border br = new Border();
br.Background = new SolidColorBrush(Colors.Gray);
br.CornerRadius = new CornerRadius(10);
TextBlock tb = new TextBlock();
br.MinWidth = 70;
br.MaxWidth = 150;
tb.Text = text;
tb.TextWrapping = TextWrapping.Wrap;
tb.Margin =new Thickness(10, 10, 10, 10);
br.Child = tb;
inline.Child = br;
para.Inlines.Add(inline);
(sender as TextBox).Text = "";
}
//below codes I haven't tried
<GridView x:Name="myGridView" IsItemClickEnabled="True">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="5"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
//here you have to put RichTextBlock and textbox as two gridview items

Related

Composition animation on a visual sub channel

I use the new Composition Animation API in UWP (Windows 10 1607) to animate a custom control. I want changes of the vertical offset of its children to be animated, but not changes to the horizontal offset.
I can animate the whole offset (X and Y) like this:
var offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = "Offset";
offsetAnimation.Duration = TimeSpan.FromSeconds(0.5);
offsetAnimation.InsertExpressionKeyFrame(1.0f, "This.FinalValue");
animationCollection["Offset"] = offsetAnimation;
var visual = ElementCompositionPreview.GetElementVisual(child);
visual.ImplicitAnimations = animationCollection;
I expected this code to only animate the Y offset of the child, but it doesn't display any animation at all:
var offsetAnimation = compositor.CreateScalarKeyFrameAnimation();
offsetAnimation.Target = "Offset.Y";
offsetAnimation.Duration = TimeSpan.FromSeconds(0.5);
offsetAnimation.InsertExpressionKeyFrame(1.0f, "This.FinalValue");
animationCollection["Offset"] = offsetAnimation;
var visual = ElementCompositionPreview.GetElementVisual(child);
visual.ImplicitAnimations = animationCollection;
Setting Target to "Offset.y" or the trigger to "Offset.Y" doesn't help.
I haven't found an example using visual sub channels in the documentation, so I would appreciate any help on how to only animate the y channel of the offset.
According to your code snippet, you set a Offset trigger for the animation. That means once the offset of child is changed, this animation will started. And you set 'This.FinalValue' for the ExpressionKeyFrame, so it will bind the value you set to the new offset and it will animate to that new value.
So we if we just set the new offset to only have y value changed (let x and z be same as the old one), the animation will only animate the y channel.
Code like follows:
<TextBlock x:Name="txtanimation" Text="animation test" Height="30" Width="100" Foreground="Red" ></TextBlock>
<Button x:Name="btnanimate" Click="btnanimate_Click" Content="Animate y" Margin="60"></Button>
Code behind:
Visual visual;
public MainPage()
{
this.InitializeComponent();
visual = ElementCompositionPreview.GetElementVisual(txtanimation);
Compositor compositor = ElementCompositionPreview.GetElementVisual(txtanimation).Compositor;
ImplicitAnimationCollection animationCollection = compositor.CreateImplicitAnimationCollection();
Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = "Offset";
offsetAnimation.Duration = TimeSpan.FromSeconds(2.5);
offsetAnimation.InsertExpressionKeyFrame(1.0f, "This.FinalValue");
animationCollection["Offset"] = offsetAnimation;
visual.ImplicitAnimations = animationCollection;
}
private void btnanimate_Click(object sender, RoutedEventArgs e)
{
visual.Offset=visual.Offset+ new System.Numerics.Vector3(0f, 400f, 0f);
}
And the result:
More samples please reference WindowsUIDevLabs. And more details you can reference videos like Implicit Animations.

how to avoid checkbox when we press space bar in tree control?

I am having a tree control where initially I had set it style in OnInitDialog as follows,
BOOL OnInitDialog()
{
CPropertyPage::OnInitDialog();
//Setting Treecontrol with TVS_CHECKBOXES style
HWND m_hTreeWnd = ::GetDlgItem(m_hWnd,IDC_TREE);
DWORD dwStyle = GetWindowLong(m_hTreeWnd,GWL_STYLE);
dwStyle |= (TVS_CHECKBOXES);
SetWindowLongPtr(m_hTreeWnd,GWL_STYLE,dwStyle);//CTreeCtrl m_hTreeWnd;
//Now I had initialized the tree control and I am want only few items of the tree to have
//checkboxes,In order to achieve that I did it as follows(Removing checkboxes where not required)
tvInsertItem.hParent = NULL;
tvInsertItem.hInsertAfter = TVI_ROOT;
tvInsertItem.item.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvInsertItem.item.pszText = L"Name"
hParentItemHandle = m_TreeCtrl.InsertItem(&tvInsertItem);
//Removal of checkboxes for the above item
tvItem.hItem = hParentItemHandle;
tvItem.mask = TVIF_TEXT|TVIF_STATE|TVIF_SELECTEDIMAGE;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
tvItem.state = 0;
tvItem.pszText = szCommonModel;
m_TreeCtrl.SetItem(&tvItem);
}
Everything is fine I removed checkbox in which ever the node it is not required.And having checkbox where they are required.
All of a sudden I noticed this issue i.e, after selecting an item in the tree control which does not have checkbox ,now if
I press "space bar"then a checkbox is getting append to that item.
I want avoid the checkbox when I press space bar.
To avoid checkbox I tried this but did not work.
BOOL CTreeControlDlg::OnTvnItemChangingTree(UINT i,NMHDR *pNMHDR, LRESULT *pResult)
{
NMTVITEMCHANGE *pNMTVItemChange = reinterpret_cast<NMTVITEMCHANGE *>(pNMHDR);
HTREEITEM hTree = pNMTVItemChange->hItem;
UINT ChangItem = pNMTVItemChange->uStateNew;
UINT ChangItem1 = pNMTVItemChange->uStateOld;
UINT ItemState = m_TreeCtrl.GetItemState(hTree, TVIS_STATEIMAGEMASK);
if(98 == ItemState)//98 is the thing I observed while debugging this is not correct I know.
return FALSE;
return TRUE;
}
I am bit confused how do we get the state image mask TVIS_STATEIMGAEMASK and how do we make that check whether the item has this mask or not .
Can anyone please suggest me a way to acheive this.

Regarding bullets and levelled bullets in ms word using c#

I have a ribbon button which inserts numbered bullets in my doc like so:
private void button4_Click_1(object sender, RibbonControlEventArgs e)
{
Word.ListGallery listGallery = Globals.ThisAddIn.Application.ActiveDocument.Application.ListGalleries[Word.WdListGalleryType.wdOutlineNumberGallery];
oPara = oDoc.Content.Paragraphs.Add(range);
listFormat = oPara.Range.ListFormat;
this.ApplyListTemplate1(listGallery, listFormat, 1);
range.ListFormat.ListLevelNumber = 1;
}
and I have another button which applies levelled numbered bullets like 1.1 ,1.2 etc like this
private void button5_Click(object sender, RibbonControlEventArgs e)
{
Word.ListGallery listGallery = Globals.ThisAddIn.Application.ActiveDocument.Application.ListGalleries[Word.WdListGalleryType.wdOutlineNumberGallery];
oPara = oDoc.Content.Paragraphs.Add(range);
listFormat = oPara.Range.ListFormat;
this.ApplyListTemplate1(listGallery, listFormat, 2);
oPara.Range.ListFormat.ListLevelNumber = 2;
}
Here is my apply list template:
private void ApplyListTemplate1(Word.ListGallery listGallery, Word.ListFormat listFormat, int level = 2)
{
listFormat.ApplyListTemplateWithLevel(
listGallery.ListTemplates[level],
ContinuePreviousList: true,
ApplyTo: Word.WdListApplyTo.wdListApplyToSelection,
DefaultListBehavior: Word.WdDefaultListBehavior.wdWord10ListBehavior,
ApplyLevel: level);
}
The problem:
Here if I click on the level 1 button it inserts a numbered bullet , like "1." and I click on second button, it inserts "1.1.", and now if I press the first button to insert "2 ." it inserts fine, but the second button click does not insert 2.1 ,instead it inserts "1.2" Have been stuck on it for days.
Instead of using
oPara.Range.ListFormat.ListLevelNumber = 2;
I used oPara.Range.ListFormat.ListIndent();
and oPara.Range.ListFormat.Outdent() instead of oPara.Range.ListFormat.ListLevelNumber = 1;
and it worked without bugs.

Android Spannable dismiss after editing

I am implementing mentions as in twitter #username. But mentions can't be typed, only selected from MultiAutoCompleteTextView.
I am creating spannable with blue text color on treminateToken
Spannable sp = new SpannableString(text + " ");
sp.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
But the problem is that user can edit this text.
How to make this snap non editable? To delete whole span when user press backspace?
Or at least change color back to black if it have been edited.
Try this
Pass your string to this below method using key listener of your editext
public void displayText(String s)
{
String ss[]=s.split(",");
for(int i=0;i<ss.length;i++)
{
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createContactTextView(ss[i]);
BitmapDrawable bd = (BitmapDrawable) GlobalMethods.convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
sb.append(ss[i] + ",");
sb.setSpan(new ImageSpan(bd), sb.length()-(ss[i].length()+1), sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
contact.setText(sb);//your edit text here
}
}
set text style
public TextView createContactTextView(String text)
{
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextSize(25);
tv.setBackgroundResource(R.drawable.design);
return tv;
}
Output:
if the user try to delete the text it delete the whole text

How to assign a string id to UI elements.

I'm developing an app on android and I am generating UI elements in a loop. But I need these elements to have an id with letters and numbers, for example "rl1" or "rl2". I was trying to use the method RelativeLayout.setId() but, that method only accepts int. Is there a way I can set an ID as I want without being limited to numbers?
Thanks.
Here is the code I am trying to make work.
for (int i=1; i < 10; i++)
{
//gets the frameview where the elements will be created.
String LinearLayoutId = "frameview1";
int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
LinearLayout linearLayout = (LinearLayout)findViewById(resID);
//creates the RelativeLayout that will hold the ImageIcon and the TextView
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,40 );
rl.setLayoutParams(lp);
rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));
//creates the image icon within the layout at the left side
ImageView image = new ImageView(this);
lp = new RelativeLayout.LayoutParams(
40,RelativeLayout.LayoutParams.MATCH_PARENT );
image.setLayoutParams(lp);
String imageicon = "icon_"+i;
resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
image.setImageDrawable(getResources().getDrawable(resID)); //sets the icon
rl.addView(image); //adds the ImageView to the relative layout
//creates the TextView within the layout with a 40 margin to the left
TextView tv = new TextView(this);
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
lp.setMargins(40, 0, 0, 0);
tv.setLayoutParams(lp);
String textViewID = "tv"+i;
resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
tv.setText(getResources().getString(resID));
tv.setTextColor(Color.BLACK);
tv.setTextSize(25);
rl.addView(tv);//adds the TextView to the relative layout
rl.setOnClickListener(mAddListener);
linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}
and then I have the OnCLickListener like this...
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v){
Intent intent;
Bundle bundle;
String id = getResources().getResourceEntryName(v.getId());
id = id.replaceAll("\\D+","");
int value = Integer.parseInt(id);
intent = new Intent(ERS.this, ShowInfo.class);
bundle = new Bundle();
bundle.putInt("key", value);
System.out.println(v.getId());
intent.putExtras(bundle);
startActivity(intent);
}
};
I have tried to set up numeric IDs, but then when I Look for them with:
String id = getResources().getResourceEntryName(v.getId());
It can't find them.
I had all of this in an xml file to begin with, but it was really long because there are about forty items in the list, and it was complicated for me to go and change a letter for example in all of them. I came up with this idea to generate them at runtime in a for loop. I am testing in the meantime with ten, but I can't get it to work.
If I am doing something incorrect, then pardon me, but I am new to this.
You may still find it easier to go back to XML layouts and use the R class to generate meaningful IDs. Although as you haven't included the original xml file you refer to at the end of the question, so I can only guess at the problem you had with it. It does seem to fit the bill though, and would allow you to create something along the lines of:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/hellotextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi there"/>
The android:id="#+id/hellotextview" generates an id that can be used elsewhere in your project. In your java code you could access that specific TextView with something similar to:
TextView helloText = (TextView) findViewById(R.id.hellotextview);
The R.id.hellotextview is a int automatically generated when the project is built (in gen/R.java), but as you get to pick the name you can assign them something relevant to you and your project. So instead of trying to use strings values such as "rl1" and "rl2" that you mentioned, you could use R.id.rl1 and R.id.rl2.
As well as individual UI elements, you can also use the same technique for strings (in res/values/strings.xml), and other resources stored under the project's res/ folder, such as icons, media files, etc. In the case of strings you would access them getString(R.string.some_name_given_by_you);
See Accessing Resources at the Android Developers site for more info.
Why dont you try using SharedPreferences as an alternative in case you want to access the elements which you give some ID elsewhere in some other activity.

Resources