|
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
|
|
|
|
|
___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[^]
Now what?
_ Your '!DesignMode' solution, is really nice and i didnt know it at all !
Awesome so far.
|
|
|
|
|
_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
|
|
|
|
|
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.
|
|
|
|
|
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();
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.
|
|
|
|
|
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.
.
|
|
|
|
|
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[^]
|
|
|
|
|
Hello,
We faced a the remote share folder issues using the C#.
1. We used C# to build up a windows service to copy the file to the remote share folder.
2. There is a AD domain account which can be accessed to the share folder: \\server\path which on a remote server.
Issues:
If the server was be reboot, we will got the error when the Windows service tried to access to the share folder \\server\path on remote server.
But if I sign to Windows with the AD domain account and re-open the remote share folder using the authorized account without any password. Then the Windows service can access to the remote share folder.
So, is there any way to use the C# to access without sign in to Windows?
Thanks,
Jowvid.
|
|
|
|
|
Create a service account that the windows service runs as.
Service Accounts (Windows 10) | Microsoft Docs[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I designed a CBT questions and answer using c# I have connected to the database using my sqlconne,but I don't know where to begin to start the code so the app can. Run or be active.or to function
Am stuck.
I don't know if u r getting this
|
|
|
|
|
|
Now you need to decide on the client, so your options are Web or desktop and do you include mobile options?
If your clients in house and behind the firewall then WPF or winforms are your options. If you want it on the web then pick one of thousands of web frameworks to use.
I would leave any mobile options for a future date.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Although, he could also do a web site that is restricted to the company's intranet as well.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Yah but I'm a desktop developer and have never liked the web stack since they killed off Silverlight.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Blazor brings the web app option back to within reason. No JavaScript needed. Just HTML and C#.
|
|
|
|
|
Member 14541728 wrote: I designed a CBT questions and answer using c# Then tell us how you did this. If you actually wrote code, describe it.
We can't read your mind.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
BillWoodruff wrote: We can't read your mind.
Wha??
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hello I want to print 2 copy of Invoice
1. Original Copy
2. Duplicate copy
i want above text is written on bill on right upper side!
and this event is occurs on one button click
|
|
|
|
|
"I want" is not a question.
Show us what you have tried, tell us where you are stuck, and maybe somebody will be able to help you.
If you were expecting somebody to write the code for you, you've come to the wrong site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Unless you're using "2 part paper", you can't claim an "original" and a "duplicate".
Maybe a "customer copy" ...
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Hi
I have two queries on a button click. One query inserts a new record in a access database table called Engineering, the other updates an already existing record. They both use the same set of variables which in turn get their values from textboxes on the form. The insert query runs fine. However when I run the update query I get a data type mismatch error. I have checked and rechecked the values to be placed in the database are correct. So I believe that the error is in the syntax of the where clause.
This is the Update query
The where clause gets it's string value 'n' from a textbox on the form called Name_textBox, Which in turn gets it's value from the Name field in the table.
string my_querry = "UPDATE Engineering SET Address = a, Phone = p, Email = em, DateBroughtIn = d, Donation = don, Repair = r, Warranty = war, Type = IT, Make = m, Model = MO, Serial = s, ReportedFault = RF, RepairedBy = i, FaultType = FT, Repaired = Rep, BER = BER, FaultFound = FF, DateTested = DT, DueDate = DueD, TestedBy = TestedBy, ConductorResistance = CPass, CReading = CR, InsulatorResistance = InRes, IReading = IR, EarthLeakage = EarthL, EReading = ER WHERE [Name] ='" + n + "'";
|
|
|
|
|
Since we're in the C# forum, care to show us how you assign the values to the parameters, and explain why you're not using a normal parameterized query?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hi
I use this code to add the values to the parameters.
cmd.Parameters.AddWithValue("@n", this.Name_textBox.Text);
cmd.Parameters.AddWithValue("@a", this.Address_textBox.Text);
cmd.Parameters.AddWithValue("@p", this.Phone_textBox.Text);
cmd.Parameters.AddWithValue("@em", this.Email_textBox.Text);
cmd.Parameters.AddWithValue("@d", this.Date_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@don", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@war", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@IT", this.Type_comboBox.Text);
cmd.Parameters.AddWithValue("@m", this.Make_textBox.Text);
cmd.Parameters.AddWithValue("@MO", this.Model_textBox.Text);
cmd.Parameters.AddWithValue("@s", this.Serial_textBox.Text);
cmd.Parameters.AddWithValue("@RF", this.ReportedFault_textBox.Text);
cmd.Parameters.AddWithValue("@i", this.RepairedBy_textBox.Text);
cmd.Parameters.AddWithValue("@FT", this.FaultType_comboBox.Text);
cmd.Parameters.Add(new OleDbParameter("@Rep", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@BER", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@FF", this.FaultFound_textBox.Text);
if (DateTested_textBox.Text != "")
{
cmd.Parameters.AddWithValue("@DT", this.DateTested_textBox.Text);
cmd.Parameters.AddWithValue("@DueD", this.DueDate_textBox.Text);
cmd.Parameters.AddWithValue("@TestedBy", this.TestedBy_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@CPass", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@CR", this.Conductor_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@InRes", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@IR", this.Insulator_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@EarthL", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@ER", this.Earth_textBox.Text);
}
else
{
cmd.Parameters.AddWithValue("@DT", "");
cmd.Parameters.AddWithValue("@DueD", "");
cmd.Parameters.AddWithValue("@TestedBy", "");
cmd.Parameters.AddWithValue("@CPass", false);
cmd.Parameters.AddWithValue("@CR", "");
cmd.Parameters.AddWithValue("@InRes", false);
cmd.Parameters.AddWithValue("@IR", "");
cmd.Parameters.AddWithValue("@EarthL", false);
cmd.Parameters.AddWithValue("@ER", "");
}
I have changed the checkbox lines from
cmd.Parameter.AddWithValue("@r, this.Repair_checkbox.checked); to
cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean));
The Bool variable r gets it's value with the line
bool r = Repair_checkBox.Checked;
However changing the line gives me an error saying "Failed Due to Parameter r has no default value"
The only checkbox line before the r variable is don and the corresponding checkbox is ticked, which is why I think it doesn't error.
I am only learning C# so the database will never be used. Therefore I am concentrating on getting it working rather than security, Which to me would make it even harder to understand.
|
|
|
|
|
Member 13261084 wrote: cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean)); That adds and creates a parameter, but doesn't assign a value for it yet. That's why it is complaining about the default value; it has no value assigned.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|