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.
Related
I'm trying to add .ico 48x48 image before text in WinForms button 87x30 size:
button1.BackgroundImageLayout = ImageLayout.Stretch;
button1.BackgroundImageLayout = ImageLayout.None;
button1.BackgroundImageLayout = ImageLayout.Zoom;
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;
Result is:
I'm trying to figure out, how to align image on the left side with text on related distance, like this:
edit:
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleLeft; /// MiddleRight; // MiddleCenter;
button1.ImageAlign = ContentAlignment.MiddleRight; /// MiddleLeft;
Result:
The background image property is like the operating system desktop background, it is a wallpaper, that can be stretched, adapted, repeated...
Therefore here you don't need to use BackgroundImage for a button icon style image associated to its text.
If you use the Image property and set alignments to left for it and right for text, all works fine:
Then you can adapt these alignments as well as width and height to the desired result depending on the image size and/or text size and length.
Also, as indicated by the duplicate I finally found, to simply center all, you can use the TextImageRelation and set it to TextImageRelation.ImageBeforeText without changing alignments, and if necessary by increasing the height according to the size of the displayed image to have a clean result:
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?
I am trying to create an application wherein you can write text in a textbox, chose the font, color, etc.
All of this will be shown in a preview box next to it and when the user clicks "Go Live", the same content from the preview box will be shown fullscreen.
There comes the problem.
If the user types "Hello World" and choses font-size as "8", the preview box might show that the text is taking up the whole screen. But when I click "Go Live", the font-size on the fullscreen is still 8 and the text looks very tiny.
I figured that I will somehow have to find a ratio of the sizes of the preview box and the screen resolution and map the font-size from the preview to the fullscreen. The following algorithm seems to work :
multiply_factor = (int)monitor_width/preview_box_width;
add_factor = (int)(1st digit of the decimal part of the above division);
live_Screen_font_size = (preview_font_size * multiply_factor) + add_factor;
example:
If screen resolution width is 1366px and preview box width is 310px;
=> 1366/310 = 4.406
multiply_factor = 4;
add_factor = 4;
preview_font_size = 8;
live_Screen_font_size = (8*4)+4 = 36;
This seems to work but not as well as I want it to. Also its obviously not correct as I haven't considered the screen height as well.
Is there a correct way for doing this?
Or is there another way to do this in C#?
Please also suggest appropriate tags for this question so that the correct people will find it.
Thanks in advance.
I have just started learning C# and Visual Studio trying to work both on books and on sample code.
I am aware that tis is not a very brilliant question, but this is the problem that I am trying to solve. I have a windows form and I need to show an image in a Picture box contained in a tableLayoutPanel. The simple problem is that the images I have to load could have several sizes and a typical image is not completely shown within the allocated space: only the area that fits the container is shown, the rest of the image is cutted off. I have to show the image in its entirety, i do not have to resize it. I have already set the autosize property, but this does not seems to work.
Here some code in the form.cs
string imageName = openFileDialog1.FileName; // Get the image name
// Read the image
try
{
img = ( Bitmap) Image .FromFile(imageName);
}
catch
{
MessageBox.Show("oooops" , Text, MessageBoxButtons.OK, MessageBoxIcon .Hand);
}
pictureBox1.Image = img; // show the image
and then in the private void InitializeComponent() found in the form.designer.cs:
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
...
this.pictureBox1 = new System.Windows.Forms.PictureBox();
...
this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 1, 1);
...
this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 1, 1);
...
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.034863F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 2.388038F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 88.5771F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(784, 762);
...
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
Have you any hint about how to show the whole image?
Even using slidbars would be ok, but notwhithstanding the fact that the container has autoscroll = true, nothing happens and the image is still truncated.
Thanks for any help
The autoscroll property in the tableLayout control manage the scrolling of the whole table, including all the child controls. When the image is too big to fit within the picture control box, the autoscroll = yes property show slidebars allowing to slide all the content packed in the table layout control, not the individual image cell. Picture box does not have an autoscroll property, because autoscroll is, as far as I understand, a container's property; to slide the image in its own assigned space an intermediate container should be used, I guess.
My question was not a good question. It is rooted in my confusion about the containment hierarchy and relevant properties, more than in a genuine lack of knowledge or notions. Well, there is always room to improve ...
I have a form on which I have a number of textboxes. I wish to print the text from these textboxes in the locations they are on the form. It is printing at the moment using the code below. However, the text prints differently on different printers (on some it prints just right, on some too high, etc). It is being printed on a pre-printed form with spaces for the text so it needs to be fairly exact. What am I missing to make it print the same on every printer?
public void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Panel curPanel = this.FormPanel;
Graphics g = (Graphics)e.Graphics;
Pen aPen = new Pen(Brushes.Black, 1);
// Cycle through each control. Determine if it's a checkbox or a textbox and draw the information inside
// in the correct position on the form
int xLocation, yLocation;
for (int j = 0; j < curPanel.Controls.Count; j++)
{
// Check if its a TextBox type by comparing to the type of one of the textboxes
if (curPanel.Controls[j] is TextBox)
{
// Unbox the Textbox
TextBox theText = (TextBox)curPanel.Controls[j];
// Draw the textbox string at the position of the textbox on the form, scaled to the print page
xLocation = theText.Bounds.Left;
yLocation = theText.Bounds.Top;
g.DrawString(theText.Text, theText.Font, Brushes.Black, xLocation, yLocation);
}
}
}
The problem is that you ignoring how the text is aligned inside the control. Default alignment is roughly equal to StringFormat.Alignment = StringAlignment.Center, it can be changed for buttons and check boxes with their TextAlign property. You'll need to use the DrawString() overload that takes a Rectangle and a StringFormat. Note that TextBox is tricky, you might still be off by a few pixels.
Take a look at Control.DrawToBitmap() for a completely different approach.
I'm wondering if maybe the problem is discrepencies in how different printers pull in the paper. The text is off by a maximum of half an inch between printers. I was hoping this wasn't the case because if so I will just have to tailor my application to the client's particular printer (not ideal). Has anyone else run into this situation?
This is most likely a combination of two things:
You need to explicitly set up the page margins/boundaries. Various printers will have default margin and page size settings. Use a PageSetupDialog to help you out. If you want consistent printing, you can make the margins constant, but page size should be the responsibility of the user (and then check to make sure your margins actually fit on the page!).
The text needs to be placed on the page in relation to the page boundaries. I know your comment says that it will be, but it doesn't look like that it is actually implemented in your code. Setting the OriginAtMargins (on your PrintDocument control) to true helps immensely with this.