Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. showing image on label.

showing image on label.

Scheduled Pinned Locked Moved C#
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ron bharath
    wrote on last edited by
    #1

    Hello All, I have a situation here where in i have to concatenate an icon to the label text based on some information. As we have already set the image property i cannot go ahead with the image align property as it will align the image attached to the image property. I am trying here to add an icon or image to the end of the text. Thanks in Advance Bharath s Ron

    L B 2 Replies Last reply
    0
    • R Ron bharath

      Hello All, I have a situation here where in i have to concatenate an icon to the label text based on some information. As we have already set the image property i cannot go ahead with the image align property as it will align the image attached to the image property. I am trying here to add an icon or image to the end of the text. Thanks in Advance Bharath s Ron

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      class Program
      {
      static void Main(string[] args)
      {
      using (var f = new Form())
      {
      var lbl = new Label();
      lbl.Text = "Hello world";
      lbl.Image = f.Icon.ToBitmap();
      lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
      f.Controls.Add(lbl);
      f.ShowDialog();
      }
      }
      }

      Ron.bharath wrote:

      i cannot go ahead with the image align property as it will align the image attached to the image property.

      That's what you asked; put the image in the label, on the right side of the text. If you want something else, you'd have to specify 'what' you want a bit more clearly. You could add a PictureBox on the right next to the label (concatenating controls, not data), but that would be less efficient.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      B 1 Reply Last reply
      0
      • R Ron bharath

        Hello All, I have a situation here where in i have to concatenate an icon to the label text based on some information. As we have already set the image property i cannot go ahead with the image align property as it will align the image attached to the image property. I am trying here to add an icon or image to the end of the text. Thanks in Advance Bharath s Ron

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        At design-time you have several choices ... if your Label, and whatever graphic element object, is going to remain the same size at run-time no matter how its containing Control, or Form, is re-sized, and if your Label content, and the graphic content object, is not going to change: 1. you can position the Icon, or PictureBox, over the end of the Label Text. 2. you could create a custom UserControl combining a Label and a graphic element object, and design it to look "right." If you are talking about creating text, and a graphic element, at run-time, or having text, or graphic, that changes at run-time, the problems of alignment become significant. You can, in fact, insert a graphic object into the Controls Collection of a Label (or other Win Forms Control); you can't do that at design-time unless you modify the Designer.cs file yourself, which is not something recommended to do. Once you have a graphic element "inside" a Label's DisplayRectangle, you may have the same problems of alignment/positioning you would have at design-time. An alternative you could consider would be using a RichTextBox instead of a Label, where the insertion of graphic elements ... while not simple ... is more straightforward in that they can be placed in the "flow" of the text layout, and, in a RichTextBox calculation of the co-ordinates of the current end of the text content is much easier. What have you tried so far ? And, is what you need to do to be done at run-time with changing content for both Label Text, and the related graphic element ?

        “I'm an artist: it's self evident that word implies looking for something all the time without ever finding it in full. It is the opposite of saying : 'I know all about it. I've already found it.' As far as I'm concerned, the word means: 'I am looking. I am hunting for it. I am deeply involved.'” Vincent Van Gogh

        R 1 Reply Last reply
        0
        • L Lost User

          class Program
          {
          static void Main(string[] args)
          {
          using (var f = new Form())
          {
          var lbl = new Label();
          lbl.Text = "Hello world";
          lbl.Image = f.Icon.ToBitmap();
          lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
          f.Controls.Add(lbl);
          f.ShowDialog();
          }
          }
          }

          Ron.bharath wrote:

          i cannot go ahead with the image align property as it will align the image attached to the image property.

          That's what you asked; put the image in the label, on the right side of the text. If you want something else, you'd have to specify 'what' you want a bit more clearly. You could add a PictureBox on the right next to the label (concatenating controls, not data), but that would be less efficient.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Upvoted. Good answer: I had assumed the OP found that using the Image property of the Label, and ContentAlignment, didn't meet his needs, which may not be the case, here.

          “I'm an artist: it's self evident that word implies looking for something all the time without ever finding it in full. It is the opposite of saying : 'I know all about it. I've already found it.' As far as I'm concerned, the word means: 'I am looking. I am hunting for it. I am deeply involved.'” Vincent Van Gogh

          1 Reply Last reply
          0
          • B BillWoodruff

            At design-time you have several choices ... if your Label, and whatever graphic element object, is going to remain the same size at run-time no matter how its containing Control, or Form, is re-sized, and if your Label content, and the graphic content object, is not going to change: 1. you can position the Icon, or PictureBox, over the end of the Label Text. 2. you could create a custom UserControl combining a Label and a graphic element object, and design it to look "right." If you are talking about creating text, and a graphic element, at run-time, or having text, or graphic, that changes at run-time, the problems of alignment become significant. You can, in fact, insert a graphic object into the Controls Collection of a Label (or other Win Forms Control); you can't do that at design-time unless you modify the Designer.cs file yourself, which is not something recommended to do. Once you have a graphic element "inside" a Label's DisplayRectangle, you may have the same problems of alignment/positioning you would have at design-time. An alternative you could consider would be using a RichTextBox instead of a Label, where the insertion of graphic elements ... while not simple ... is more straightforward in that they can be placed in the "flow" of the text layout, and, in a RichTextBox calculation of the co-ordinates of the current end of the text content is much easier. What have you tried so far ? And, is what you need to do to be done at run-time with changing content for both Label Text, and the related graphic element ?

            “I'm an artist: it's self evident that word implies looking for something all the time without ever finding it in full. It is the opposite of saying : 'I know all about it. I've already found it.' As far as I'm concerned, the word means: 'I am looking. I am hunting for it. I am deeply involved.'” Vincent Van Gogh

            R Offline
            R Offline
            Ron bharath
            wrote on last edited by
            #5

            The text and the image changes at runtime. I have tried drawing the image using graphics but i could not figure out how to repaint the image with another or erase only the image on the label.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups