Exemple #1
0
 public static void SetTabWidth(TextBox textbox, int tabWidth)
 {
     System.Drawing.Graphics graphics = textbox.CreateGraphics();
     var characterWidth = (int)graphics.MeasureString("M", textbox.Font).Width;
     SendMessage(textbox.Handle, EM_SETTABSTOPS, 1,
                 new int[] { tabWidth * characterWidth });
 }
        /// <summary>
        /// Automatically resize font in single line textbox based on
        /// textbox size and length of sample string
        /// </summary>
        /// <remarks>
        /// Based on https://stackoverflow.com/a/38932706 but fixed/simplified.
        /// Must set AutoSize = false on textbox and anchor it to container
        /// so it resizes. A bit of a hack, but it works.
        /// </remarks>
        /// <param name="tb">Textbox</param>
        /// <param name="s">String to use as reference for width</param>
        public static void FontAutoResize(this System.Windows.Forms.TextBox tb, string s)
        {
            if (s.Length > 0)
            {
                SizeF stringSize;

                using (Graphics gfx = tb.CreateGraphics())
                {
                    // Get the size given the string and the font
                    stringSize = gfx.MeasureString(s, tb.Font);

                    double areaAvailable = tb.Height * tb.Width;
                    double areaRequired  = stringSize.Width * stringSize.Height * 1.1;

                    if (areaAvailable / areaRequired > 1.2)
                    {
                        while (areaAvailable / areaRequired > 1.2)
                        {
                            tb.Font      = new Font(tb.Font.FontFamily, tb.Font.Size * 1.1F);
                            stringSize   = gfx.MeasureString(s, tb.Font);
                            areaRequired = stringSize.Width * stringSize.Height * 1.1;
                        }
                    }
                    else
                    {
                        while (areaRequired * 1.2 > areaAvailable)
                        {
                            tb.Font      = new Font(tb.Font.FontFamily, tb.Font.Size / 1.1F);
                            stringSize   = gfx.MeasureString(s, tb.Font);
                            areaRequired = stringSize.Width * stringSize.Height * 1.1;
                        }
                    }
                }
            }
        }
 public static System.Windows.Point GetDpi()
 {
     using (var textBox = new System.Windows.Forms.TextBox()) {
         using (var graphics = textBox.CreateGraphics()) {
             return(new System.Windows.Point(graphics.DpiX / 96, graphics.DpiY / 96));
         }
     }
 }
// </Snippet2>

        private void CreateGraphicsButton_Click(object sender, System.EventArgs e)
        {
// <Snippet3>
            Graphics myGraphics = myTextBox.CreateGraphics();

            myGraphics.DrawEllipse(new Pen(Color.Black, 3), 0F, 0F, 230F, 105F);
            myGraphics.FillEllipse(Brushes.Goldenrod, 0F, 0F, 230F, 105F);
// </Snippet3>
        }
Exemple #5
0
        public void SetTextBoxWidthAndHeight(System.Windows.Forms.TextBox textBox, int maxLength)
        {
            textBox.MaxLength = maxLength;

            using (Graphics G = textBox.CreateGraphics())
            {
                textBox.Width = 28;
            }
        }
 static System.Windows.Point GetGraphicsDpi()
 {
     System.Windows.Point dpi;
     using (var textBox = new System.Windows.Forms.TextBox()) {
         using (var graphics = textBox.CreateGraphics()) {
             dpi = new System.Windows.Point(graphics.DpiX, graphics.DpiY);
         }
     }
     return(dpi);
 }
 private void UpdateSize()
 {
     if (this.IsHandleCreated)
     {
         Size      overhead = Size - _exceptionMessage.DisplayRectangle.Size;
         Rectangle working  = _working;
         working.Size    = working.Size - overhead;              // Convert working to inner size
         working.Width  -= System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
         working.Height -= System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight;
         Size size;
         using (System.Drawing.Graphics graphics = _exceptionMessage.CreateGraphics())
         {
             if (_exceptionMessage.WordWrap)
             {
                 size = Size.Ceiling(graphics.MeasureString(_exceptionMessage.Text, _exceptionMessage.Font, working.Width));
             }
             else
             {
                 size = Size.Ceiling(graphics.MeasureString(_exceptionMessage.Text, _exceptionMessage.Font));
             }
         }
         ScrollBars scrollBars = ScrollBars.None;
         if (size.Height > working.Height)
         {
             size.Height = working.Height;
             scrollBars |= ScrollBars.Vertical;
         }
         if (size.Width > working.Width)
         {
             size.Width  = working.Width;
             scrollBars |= ScrollBars.Horizontal;
         }
         _exceptionMessage.ScrollBars = scrollBars;
         Element.ConstrainMin(ref size, new Size(220, 26));
         size       += overhead;
         this.Bounds =
             new Rectangle
             (
                 new Point
                 (
                     ((_working.Width / 2) - (size.Width / 2)) + _working.Left,
                     ((_working.Height / 2) - (size.Height / 2)) + _working.Top
                 ),
                 size
             );
     }
 }
Exemple #8
0
        public void SetTextBoxWidthAndHeight(System.Windows.Forms.TextBox textBox, int maxLength)
        {
            textBox.MaxLength = maxLength;

            using (Graphics G = textBox.CreateGraphics())
            {
                //textBox.Width = (int)(maxLength * (G.MeasureString("X", textBox.Font)).Width);
                //textBox.Height = (int) (G.MeasureString("X", textBox.Font).Height);
                textBox.Width = 28;
                //textBox.Height = 34;
            }

            ////int width = Year.ClientSize.Width + YearMonth.ClientSize.Width + Month.ClientSize.Width + MonthDay.ClientSize.Width + Day.ClientSize.Width;
            ////int height = Year.ClientSize.Height;
            ////this.ClientSize = new Size(width, height);
            //this.Size=new Size(width,height);
        }
        /// <summary>
        /// Fills the form using data from the Customer dataset.
        /// </summary>
        private void FillForm()
        {
            // Populate the controls from the DataSet
            System.Data.DataRow drCustomer = m_dsCustomer.Tables[Customer.TN_CUSTOMER].Rows[0];

            txtName.Text       = drCustomer[Customer.FN_CUSTOMER_NAME].ToString();
            txtCountry.Text    = drCustomer[Customer.FN_COUNTRY].ToString();
            txtPostalCode.Text = drCustomer[Customer.FN_POSTALCODE].ToString();
            //TODO: Add the rest of the controls here

            // Set the customer name width appropriate for the contents
            // Create a graphics object to use to measure the width.
            Graphics g      = txtName.CreateGraphics();
            Int32    iWidth = (Int32)g.MeasureString(txtName.Text, txtName.Font).Width + 5;

            txtName.Width = iWidth;
            g.Dispose();
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handle an event when mouse enters the text box to display the whole path if it is
        /// truncated. (Tooltip not currently displayed)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// ------------------------------------------------------------------------------------
        private void m_textBoxBackupPath_MouseEnter(object sender, System.EventArgs e)
        {
            Graphics graphics = m_textBoxBackupPath.CreateGraphics();

            if (m_textBoxBackupPath.ClientSize.Width + 12 <
                graphics.MeasureString(m_textBoxBackupPath.Text, m_textBoxBackupPath.Font).Width)
            {
                toolTip1.SetToolTip(m_textBoxBackupPath, m_textBoxBackupPath.Text);
                toolTip1.AutoPopDelay = 3500;
                toolTip1.InitialDelay = 1000;
            }
            else
            {
                toolTip1.SetToolTip(m_textBoxBackupPath, null);
            }

            graphics.Dispose();
        }
 /// <summary>
 /// Returns a default TextBox.
 /// Its position is based on both parameters.
 /// </summary>
 /// <param name="position">Position of the TextBox.</param>
 /// <param name="reqWidth">Requested width, as a number of characters.</param>
 /// <returns></returns>
 private TextBox CreateTextBox(Position position, int reqWidth)
 {
   TextBox txt = new TextBox();
   txt.Multiline = false;
   StringBuilder sb = new StringBuilder(reqWidth < 0 ? 0 : reqWidth);
   for (int i = 0; i < reqWidth; i++)
     sb.Append("o"); // take 'o' as the character to measure
   int width = (int)(txt.CreateGraphics().MeasureString(sb.ToString(), txt.Font).Width);
   if (width < position.WidthColumnTwo)
   {
     txt.Width = position.WidthColumnTwo;
     txt.Location = new Point(position.StartColumnTwo, position.LinePosition - 5);
   }
   else
   {
     txt.Width = position.Width;
     position.LinePosition += position.LineHeight;
     txt.Location = new Point(position.StartColumnOne + position.Margin, position.LinePosition);
   }
   txt.TabIndex = position.NextTabIndex;
   txt.Name = "textBox" + position.TabIndex.ToString();
   txt.Leave += _entryLeave;
   return txt;
 }
 public static bool IsNewLineNeeded(string text, TextBox tb)
 {
     TextFormatFlags noPrefix = TextFormatFlags.NoPrefix;
     using (Graphics graphics = tb.CreateGraphics())
     {
         System.Drawing.Size proposedSize = new System.Drawing.Size(0x7fffffff, 0x7fffffff);
         System.Drawing.Size size2 = TextRenderer.MeasureText(graphics, text, tb.Font, proposedSize, noPrefix);
         return (tb.ClientSize.Width < size2.Width);
     }
 }