c#-将字符串转换为字体和颜色

有人可以帮我做一个正则表达式(或其他一些东西)吗,我真的很难做到这一点,而且找不到任何可以帮助我完成的东西.

我有一个程序,用户可以在窗体上放置一些控件.当他们单击“保存”按钮时,它会遍历表单上的所有控件,并将其详细信息保存到文本文件(我知道该怎么做).

Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617

说明:

Control Type
Text property of that control
Location of the control
Font properties for that control
ForeColor for that control.


用户保存时创建的此文本文件可能仅包含单个控件(如上所示)的信息,甚至可能包含多个控件的信息,例如:

Label
"This is text on a label"
20, 97
Tahoma, 7.5, Regular
-778225617
LinkLabel
"This text belongs to the linklabel text property."
Arial, 20, Bold
-119045893

说明:

Control
Text Property
Location
Font Properties
ForeColor
Control
Text Property
Location
Font Properties
ForeColor

…等等…我觉得这对我来说很难,因为到目前为止我还不是专家.有人能帮帮我吗?我还需要将Font属性行从字符串转换为Font对象,以便可以在运行时将其分配给指定控件的Font属性.

我真的很感谢任何帮助.非常感谢.

谢谢
杰伊

解决方法:

10分钟解决方案:

好吧,接下来的五分钟是我真正的意思.我希望这可以解决问题.

>步骤1:获取画布-画布对象
>步骤2:向其中添加图形/控件
>步骤3:序列化,保存并重新加载对象

请参阅以下内容.

步骤1:画布类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

namespace SaveControls
{
    [Serializable()]
    public class CCanvas
    {

        List<CDrawing> _listControls;
    public List<CDrawing> Controls
    {
        get { return _listControls; }
    }

    public CCanvas()
    {
        _listControls = new List<CDrawing>();
    }

    public void AddControls(CDrawing theControls)
    {
        _listControls.Add(theControls);
    }

    public void ReloadControl(Form frm)
    {
        //foreach (CDrawing draw in _listControls) -- requires IEnumerable implementation.
        for (int i = 0; i < _listControls.Count; i++)
        {
            CDrawing d = (CDrawing)_listControls[i];
            d.Draw(frm);
        }
    }


    public void Save()
    {
        try
        {
            using (Stream stream = File.Open("data.bin", FileMode.Create))
            {
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(stream, this);
            }
        }
        catch (IOException)
        {
        }

    }

    public CCanvas Open()
    {
        CCanvas LoadedObj = null;
        using (Stream stream = File.Open("data.bin", FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            LoadedObj = (CCanvas)bin.Deserialize(stream);

        }
        return LoadedObj;
    }
}

}

步骤2:添加工程图

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Data;

using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Drawing;

namespace SaveControls
{
    [Serializable()]
    public class CDrawing
    {
        public enum ControlTypes { Label, TextBox, None };

        private ControlTypes _controlType;
    public ControlTypes ControlType
    { get { return _controlType; } }

    private string _strControlText;
    public string Text
    { get { return _strControlText; } }

    private int _xPosition;
    public int X
    { get { return _xPosition; } }

    private int _yPosition;
    public int Y
    { get { return _yPosition; } }


    private string _strFontName;
    public string Font
    { get { return _strFontName; } }

    double _fFontSize;
    public double Size
    { get { return _fFontSize; } }

    string _strStyle;
    public string Style
    { get { return _strStyle; } }

    decimal _dForegroundColor;
    public decimal Color
    { get { return _dForegroundColor; } }

    public CDrawing(ControlTypes controlType, string strControlText, int xPosition, int yPosition,
    string strFontName, double fFontSize, string strStyle, decimal dForegroundColor)
    {
        _controlType = controlType;
        _strControlText = strControlText;
        _xPosition = xPosition;
        _yPosition = yPosition;
        _strFontName = strFontName;
        _fFontSize = fFontSize;
        _strStyle = strStyle;
        _dForegroundColor = dForegroundColor;


    }

    public void Draw(Form frm)
    {
        if (_controlType == ControlTypes.Label)
        {
            Label lbl = new Label();

            lbl.Text = _strControlText;
            lbl.Location = new Point(_xPosition, _yPosition);

            System.Drawing.FontStyle fs = (_strStyle == System.Drawing.FontStyle.Regular.ToString()) ? System.Drawing.FontStyle.Regular : System.Drawing.FontStyle.Bold;

            lbl.Font = new System.Drawing.Font(_strFontName, (float)_fFontSize, fs);
            lbl.ForeColor = SystemColors.Control;// _dForegroundColor;
            lbl.Visible = true;
            frm.Controls.Add(lbl);
        }
    }


}

}

步骤3:使用,序列化,保存,重新加载

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


    public void Save()
    {
        //Create a canvas object
        CCanvas Canvas1 = new CCanvas();

        //Add controls
        Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label1", 10, 100, "Tahoma", 7.5, "Regular", -778225617));
        Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label11", 20, 200, "Verdana", 7.5, "Regular", -778225617));
        Canvas1.AddControls(new CDrawing(CDrawing.ControlTypes.Label, "This is text on a label111", 30, 300, "Times New Roman", 7.5, "Regular", -778225617));

        //Save the object
        Canvas1.Save();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Save();

    }

    private void btnLoad_Click(object sender, EventArgs e)
    {
        Load();

    }

    public void Load()
    {
        //Retrieve
        CCanvas Canvas2 = new CCanvas();

        //opens the binary file
        Canvas2 = Canvas2.Open();

        //loads control to this form.
        Canvas2.ReloadControl(this);


    }

}

如果您打算讨厌此解决方案,请告诉我们原因.同时,我正在尝试寻找上传的地方. Googlecode,但是我没有安装Subversion客户端. :0(

上一篇:C#-Winform Webbrowser自定义字体未显示


下一篇:Python以编程方式更改控制台字体大小