Simplification de code

Résolu
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013 - 8 juil. 2009 à 14:01
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013 - 8 juil. 2009 à 15:49
bonjour
j'ai 2 routines presque identique seul le (Textbox) ou le (Label)
c'est possible de reunir les 2 dans une même methode


private string TextBoxToXml(Control c, StreamWriter sw)
{
try
{
if (c is TextBox)
{

sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");
sw.WriteLine(" <Text>" + c.Text + "</Text>");
sw.WriteLine(" <LocationX>" + ((TextBox)c).Location.X + "</LocationX>");
sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");
return "";
}
else return "no textbox";
}
catch (Exception e)
{
return "TextBoxToXml :error " + e.Message;
}
///********
private string LabelToXml(Control c, StreamWriter sw)
{
try
{
if (c is Label)
{

sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");
sw.WriteLine(" <Text>" + c.Text + "</Text>");
sw.WriteLine(" <LocationX>" + ((Label)c).Location.X + "</LocationX>");
sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");
return "";
}
else return "no Label";
}
catch (Exception e)
{
return "LabelToXml :error " + e.Message;
}
}



Merci d'avance

8 réponses

krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
8 juil. 2009 à 14:41
Je ne vois pas vraiment où est le problème :

        private string ControlToXml(Control c, StreamWriter sw)

        {

            try

            {

                if (c is TextBox)

                {

                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");

                    sw.WriteLine("  <Text>" + c.Text + "</Text>");

                    sw.WriteLine("  <LocationX>" + ((TextBox)c).Location.X + "</LocationX>");

                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");

                    return "";

                }

                else if (c is Label)

                {

                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");

                    sw.WriteLine("  <Text>" + c.Text + "</Text>");

                    sw.WriteLine("  <LocationX>" + ((Label)c).Location.X + "</LocationX>");

                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");

                    return "";

                }
                else return "no textbox nor label";

            }

            catch (Exception e)

            {

                return "ControlToXml :error " + e.Message;

            }

        }

Etant donné que toutes les propriétés que tu utilises sont dans Control, tu peux même faire ça :
        private string ControlToXml(Control c, StreamWriter sw)


        {


            try


            {


                if (c is TextBox || c is Label)


                {


                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");


                    sw.WriteLine("  <Text>" + c.Text + "</Text>");


                    sw.WriteLine("  <LocationX>" + ((TextBox)c).Location.X + "</LocationX>");


                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");


                    return "";


                }


                else return "no textbox nor label";


            }


            catch (Exception e)


            {


                return "ControlToXml :error " + e.Message;


            }


        }

Krimog : while (!(succeed = try())) ;
- NON, "LE BAR" n'est PAS un langage de programmation ! -
3
Zakki49 Messages postés 71 Date d'inscription vendredi 30 mai 2008 Statut Membre Dernière intervention 18 mai 2011
8 juil. 2009 à 14:39
Salut,

oui tu peux, puisque tu fais le test si c (ton control) est une textbox, donc tu peux très bien mettre dans ta fonction la partie testant si c'est un label.
0
Zakki49 Messages postés 71 Date d'inscription vendredi 30 mai 2008 Statut Membre Dernière intervention 18 mai 2011
8 juil. 2009 à 14:45
non pas possible de faire ça :

if (c is TextBox || c is Label)
                {
                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");
                    sw.WriteLine("  <Text>" + c.Text + "</Text>");
                    sw.WriteLine("  <LocationX>" + ((TextBox)c).Location.X + "</LocationX>");
                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");
                    return "";
                }
                else return "no textbox nor label";
            }

puisqu'il n'ecrit pas tout à fait la même chose
0
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013
8 juil. 2009 à 15:03
en effet mon probleme est sur
((TextBox)c).Location.X
ou
((Label)c).Location.X

je veux lister tous mes controls et en suite
les serialiser ou les deserialiser
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Zakki49 Messages postés 71 Date d'inscription vendredi 30 mai 2008 Statut Membre Dernière intervention 18 mai 2011
8 juil. 2009 à 15:05
ce que t'as mis krimog en premier est très bien :

private string ControlToXml(Control c, StreamWriter sw)
        {
            try
            {
                if (c is TextBox)
                {
                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");
                    sw.WriteLine("  <Text>" + c.Text + "</Text>");
                    sw.WriteLine("  <LocationX>" + ((TextBox)c).Location.X + "</LocationX>");
                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");
                    return "";
                }
                else if (c is Label)
                {
                    sw.WriteLine("<" + c.GetType() + "." + c.Name + ">");
                    sw.WriteLine("  <Text>" + c.Text + "</Text>");
                    sw.WriteLine("  <LocationX>" + ((Label)c).Location.X + "</LocationX>");
                    sw.WriteLine("</" + c.GetType() + "." + c.Name + ">");
                    return "";
                }
                else return "no textbox nor label";
            }
            catch (Exception e)
            {
                return "ControlToXml :error " + e.Message;
            }
        }

tu validera la reponse de krimog
0
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013
8 juil. 2009 à 15:14
non ce n'est pas ce que je recherche dans la solution qui marche j'ecrit 2 fois les même code textbox label pret
dans mon projet gloabal j'ai beaucoup plus de sw.writeln et j'ai des picturebox et des button dans je vais un copier coller puis je change textbox par button par exemple
mais j'aimerais savoir s'il n'existe pas mieux en passant en paramètre le textbox ou button
merci
0
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
8 juil. 2009 à 15:21
J'ai juste oublié un tout petit détail dans ma 2ème version : il ne faut pas mettre "((Textbox)c).Location" mais juste c.Location, au temps pour moi.

Krimog : while (!(succeed = try())) ;
- NON, "LE BAR" n'est PAS un langage de programmation ! -
0
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013
8 juil. 2009 à 15:49
ok ça l'air de marche mais je ne comprend j'avais essayer faire un c. (Ctrl+j) et je ne voyais pas les methodes

merci super
0
Rejoignez-nous