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. Error when i try to add my control to form1

Error when i try to add my control to form1

Scheduled Pinned Locked Moved C#
helpvisual-studiographicscsharpcom
8 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.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    - I get this error when i try to add my control (named 'tab') to the form1: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/281c49f0-e874-4b83-bff6-9bc6b8526d42/ddctbbc-1dc0bf33-d2a3-4c8c-b6f5-c88acb2bf200.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzI4MWM0OWYwLWU4NzQtNGI4My1iZmY2LTliYzZiODUyNmQ0MlwvZGRjdGJiYy0xZGMwYmYzMy1kMmEzLTRjOGMtYjZmNS1jODhhY2IyYmYyMDAuanBnIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.BbYanI4o_JGUrbacjktRwbF20pjWI8KkmdUKJ6zs0x8[^] and my custom tab control code is here: "

        public tab()
        {
            InitializeComponent();
        }
    
    
        Image img = new Bitmap("google.png"); 
        Rectangle rect = new Rectangle(5, 5, 22, 22);
        
        private void tab\_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(img, rect);
        }
    

    " the image "google.png" is in this folder: D:\Programe\Info\Visual Studio 2010\Projects\Web\FFtabs\FFtabs\bin\Debug\ from the same folder where the executable is running! Why is doing this? Please help me. I fight with this problem for years and I always had problems with loading images in VS. I think is a root

    Richard DeemingR D 2 Replies Last reply
    0
    • _ _Q12_

      - I get this error when i try to add my control (named 'tab') to the form1: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/281c49f0-e874-4b83-bff6-9bc6b8526d42/ddctbbc-1dc0bf33-d2a3-4c8c-b6f5-c88acb2bf200.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzI4MWM0OWYwLWU4NzQtNGI4My1iZmY2LTliYzZiODUyNmQ0MlwvZGRjdGJiYy0xZGMwYmYzMy1kMmEzLTRjOGMtYjZmNS1jODhhY2IyYmYyMDAuanBnIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.BbYanI4o_JGUrbacjktRwbF20pjWI8KkmdUKJ6zs0x8[^] and my custom tab control code is here: "

          public tab()
          {
              InitializeComponent();
          }
      
      
          Image img = new Bitmap("google.png"); 
          Rectangle rect = new Rectangle(5, 5, 22, 22);
          
          private void tab\_Paint(object sender, PaintEventArgs e)
          {
              e.Graphics.DrawImage(img, rect);
          }
      

      " the image "google.png" is in this folder: D:\Programe\Info\Visual Studio 2010\Projects\Web\FFtabs\FFtabs\bin\Debug\ from the same folder where the executable is running! Why is doing this? Please help me. I fight with this problem for years and I always had problems with loading images in VS. I think is a root

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The problem is that when the code runs in the design view, the current directory is the Visual Studio application directory, not your project's directory. The file C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\google.png doesn't exist, so the Bitmap constructor throws an exception. You can use the DesignMode property[^] to test whether your control is in design mode. You should probably also check that the file hasn't been deleted:

      Image img;

      public tab()
      {
      InitializeComponent();

      if (!DesignMode && File.Exists("google.png"))
      {
          img = new Bitmap("google.png");
      }
      

      }

      private void tab_Paint(object sender, PaintEventArgs e)
      {
      if (img != null)
      {
      e.Graphics.DrawImage(img, rect);
      }
      }

      You should be able to adapt the code from this SO answer[^] to resolve the image path at design time, if you need to see the image in the designer:

      private string ResolveImagePath()
      {
      string path = "google.png";
      if (DesignMode)
      {
      var typeResolver = (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
      if (typeResolver != null)
      {
      AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName();
      string basePath = typeResolver.GetPathOfAssembly(name);
      path = Path.Combine(basePath, path);
      }
      }

      return path;
      

      }

      public tab()
      {
      InitializeComponent();

      string imagePath = ResolveImagePath();
      if (File.Exists(imagePath))
      {
          img = new Bitmap(imagePath);
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      _ 3 Replies Last reply
      0
      • _ _Q12_

        - I get this error when i try to add my control (named 'tab') to the form1: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/281c49f0-e874-4b83-bff6-9bc6b8526d42/ddctbbc-1dc0bf33-d2a3-4c8c-b6f5-c88acb2bf200.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzI4MWM0OWYwLWU4NzQtNGI4My1iZmY2LTliYzZiODUyNmQ0MlwvZGRjdGJiYy0xZGMwYmYzMy1kMmEzLTRjOGMtYjZmNS1jODhhY2IyYmYyMDAuanBnIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.BbYanI4o_JGUrbacjktRwbF20pjWI8KkmdUKJ6zs0x8[^] and my custom tab control code is here: "

            public tab()
            {
                InitializeComponent();
            }
        
        
            Image img = new Bitmap("google.png"); 
            Rectangle rect = new Rectangle(5, 5, 22, 22);
            
            private void tab\_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawImage(img, rect);
            }
        

        " the image "google.png" is in this folder: D:\Programe\Info\Visual Studio 2010\Projects\Web\FFtabs\FFtabs\bin\Debug\ from the same folder where the executable is running! Why is doing this? Please help me. I fight with this problem for years and I always had problems with loading images in VS. I think is a root

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        On top of what Richard said, for custom controls where you have images that don't change and are a mandatory part of your control, you should probably move the images to Resources instead of leaving them in files. You can then load the image from your controls Resource and you won't have any problems with managing files on the drive. Load image from resources area of project in C# - Stack Overflow[^]

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          The problem is that when the code runs in the design view, the current directory is the Visual Studio application directory, not your project's directory. The file C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\google.png doesn't exist, so the Bitmap constructor throws an exception. You can use the DesignMode property[^] to test whether your control is in design mode. You should probably also check that the file hasn't been deleted:

          Image img;

          public tab()
          {
          InitializeComponent();

          if (!DesignMode && File.Exists("google.png"))
          {
              img = new Bitmap("google.png");
          }
          

          }

          private void tab_Paint(object sender, PaintEventArgs e)
          {
          if (img != null)
          {
          e.Graphics.DrawImage(img, rect);
          }
          }

          You should be able to adapt the code from this SO answer[^] to resolve the image path at design time, if you need to see the image in the designer:

          private string ResolveImagePath()
          {
          string path = "google.png";
          if (DesignMode)
          {
          var typeResolver = (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
          if (typeResolver != null)
          {
          AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName();
          string basePath = typeResolver.GetPathOfAssembly(name);
          path = Path.Combine(basePath, path);
          }
          }

          return path;
          

          }

          public tab()
          {
          InitializeComponent();

          string imagePath = ResolveImagePath();
          if (File.Exists(imagePath))
          {
              img = new Bitmap(imagePath);
          }
          

          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          _ Offline
          _ Offline
          _Q12_
          wrote on last edited by
          #4

          ___The bug is thrown at the initialization of the image. As you can see in the code below:

              public tab()
              {
                  InitializeComponent();
              }
          
          
              Image img = new Bitmap(@"google.png");
              Rectangle rect = new Rectangle(5, 5, 22, 22);
              
              private void tab\_Paint(object sender, PaintEventArgs e)
              {
                  e.Graphics.DrawImage(img, rect);
              }
          

          ___ NEXT, i initialize it in the constructor, following your helping code like this:

          public partial class tab : UserControl
          {
              public tab()
              {
                  InitializeComponent();
                  if (!DesignMode && File.Exists("google.png"))
                  {
                      img = new Bitmap("google.png");
                  }
              }
          
          
              Image img;
              Rectangle rect = new Rectangle(5, 5, 22, 22);
              
              private void tab\_Paint(object sender, PaintEventArgs e)
              {
                  e.Graphics.DrawImage(img, rect);
              }
          

          but another Error appear: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/281c49f0-e874-4b83-bff6-9bc6b8526d42/ddctlq3-8c112b8d-5685-4a43-a96e-21046443ef9a.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzI4MWM0OWYwLWU4NzQtNGI4My1iZmY2LTliYzZiODUyNmQ0MlwvZGRjdGxxMy04YzExMmI4ZC01Njg1LTRhNDMtYTk2ZS0yMTA0NjQ0M2VmOWEuanBnIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.1fkgpeEVDKRy30I70lftNt0WfbpoFl8LACty1N_fzsc[

          Richard DeemingR 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            The problem is that when the code runs in the design view, the current directory is the Visual Studio application directory, not your project's directory. The file C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\google.png doesn't exist, so the Bitmap constructor throws an exception. You can use the DesignMode property[^] to test whether your control is in design mode. You should probably also check that the file hasn't been deleted:

            Image img;

            public tab()
            {
            InitializeComponent();

            if (!DesignMode && File.Exists("google.png"))
            {
                img = new Bitmap("google.png");
            }
            

            }

            private void tab_Paint(object sender, PaintEventArgs e)
            {
            if (img != null)
            {
            e.Graphics.DrawImage(img, rect);
            }
            }

            You should be able to adapt the code from this SO answer[^] to resolve the image path at design time, if you need to see the image in the designer:

            private string ResolveImagePath()
            {
            string path = "google.png";
            if (DesignMode)
            {
            var typeResolver = (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
            if (typeResolver != null)
            {
            AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName();
            string basePath = typeResolver.GetPathOfAssembly(name);
            path = Path.Combine(basePath, path);
            }
            }

            return path;
            

            }

            public tab()
            {
            InitializeComponent();

            string imagePath = ResolveImagePath();
            if (File.Exists(imagePath))
            {
                img = new Bitmap(imagePath);
            }
            

            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            _ Offline
            _ Offline
            _Q12_
            wrote on last edited by
            #5

            Are you sure the root is correct? What i did: I created a new alb.jpg file into (your specified folder root): "c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE and the code now checks if in 'DesignMode', then load the default alb.jpg image. Else, is runtime and load the normal image. here is my code:

                public tab()
                {
                    InitializeComponent();
                    //if (!DesignMode && File.Exists("google.png"))
                    //{
                    //    img = new Bitmap("google.png");
                    //}
                    string alb = @"c:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\alb.jpg";
                    if (DesignMode==true)
                    {
                        img = new Bitmap(alb); 
                    }
                    else
                    {
                        img = new Bitmap("google.png");
                    }
                }
            
            
                Image img;
                Rectangle rect = new Rectangle(5, 5, 22, 22);
                
                private void tab\_Paint(object sender, PaintEventArgs e)
                {
                    e.Graphics.DrawImage(img, rect);
                }
            
                string mytext;
                public string tab\_Text
                {
                    get { return mytext; }
                    set
                    {
                        label1.Text = mytext = value;
                    }
                }
            

            I get the same error as the first time, not finding the png file.

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              The problem is that when the code runs in the design view, the current directory is the Visual Studio application directory, not your project's directory. The file C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\google.png doesn't exist, so the Bitmap constructor throws an exception. You can use the DesignMode property[^] to test whether your control is in design mode. You should probably also check that the file hasn't been deleted:

              Image img;

              public tab()
              {
              InitializeComponent();

              if (!DesignMode && File.Exists("google.png"))
              {
                  img = new Bitmap("google.png");
              }
              

              }

              private void tab_Paint(object sender, PaintEventArgs e)
              {
              if (img != null)
              {
              e.Graphics.DrawImage(img, rect);
              }
              }

              You should be able to adapt the code from this SO answer[^] to resolve the image path at design time, if you need to see the image in the designer:

              private string ResolveImagePath()
              {
              string path = "google.png";
              if (DesignMode)
              {
              var typeResolver = (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
              if (typeResolver != null)
              {
              AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName();
              string basePath = typeResolver.GetPathOfAssembly(name);
              path = Path.Combine(basePath, path);
              }
              }

              return path;
              

              }

              public tab()
              {
              InitializeComponent();

              string imagePath = ResolveImagePath();
              if (File.Exists(imagePath))
              {
                  img = new Bitmap(imagePath);
              }
              

              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              _ Offline
              _ Offline
              _Q12_
              wrote on last edited by
              #6

              I find another solution but all thanks to your support, so many many thanks - it really helped me !!! control code:

                  public tab()
                  {
                      InitializeComponent();
                  }
              
              
                  Image img;
                  Rectangle rect = new Rectangle(3, 3, 22, 22);
                  
                  private void tab\_Paint(object sender, PaintEventArgs e)
                  {
                      if (img!=null)
                      {
                          e.Graphics.DrawImage(img, rect);
                      }
                      else
                      {
                          e.Graphics.FillRectangle(new SolidBrush(Color.Black), rect);
                      }
                  }
              
                  public Image tab\_Image
                  {
                      get { return img; }
                      set
                      {
                          img = value;
                      }
                  }
              

              and in design mode i set the property for image. And the image is seen in the design mode this way with no apparent errors. I will see in time how good solution is this one. .

              1 Reply Last reply
              0
              • _ _Q12_

                ___The bug is thrown at the initialization of the image. As you can see in the code below:

                    public tab()
                    {
                        InitializeComponent();
                    }
                
                
                    Image img = new Bitmap(@"google.png");
                    Rectangle rect = new Rectangle(5, 5, 22, 22);
                    
                    private void tab\_Paint(object sender, PaintEventArgs e)
                    {
                        e.Graphics.DrawImage(img, rect);
                    }
                

                ___ NEXT, i initialize it in the constructor, following your helping code like this:

                public partial class tab : UserControl
                {
                    public tab()
                    {
                        InitializeComponent();
                        if (!DesignMode && File.Exists("google.png"))
                        {
                            img = new Bitmap("google.png");
                        }
                    }
                
                
                    Image img;
                    Rectangle rect = new Rectangle(5, 5, 22, 22);
                    
                    private void tab\_Paint(object sender, PaintEventArgs e)
                    {
                        e.Graphics.DrawImage(img, rect);
                    }
                

                but another Error appear: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/281c49f0-e874-4b83-bff6-9bc6b8526d42/ddctlq3-8c112b8d-5685-4a43-a96e-21046443ef9a.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzI4MWM0OWYwLWU4NzQtNGI4My1iZmY2LTliYzZiODUyNmQ0MlwvZGRjdGxxMy04YzExMmI4ZC01Njg1LTRhNDMtYTk2ZS0yMTA0NjQ0M2VmOWEuanBnIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.1fkgpeEVDKRy30I70lftNt0WfbpoFl8LACty1N_fzsc[

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                _Q12_ wrote:

                another Error appear

                You missed the update to the tab_Paint event handler: :)

                private void tab_Paint(object sender, PaintEventArgs e)
                {
                if (img != null)
                {
                e.Graphics.DrawImage(img, rect);
                }
                }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                _ 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  _Q12_ wrote:

                  another Error appear

                  You missed the update to the tab_Paint event handler: :)

                  private void tab_Paint(object sender, PaintEventArgs e)
                  {
                  if (img != null)
                  {
                  e.Graphics.DrawImage(img, rect);
                  }
                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  _ Offline
                  _ Offline
                  _Q12_
                  wrote on last edited by
                  #8

                  Thank you for all your help - you really inspired me. It was a very very old problem and now you 'confirmed' it to me. Thats why im so thankful. And pointing that was more a design issue and not a compile issue, cemented it for me. :thumbsup: :cool:

                  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