找回密码
 立即注册

QQ登录

只需一步,快速开始

linanxian

注册会员

16

主题

27

帖子

99

积分

注册会员

积分
99

活字格认证

linanxian
注册会员   /  发表于:2013-9-24 17:04  /   查看:5723  /  回复:6
private void detail_BeforePrint(object sender, EventArgs e)
        {
            foreach (var item in this.pageHeader.Controls)
            {

                Label lb = item as Label;
                if (lb.Name.ToString() == "lable8")
                {
                    lb.Width = lb.Text.Length;
                }
            }

            foreach (var item in this.detail.Controls)
            {
               
                TextBox tb = item as TextBox;
                if (null != tb)
                {
                    tb.Height = this.detail.Height;
                }
            }
        }

上述代码 if (lb.Name.ToString() == "lable8")里报错

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

6 个回复

倒序浏览
linanxian
注册会员   /  发表于:2013-9-24 17:33:00
沙发
if (lb.Name.ToString() == "lable8")
                {
                    lb.Width = lb.Text.Length;
                }
想在这段代码里实现想要设置指定lable的宽度,现在这样写是有问题的。
lb.Width = lb.Text.Length;也不能这样直接显示文字长度赋给lb.width里,有什么好的方法吗?
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-9-24 17:46:00
板凳
需要调用 MeasureString 方法来测量字符串显示长度,请参考:http://msdn.microsoft.com/zh-cn/ ... phics.measurestring(v=vs.85).aspx
回复 使用道具 举报
linanxian
注册会员   /  发表于:2013-9-25 09:36:00
地板
private void MeasureStringMin(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}

上述代码中参数PaintEventArgs报错,我几经参照了system.drawing.dll 帮忙能看一下吗
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-9-25 10:26:00
5#
请尝试以下代码:
  1.     private void SectionReport2_ReportStart(object sender, EventArgs e)
  2.     {
  3.         Bitmap bm = new Bitmap(300, 300);
  4.         Graphics g = Graphics.FromImage(bm);

  5.         label1.Width = g.MeasureString(label1.Text, label1.Font).Width;
  6.     }
复制代码
回复 使用道具 举报
linanxian
注册会员   /  发表于:2013-9-25 12:40:00
6#

区域报表控件宽度设置问题。

if (i == 7)
                {
                    GrapeCity.ActiveReports.SectionReportModel.Label label1 = new GrapeCity.ActiveReports.SectionReportModel.Label();
                    label1.Text = dt.Columns.Caption;
                    //label.Width = 0.45f;                  
                    label1.Style = "font-family: MS ゴシック; font-size: 11pt;";
                    Bitmap bm = new Bitmap(300, 300);
                    Graphics g = Graphics.FromImage(bm);
                    label1.Width = g.MeasureString(label1.Text, label1.Font).Width;
                    label1.Height = 0.35f;
                    label1.Location = new PointF(5.98f, 1.8f);
                    label1.Border.TopStyle = GrapeCity.ActiveReports.BorderLineStyle.ThickSolid;
                    label1.Border.BottomStyle = GrapeCity.ActiveReports.BorderLineStyle.ThickSolid;
                    label1.Border.RightStyle = GrapeCity.ActiveReports.BorderLineStyle.ThickSolid;

                    pageHeader.Controls.Add(label1);
                }

上述代码的  label1.Width = g.MeasureString(label1.Text, label1.Font).Width;的取值很大,能帮忙看一下吗

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-9-25 13:59:00
7#
增加一行代码 g.PageUnit = GraphicsUnit.Inch ,如下:
  1.     private void SectionReport2_ReportStart(object sender, EventArgs e)
  2.     {
  3.         Bitmap bm = new Bitmap(300, 300);
  4.         Graphics g = Graphics.FromImage(bm);
  5.         g.PageUnit = GraphicsUnit.Inch;

  6.         label1.Width = g.MeasureString(label1.Text, label1.Font).Width;
  7.     }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部