无法将类型'string'隐式转换为'System.Drawing.SizeF'

时间:2016-04-07 13:39:31

标签: c#

在尝试将string“A3”值转换为System.Drawing.SizeF时遇到此错误:

  

无法将类型'string'隐式转换为'System.Drawing.SizeF'

我尝试过的一个例子是:

SizeF f = new SizeF();
f = "A3";

在我的场景中,我将通过一个名为pdfSize的变量传递此字符串值。尺寸可以是A3,A4等。如何识别A4,A0的尺寸,如A3所述为297 mm * 420 mm。

问题:如何将string值(如“A3”或“A4”)转换为System.Drawing.SizeF值?

2 个答案:

答案 0 :(得分:2)

您无法直接转换它。你需要做那样的事情:

float a4Height = 10;
float a4Width = 5;

float a3Height = 5;
float a3Width = 2;

private SizeF CreateSizeF(string pageSize)
{
    SizeF sizeF = null;

    if(pageSize == "A4")
    {
        sizeF = new System.Drawing.SizeF(a4Width, a4Height);
    }
    else if(pageSize == "A3")
    {
        sizeF = new System.Drawing.SizeF(a3Width, a3Height);
    }

    return sizeF;
}

答案 1 :(得分:0)

以下是如何存储和使用这些值的快速而肮脏的示例:

    private void btnStop_Click(object sender, EventArgs e)
            {
                if (Informacao.RadButtonValue == "Normal")
                {
                    foreach (ListViewItem itemcheck in Informacao.mListView.CheckedItems)
                    {
                        if (itemcheck.Text == lblTimerScreenName.Text)
                        {
                            DialogResult result = MessageBox.Show(String.Format("Confirm changes to {0}", lblTimerScreenName.Text),
                                                                  "Please Confirm", MessageBoxButtons.OKCancel);
                            if (result == DialogResult.OK)
                            {
                                timer2.Stop();
                                itemcheck.SubItems[1].Text = lblTimerScreenName.Text;
                                SaveTime = textBox1.Text;
                                itemcheck.SubItems[9].Text = SaveTime;
                                itemcheck.SubItems[10].Text = Informacao.RadButtonValue;
                            }
                            break;
                        }
                    }
                }
...