Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
QuestionRectangleF to and back from string? Pin
Chesnokov Yuriy30-May-11 22:49
professionalChesnokov Yuriy30-May-11 22:49 
AnswerRe: RectangleF to and back from string? Pin
Richard MacCutchan30-May-11 23:30
mveRichard MacCutchan30-May-11 23:30 
AnswerRe: RectangleF to and back from string? Pin
Pete O'Hanlon31-May-11 0:32
mvePete O'Hanlon31-May-11 0:32 
QuestionRe: RectangleF to and back from string? Pin
Chesnokov Yuriy31-May-11 0:45
professionalChesnokov Yuriy31-May-11 0:45 
AnswerRe: RectangleF to and back from string? Pin
Pete O'Hanlon31-May-11 1:27
mvePete O'Hanlon31-May-11 1:27 
AnswerRe: RectangleF to and back from string? Pin
Chesnokov Yuriy31-May-11 1:54
professionalChesnokov Yuriy31-May-11 1:54 
GeneralRe: RectangleF to and back from string? Pin
Pete O'Hanlon31-May-11 2:36
mvePete O'Hanlon31-May-11 2:36 
AnswerRe: RectangleF to and back from string? [modified] Pin
DaveyM691-Jun-11 0:35
professionalDaveyM691-Jun-11 0:35 
The following two methods are based on the ConvertFrom and ConvertTo methods of the RectangleConverter class, tweaked for RectangleF and other bits removed as we know the types required in advance.
C#
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;

public class RectangleFUtilities
{
    public static bool TryParse(string s, out RectangleF result)
    {
        if (string.IsNullOrEmpty(s))
        {
            result = RectangleF.Empty;
            return false;
        }
        s = s.Trim();
        if (s.Length == 0)
        {
            result = RectangleF.Empty;
            return false;
        }
        CultureInfo culture = CultureInfo.CurrentCulture;
        string[] strArray = s.Split(new char[] { culture.TextInfo.ListSeparator[0] });
        float[] numArray = new float[strArray.Length];
        for (int i = 0; i < numArray.Length; i++)
        {
            if (!float.TryParse(strArray[i], out numArray[i]))
            {
                result = RectangleF.Empty;
                return false;
            }
        }
        if (numArray.Length != 4)
        {
            result = RectangleF.Empty;
            return false;
        }
        result = new RectangleF(numArray[0], numArray[1], numArray[2], numArray[3]);
        return true;
    }

    public static string ToString(RectangleF rectangleF)
    {
        CultureInfo culture = CultureInfo.CurrentCulture;
        string separator = culture.TextInfo.ListSeparator + " ";
        TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
        string[] strArray = new string[4];
        int num = 0;
        strArray[num++] = converter.ConvertToString(null, culture, rectangleF.X);
        strArray[num++] = converter.ConvertToString(null, culture, rectangleF.Y);
        strArray[num++] = converter.ConvertToString(null, culture, rectangleF.Width);
        strArray[num++] = converter.ConvertToString(null, culture, rectangleF.Height);
        return string.Join(separator, strArray);
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



modified on Wednesday, June 1, 2011 7:12 AM

QuestionSerialcomm dialing voice over modem Pin
Chuck Richards30-May-11 11:17
Chuck Richards30-May-11 11:17 
AnswerRe: Serialcomm dialing voice over modem Pin
Luc Pattyn30-May-11 12:25
sitebuilderLuc Pattyn30-May-11 12:25 
GeneralRe: Serialcomm dialing voice over modem Pin
Chuck Richards1-Jun-11 3:02
Chuck Richards1-Jun-11 3:02 
AnswerRe: Serialcomm dialing voice over modem Pin
Luc Pattyn1-Jun-11 3:13
sitebuilderLuc Pattyn1-Jun-11 3:13 
GeneralRe: Serialcomm dialing voice over modem Pin
Chuck Richards1-Jun-11 9:41
Chuck Richards1-Jun-11 9:41 
AnswerRe: Serialcomm dialing voice over modem Pin
Luc Pattyn1-Jun-11 14:12
sitebuilderLuc Pattyn1-Jun-11 14:12 
GeneralRe: Serialcomm dialing voice over modem Pin
Chuck Richards2-Jun-11 9:15
Chuck Richards2-Jun-11 9:15 
AnswerRe: Serialcomm dialing voice over modem Pin
Luc Pattyn2-Jun-11 9:31
sitebuilderLuc Pattyn2-Jun-11 9:31 
GeneralRe: Serialcomm dialing voice over modem Pin
Chuck Richards3-Jun-11 3:13
Chuck Richards3-Jun-11 3:13 
GeneralRe: Serialcomm dialing voice over modem Pin
Chuck Richards2-Jun-11 9:22
Chuck Richards2-Jun-11 9:22 
AnswerRe: Serialcomm dialing voice over modem [modified] Pin
Chuck Richards2-Jun-11 7:34
Chuck Richards2-Jun-11 7:34 
Questionproblem with calling folderbrowingDialog in a child thread. Pin
prasadbuddhika30-May-11 6:25
prasadbuddhika30-May-11 6:25 
AnswerRe: problem with calling folderbrowingDialog in a child thread. Pin
Luc Pattyn30-May-11 6:45
sitebuilderLuc Pattyn30-May-11 6:45 
GeneralRe: problem with calling folderbrowingDialog in a child thread. Pin
prasadbuddhika30-May-11 15:10
prasadbuddhika30-May-11 15:10 
GeneralRe: problem with calling folderbrowingDialog in a child thread. Pin
prasadbuddhika30-May-11 15:30
prasadbuddhika30-May-11 15:30 
GeneralRe: problem with calling folderbrowingDialog in a child thread. Pin
BobJanova30-May-11 22:33
BobJanova30-May-11 22:33 
AnswerRe: problem with calling folderbrowingDialog in a child thread. Pin
Shameel31-May-11 1:01
professionalShameel31-May-11 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.