Showing preview of Font Size in C# - c#

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.

Related

Set MinimumSize according the controls residing inside form

Title of my question could be make it look like a duplicate but please read ahead as my problem is a bit different.
I am trying to replicate the minimum size functionality of some popular media players like MPC-HC or VLC where when you try to make it small the minimum size it achieves is when only MenuStrip and Player Controls are visible.
The code I've written to attain this is:
public NewMain()
{
InitializeComponent();
int ClientTop = RectangleToScreen(ClientRectangle).Top;
int height = menuStrip1.Height + panel1.Height + ClientTop - Top;
label4.Text = height.ToString();
MinimumSize = new Size(373, height);
}
The problem is that when it runs, its not working perfectly and the menuStrip1 is still getting blocked a little at bottom from the panel1 (Docked at bottom) where the player controls will be placed.
Below is the Image of what I was able to attain with above code.
Next Image is what I expected:
Note that label on left updates when resize the form and the label on the right is the determined height via code.
My Idea was to add the difference of Form's Top and the Top of total rectangle visible on the screen i.e. the height of the title bar otherwise the resulting height will be even smaller and hide the menuStrip1 completely. I don't want to hardcode any values because it'll make the interface less adaptable to the changes that I might be doing later on.
To correctly determine the minimum height in this case is to keep the calculations relative which can be attained by:
int height = Height - (panel1.Top - menuStrip1.Bottom);
All credit goes to Hans Passant who provided this code. I'm just posting it as an answer to mark my question solved. Thank you.

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.

TextBox.TextAlign right-side alignment has no effect in certain conditions?

I have a path selector in my Visual C# Express 2010 form application.
I do it using a FolderBrowserDialog and a (single line) TextBox, to show the selected path. Using the following line in my UI refresh code.
this.textBoxFolder.Text = this.folderBrowserDialog1.SelectedPath;
The ReadOnly property is set to true and TextAlign property is set to Right using the form designer, because the selected path is often longer than the TextBox, and I prefer to show the right-side of the path. The forms designer generates this:
//
// textBoxFolder
//
this.textBoxFolder.Location = new System.Drawing.Point(40, 72);
this.textBoxFolder.Name = "textBoxFolder";
this.textBoxFolder.ReadOnly = true;
this.textBoxFolder.Size = new System.Drawing.Size(160, 20);
this.textBoxFolder.TabIndex = 13;
this.textBoxFolder.TabStop = false;
this.textBoxFolder.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
Whenever the chosen path is shorter than the textbox size, the Right alignment works. (But this is not really important)
Whenever the chosen path is longer than the textbox size, the Right alignment has no effect, the string in the textbox is displayed such that the left-most character is visible, and right-most are hidden.
I know that in a normal single line TextBox (ReadOnly = false), when an overly-long string is typed in by hand, the right most chars are visible, even when focus goes away, regardless of whether TextAlign is set to Left / Right / Center!
In other words, my goal is, when TextBox.Text is programmatically set (as opposed to typed in), and the string is longer than the width of the TextBox, how do I get the right-most chars to be visible?
Instead of setting the TextAlign property, you should move the caret to the last character:
textBoxFolder.Text = this.folderBrowserDialog1.SelectedPath;
textBoxFolder.SelectionStart = textBox1.Text.Length - 1;
Setting SelectionStart actually moves the caret to the specified position. And that makes the character at that position visible in the TextBox.
If you can use a Label instead of a text box, you can use the one created by Hans Passant here that uses TextFormatFlags.PathEllipses flag while drawing the text.
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: In Pocket PC-based applications, a single-line text box supports only left alignment. A multiline text box can be aligned on the left, right, or center.

Getting a text area in the system tray

i was just wondering what i need to research to be able to have a programme that is in the system tray, when the user clicks the programme icon, just above the system tray a small text area appears allowing the user to type in a search condition. There is plenty of resources for c# and getting your programme in the system tray, but then it just opens as normal, which is not quite what i am looking for.
Thanks
One way to accomplish this is to use a standard WinForms window which contains a single text box and has no border. This window can then be displayed and positioned as normal (likely using many of the existing samples) but will appear as a floating text box.
var form = new MyTextBoxForm();
form.FormBorderStyle = BorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
// position the form
form.ShowDialog();
Handle the NotifyIcon.Click event and show your form in the desired location.
For example:
var screen = Screen.PrimaryScreen;
form.Left = screen.WorkingArea.Right - form.Width;
form.Top = screen.WorkingArea.Bottom - form.Height;
Maybe with this Make your program in the system + add a menu you could try editing the menu, like you'd do a regular menu with toolstrips.... and change the label by a textbox.
Just a random idea.

C# Printing Inconsistent

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.

Categories