Error when i try to add my control to form1
-
- 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
-
- 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
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 theBitmap
constructor throws an exception. You can use theDesignMode
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
-
- 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
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 -
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 theBitmap
constructor throws an exception. You can use theDesignMode
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
___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); }
-
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 theBitmap
constructor throws an exception. You can use theDesignMode
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
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.
-
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 theBitmap
constructor throws an exception. You can use theDesignMode
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
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. .
-
___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); }
_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
-
_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