IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

[UserControl - TextBox] 2 format de texte


Sujet :

C#

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    612
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 612
    Points : 338
    Points
    338
    Par défaut [UserControl - TextBox] 2 format de texte
    Bonjour,

    je crèè une surcharge du TextBox pour le rendre, pour l'instant, plus design
    par la suite j'ajouterais peut etre d'autre possibilitè

    mon problème est que lorsue je tape tu texte, il s'écrit en gras et Font standard
    une fois sortis du TextBox surcharger le text prend sa place avec le Font definie dans la PropertGrid

    j'utilise 2 fonction non definie ici:
    Hauteur_Optimal => Calcul la hauteur optimal du TextBox en fonction du Font
    GetRoundedRect => fonction trouver sur internet qui genere des rectangle avec les coins en arrondis, rendus parfait!

    voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    public partial class TextBox_TBR : TextBox
    {
        public TextBox_TBR()
        {
            // Le control se peint lui même
            // Le control utilise un double buffer (anti-scintillement
            // Le control supporte la transparence
            // Le control ignore le messsage WM_ERASEBKGND pour reduire le scintillement
            this.SetStyle(ControlStyles.UserPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.AllPaintingInWmPaint, true);
     
            InitializeComponent();
     
            this.BorderStyle = BorderStyle.FixedSingle;
        }
     
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            pevent.Graphics.FillRectangle(new SolidBrush(this.Parent.BackColor),
                new Rectangle(0, 0, this.Width, this.Height));
        }
     
        protected override void OnPaint(PaintEventArgs e)
        {
            HauteurOptimal();
     
            // Fond
            GraphicsPath RRBackground = this.GetRoundedRect(new Rectangle(0, 0, this.Width - 1, this.Height - 1), 3F);
     
            e.Graphics.FillPath(new SolidBrush(this.BackColor), RRBackground);
            e.Graphics.DrawPath(new Pen(Color.FromArgb(183, 202, 234)), RRBackground);
     
            // Texte
            StringFormat sf = StringFormat.GenericTypographic;
            sf.LineAlignment = StringAlignment.Far;
     
            string _textResize = this.Text;
     
            if (e.Graphics.MeasureString(_textResize, this.Font).Width > (this.Width - 3))
                _textResize += "...";
     
            while (e.Graphics.MeasureString(_textResize, this.Font).Width > (this.Width - 3))
                _textResize = _textResize.Substring(0, _textResize.Length - 4) + "...";
     
            e.Graphics.DrawString(_textResize, this.Font, new SolidBrush(this.ForeColor),
                new Rectangle(5, -5, this.Width, this.Height),
                sf);
            sf.Dispose();
        }

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    j'ai fait le test avec un cas beaucoup plus simple (juste un DrawString dans le OnPaint), et j'obtiens le même comportement... c'est bizarre, je ne comprends pas trop ce qui déconne !

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    612
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 612
    Points : 338
    Points
    338
    Par défaut
    c'est comme si lorsque la TextBox passe en mode edition elle changer de Designer....

Discussions similaires

  1. Format du texte dans une TextBox
    Par ghosty04 dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 30/06/2009, 08h20
  2. Formater un texte dans une expression
    Par stéphane_ais2 dans le forum Access
    Réponses: 5
    Dernier message: 18/10/2005, 13h52
  3. Format champ texte
    Par Smint dans le forum Access
    Réponses: 4
    Dernier message: 15/09/2005, 16h27
  4. [LG]Formater le texte dans un WriteLn
    Par busy999 dans le forum Langage
    Réponses: 16
    Dernier message: 15/11/2003, 11h16
  5. Envoi de mail au format Rich-Text
    Par LineLe dans le forum ASP
    Réponses: 4
    Dernier message: 29/10/2003, 16h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo