Margin in RadioButton Image and Text in Winforms - c#

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?

Related

how to change backcolor of the countrol in infragistics control windows forms

I am having the requirement of changing back color of the Infragistics i have created a new ultracheckeditor and added to panel now when I try to change the back color of the ultracheckeditor to panel color i am unable change the color
appearance1.BackColor = System.Drawing.Color.Transparent;
appearance1.BorderColor = System.Drawing.Color.Transparent;
this.ultrabutton.Apperance=apperance1;
ImageSample
You have to use the Apperance property and make sure the UseOsThemes property is False:
ultraButton1.UseOsThemes = DefaultableBoolean.False;
ultraButton1.Appearance.BackColor = Color.Aqua;
The issue can due to AppStyling used in the container by setting UseAppStyling to false worked:
this.ultrabutton.UseAppStyling = false;
If this and the aforementioned UseOsThemes settings do not help, be sure that the button is not rendered using a bitmap image. If that's the case, you'll need to address the problem by have different-colored versions of the bitmap or, if transparency was used, you can change the color behind the rendered bitmap so that it shows through as desired.

Stacking different elements in a Windows.Forms.Button

I am developing a windows forms application in c#. I am creating a large amount of buttons in loops, and I wish for both an image (icon), and text to be displayed on a button. I have experimented with alignment, but I require the image to be on the very top of the Button, and the text to be below the image. My current code is:
button1.Image = im;
button1.ImageAlign = ContentAlignment.TopCenter;
button1.Text = "CS: GO";
button1.TextAlign = ContentAlignment.MiddleCenter;
This produces this image, which is clearly not what I want:
I cannot resize the Button, as the text is user defined, and subject to change in length.
Try the following:
button1.TextAlign = ContentAlignment.BottomCenter;
if this doesn't fit your needs there's another Property:
button1.TextImageRelation = TextImageRelation.ImageAboveText;
if I am right this will override some of your alignments.

Adding TextBlock to InkCanvas

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.

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.

Categories