Convert strings to font and color

Can someone please help me with a regex (or something else), I really try my best to do this and cannot find anything to help me finish it.

I have a program in which a user places some controls on a form. and when they click the "Save" button, they look at all the controls in the form and save their data in a text file (which I know how to do). For example:

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

Explanation:

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

... This text file, created when it was saved, can contain information for only one control, as shown above, or even several controls, for example:

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

Explanation:

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

... .. , , . -, , ? Font, Font .

. .

+3
6

10 :

, 5 , ; , .

  • 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 (

0

- :

using System;
using System.Drawing;

class Example
{
    static void Main()
    {
        String fontName = "Tahoma, Regular, Size";
        String[] fontNameFields = fontName.Split(',');

        Font font = new Font(fontNameFields[0],
            Single.Parse(fontNameFields[2]),
            (FontStyle)Enum.Parse(typeof(FontStyle), fontNameFields[1]));
    }
}
+3

, .

.

em . .

+1

, , ... . ( , WinForms) .

- .NET, . , WinForms CSSName [, ], CSS . [ , ]

Btw, , , , :  RGB:

255 255 255

:

  • , -, , , , WinForms. (XML ).
  • . RTF. RTF, , , , .
  • . , , , , , .
+1

XmlSerialization. , , dot Save; .

, Canvas.

- Canvas.AddControls(controlTypes.Label, " ", 20, 97, Tahoma, 7.5, Regular, -778225617);

XmlSerializer.

, xml? . .

- :

public static void Save(object obj)
{
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        // Serialize an object into the storage referenced by 'stream' object.
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // Serialize multiple objects into the stream
        formatter.Serialize(stream, obj);

        // If you want to put the stream into Array of byte use below code
        // byte[] buffer = stream.ToArray();
    }
}
+1

?

Font font = new Font("Tahoma",12,FontStyle.Regular);
-1

Source: https://habr.com/ru/post/1719247/


All Articles