Adding TextBlock to InkCanvas - c#

for some reason I'm not able to add a TextBlock to my InkCanvas. In my program I have 3 RadioButtons: FreeHand, Eraser and Text. If I select Text RadioButton it's supposed to add TextBlock wherever I click with my mouse (I should be able to add as many TextBlocks as I want). The problem is that nothing happens and if I worked with Eraser before it stays on Eraser even though I selected Text RadioButton. It's like an annotation program. This is the code I tried:
TextBlock tbx = new TextBlock();
tbx.Height = 30;
tbx.Width = 50;
tbx.TextWrapping = TextWrapping.Wrap;
tbx.Margin = new Thickness(5, 10, 0, 0);
c.Children.Clear();
c.Children.Add(tbx);
InkCanvas.SetLeft(tbx, x);
InkCanvas.SetTop(tbx, y);

Well first of all you're not setting the TextBlock's Text field, so you won't see anything. And secondly you're calling SetLeft twice, so it won't be positioned correctly.

Related

Margin in RadioButton Image and Text in Winforms

I'm trying to create a radio button dynamically using .NET 3.5, C#. Here is the code:
RadioButton radioButton = new RadioButton();
radioButton.AutoSize = true;
radioButton.Text = "The majestic text that explored the world";
radioButton.Image = (Image)ResMgr.GetObject("get");
radioButton.ImageAlign = ContentAlignment.MiddleLeft;
radioButton.TextAlign = ContentAlignment.MiddleLeft;
radioButton.TextImageRelation = TextImageRelation.ImageBeforeText;
flowLayoutPanelSelectMonitor.Controls.Add(radioButton);
Here is the result:
There are several problems that I need to fix. Any pointer will be highly appreciated.
I need some margin between the image and the text. How can I set that?
I need radio button to resize so that the text appears in one line. How to do that without setting the width?
The image is clipped at the bottom. How to avoid that without setting the height?

Animate a label

I am trying to animate a label in a WPF application. The label gets created programatically (and dynamically) so it is not defined in XAML, but is created in the C# code.
Animation Story
The label appears in the bottom of the window. The label should be positioned lower than the window, so the user can not see it initially. Then a the label moves up (like sliding) and fades out before it reaches the top of the window.
What I have done
I have implemented this behavior myself in an other project. This time I want to use WPF which should perform better.
So far I have seen there should be multiple ways of doing this. Starting with a DoubleAnimation, going by a PathAnimation and VectorAnimation (the last of which I have not tested successfully).
Encountered problems
The animation works nice with a DoubleAnimation, but there is a problem: When I resize the window, the label gets resized too (similar to an anchor in Winforms). When I make the window smaller the label gets smaller too, until it disappears completely. This effect occurs only in the height of the label. I added the code snippet adding the label. Maybe you find some error. Also there should be a better way to implement this (I personally find it very ugly).
Label lbl = new Label()
{
Content = "Test",
FontSize = 36,
Foreground = new SolidColorBrush(Colors.Red),
Background = new SolidColorBrush(Colors.Black),
HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Top
};
lbl.Margin = new Thickness(0, this.MainGrid.ActualHeight + lbl.ActualHeight, 0, 0);
this.MainGrid.Children.Add(lbl);
UpdateLayout();
Transform myTransform = new TranslateTransform();
lbl.RenderTransform = myTransform;
DoubleAnimation AnimationY = new DoubleAnimation((this.MainGrid.ActualHeight + 20) * -1, TimeSpan.FromSeconds(4));
myTransform.BeginAnimation(TranslateTransform.YProperty, AnimationY);
Questions
As I said, I have found multiple ways that seem to achieve the same behavior. Which one could I use to do this. I still have to do the fade-out on the top of the window, but this animation is easier to do compared to the movement.
The animation is fine. The reason your label is resizing is because you are adding it to a Grid. If you want your label to have a fixed height, then set its Height or MinHeight property.

How to show multiple lines of text in Listbox in Winform?

I have a listbox whose items contain only text. The listbox is having some fixed width. When I add a text which is having more width compare to the lisbox width, I am NOT able to see the remaining text.
Any solution will be helpful.
How about you remove the Width property?
that way the control will expand automatically - as wide as the maximum item
EDIT: Might not be a good idea, BUT:
You could nest the ListBox inside a Panel, and set the Panel's width to a constant value, while having Scrollbars enabled. that way - even if there's a line WIDER than the other lines - the user will be able to scroll.
If you're using WPF, add a text block and use it's text wrapping capabilities.
ListBox lb = new ListBox();
ListBoxItem li = new ListBoxItem();
TextBlock txtBlock = new TextBlock();
txtBlock.Width = 50;
txtBlock.TextWrapping = TextWrapping.Wrap;
...
li.Content = txtBlock;
lb.Items.Add(li);
If not, have a look at this answer for your question.

Can I know width of TextBlock when create it?

I create TextBox on the Canvas like this:
TextBlock t = new TextBlock();
t.Foreground = new SolidColorBrush(...);
t.FontSize = 10;
t.HorizontalAlignment = HorizontalAlignment.Left;
t.VerticalAlignment = VerticalAlignment.Top;
t.Text = ...;
Canvas.SetLeft(t, "f(width)");
Canvas.SetTop(t, ...);
canvas.Children.Add(t);
I want to know width of this TextBlock, because Left coordinate depends on this.
Can I do it? ActualWidth is 0.
Thanks.
Before you add it, call Measure on it, then use the DesiredSize.
Edit: This is OK to do because Canvas does not affect the size of the element once placed. If you added it to, say, a Grid with a fixed-size row, this wouldn't give you the real Height once added since the adding to the Grid would change it.
As Mario Vernari points out, if you have real complex positioning needs it's pretty easy to override ArrangeOverride (and sometimes MeasureOverride) and make a custom panel. Canvas is actually written this way, as is StackPanel, etc. They are all just specific-measuring and arranging panels, and you can make your own, too.

WPF Border Property not working correctly

I am working on an application that is using Drag and Drop functionality. When I drag certain items over other items, I would like to show if they are towards the top, or the bottom of the item by setting the border property of the item they are over dynamically(as oppose to in the xaml).
I have looked into this, and used examples, but none of them are working. Here is the code I have that is called when a drag operation is performed over the Grid Item.
private void Grid_DragEnter(object sender, DragEventArgs e)
{
Grid grid = (Grid)sender;
grid.Background = Brushes.Cornsilk;
Border border = new Border();
border.BorderBrush = Brushes.Black;
border.BorderThickness = new Thickness(5, 10, 15, 20);
border.Background = Brushes.Black;
border.Padding = new Thickness(10);
border.Child = grid;
}
The code above is only test code so that I can see the border is showing. Once I can get this, then I will set the top or bottom border at separate times, depending on if they are above or below the center of the grid item.
You're setting the border's child, but the border itself needs to be added to some container in order for it to be visible.
That being said, you might want to look into Adorners. They are designed for exactly this scenario, and don't require changing the visual hierarchy of your UI at runtime.

Categories