In user control designer I know how to add background image.
My image (16x16px) is located in user control. How to add this image in code?
How do I embed the image in user control so that when I bring the control to another project, the image also appears?
private Bitmap buttonResetImage = null;// <-- here I want to load my initial image.Then the user can change this image as he wishes;
public Bitmap ButtonResetImage
{
get { return buttonResetImage; }
set
{
buttonResetImage = value;
btnReset.BackgroundImage = buttonResetImage;
btnReset.BackgroundImageLayout = ImageLayout.Center;
}
}
Well since MDP does not respond I found the trick. I need to initialize this:
private Bitmap buttonResetImage = global::SliderControl.Properties.Resources.Reset;
If I use only:
private Bitmap buttonResetImage = SliderControl.Properties.Resources.Reset;
I get this error:
Error CS0117 'SliderControl' does not contain a definition for 'Properties' SliderControl
https://i.imgur.com/az52MOP.png
In order to create a user control that has any kind of resources like :
images
fonts
audio
icon
and etc, you can create them in a separate project (called Windows Forms Control Library) and add resources in it.
after all, the result(in this case, a dll file) contains all the resources and code base you need.you can use it in any where you want.
I'm trying to change the Background Pic of a Button to another Pic that I saved in the Resources Folder. Currently The Code is the following (FinalDStagePickBANNED.png being the Pic I want it to change to):
private void FinalD_Click(object sender, EventArgs e)
{
BanFDButton.Image = (Image)Properties.Resources.ResourceManager.GetObject(FinalDStagePickBANNED.png);
}
The Thing is, that it apparently finds no Pics in the Resources-Folder, although there are like 10 pics.
Solved: The solution to the problem was to put the FinalDStagePickBANNED into quatation-signs, and to remove hte .png ending.
Im trying to make my picture change when a button is clicked so that the player can see the character attack. When they do so, so the picture should change to the attack image when the button is clicked and then back to the old image when the opponents turn starts.
All the images are saved in a folder named "images" inside the project folder, which is inside the "WindowsFormsApplication1" folder, but it says it cant find namespace images inside windowsformsapplication1
Here is the code I am using to change the image:
private void ArBut_Click(object sender, EventArgs e)
{
if (playerturn == true)
{
Ar.Image = global::WindowsFormsApplication1.images.archeratack.jpeg;
drhp = drhp - 15;
DrHP.Text = drhp.ToString();
checkend();
playerturn = false;
dratak();
}
}
You can't just set the image like that. First of all, because thats not a String, it thinks you are trying to access a code element. Obviously images is not a namespace or class within your code, so you get the compiler error.
You need to use PictureBox.Load (MSDN)
Ar.Load(#"./images/archeratack.jpeg");
You could try to create an Image object and use Image.FromFile("url")
Got another problem. I have a solution "solution1" in this solution are 3 projects "projectA" , "projectB", "projectC". I have made a reference to projectB & projectC from projectA. I have added the 2 other projects to my solution folder which contains my projectA already.
I've added them in solution explorer and made a reference as I said.
Now when I click a button on a form from projectA:
private void formProjectAButton_Click(object sender, EventArgs e)
{
this.Hide();
projectB.Form1 fs = new projectB.Form1();
fs.Show();
}
the form from projectB shows no problem. But then when I use the form from projectB (which contains a picturebox and a textbox. The picturebox just contains a static image which is located in my bin/debug folder. The textbox contains text that I load when the picturebox is clicked):
private void picturebox1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
inputStream = File.OpenText("gebod1.txt");
string line = inputStream.ReadLine();
while (line != null)
{
textBox1.AppendText(line);
line = inputStream.ReadLine();
}
textBox1.Visible = true;
}
But now when I click the picturebox to load the text from the textfile (which is in projectB debug folder) into the textbox I get an filenotfoundexeption.
Anything I can do to fix this?
Thanks in advance.
You're using a relative path. The relative path is the current working folder.
So if you have a Project A in C:\Foo\ProjectA and Project B in C:\Foo\ProjectB, your text file in project A will be in C:\Foo\ProjectA\bin\Debug\foo.txt. You'll need to navigate to the correct folder relative to your working folder, or specify a full path.
For example, you could try something like this. Obviously, this is just an example to give you an idea of how you accomplish what you're after.
File.OpenText("..\..\..\ProjectA\bin\Debug\gebod1.txt");
The .. in this context means "up one folder in the tree". So if you're in C:\Foo\Bar, a relative path of ..\Baz would mean C:\Foo\Baz\
Another option would be to add a post-build step to ProjectA to copy the file to a known folder, and then reference the file out of that location.
I am developing a windows application.
I have 3 forms:
I want to change the backcolor of all the 3 forms to the color selected by the user.
I have used the following code I am able to change the backcolor but When I exit the application and restart it I am not able to get the color that user has set. I am getting the default colour only.
Is it possible to retain the colour selected by the user and use it as backcolor when the user restarts the application.
CODE
In Form1
ColorDialog c1 = new ColorDialog();
public static System.Drawing.Color bkc;
private void button1_Click(object sender, EventArgs e)
{
DialogResult res = c1.ShowDialog();
if (res == DialogResult.OK)
{
bkc = c1.Color;
this.BackColor = bkc;
MessageBox.Show(Convert.ToString(bkc));
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2();
obj1.BackColor = bkc;
obj1.Show();
}
In Form 2
CODE
private void button2_Click(object sender, EventArgs e)
{
Form3 obj1 = new Form3();
obj1.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
this.BackColor = Form1.bkc;
}
In Form3
CODE
private void button2_Click(object sender, EventArgs e)
{
Form1 obj1 = new Form1();
obj1.Show();
}
private void Form3_Load(object sender, EventArgs e)
{
//Form1 obj2 = new Form1();
this.BackColor = Form1.bkc;
}
In the color dialog box I am selecting a color and pressing Ok button the color is also changed but when I restart the application I dont get the colour which I set using the Color Dialog.I want to retain this setting so that the user can get the desired color without resetting it each time the application is executed.
The above code does not generate any error.
can anybody help me out in performing this task?
Thanks in advance!
You will need to save the value somewhere such as the Application.exe.config:
// Open App.Config of executable
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("BackgroundColour",
bkc + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Here is a C# full code example: Using System.Configuration.ConfigurationManager Example
The suggestion of using the application configuration file is close, but there are two things wrong with it.
First, all users of the application share the same application configuration file. If you have multiple users (on a network, say, or different users on the same machine), storing a user's preference in the application configuration file will change that setting for all users. A second thing wrong with it is that under a default installation on Vista it won't work anyway: by default, Vista doesn't give the user write access to anything under the Program Files directory, so saving changes to the application configuration file will throw an exception.
The right answer is to use user settings. These get stored in the application's user settings file, which lives in a (deeply nested, and OS-version-dependent) subdirectory of the user's home directory. The ConfigurationManager loads these settings at runtime, and lets you update and save them in your code. There's an entire infrastructure built into Visual Studio to make this (relatively) easy, which is good, because doing it properly involves writing a spooky amount of code against the ConfigurationManager class. Here's how it works:
If you look under the Properties of your VS project, you'll see an item called Settings.settings. When you double-click on this, it will show you a grid that lets you add settings to your project. You give the setting name, choose its the data type and default value, and, crucially, the scope. The setting can be application scope, in which case its value will be common to all users of the application and be stored in the application configuration file. Or it can be user scope, in which case each user can have his own value for the setting, and the setting will live in the user settings file.
When you add a setting to this grid, VS generates code to make the setting available to your code. Basically, it creates a class that exposes these settings to your code as properties of a singleton object. (You can see this code if you want to get an idea of what this is saving you from having to do yourself; it gets stored in the 'Settings.Designer.cs' file created under 'Settings.settings' in the project view.) It also, conveniently, regenerates this class every time you change the information in the Settings grid. Once you create a setting in the settings grid, you can reference it in your code thusly:
ctl.BackColor = Properties.Settings.Default.BackColor;
User settings can be modified by your code:
Properties.Settings.Default.BackColor = newBackColor;
And you can save them to the user settings file like this:
Properties.Settings.Default.Save();
Having these settings being exposed as properties of a class is useful for a lot of reasons. One of the most important is that since they're properties (and not, say, dictionary entries accessed by a key, which is how most code that people write against the ConfigurationManager class works), there's compile-time checking of the names you're using in code. You're not ever going to get a NullReferenceException at runtime if you misspell the name of a setting; you'll get an error when you compile it instead.
There are a few subtleties to using user settings. One of the less obvious ones is: what happens when you produce a new release of the software? The user settings are stored in a directory that's keyed to the version number of the program; if you release a new version, the user settings file for it won't exist. How do you keep the user from losing all of his settings when he upgrades your program?
This is also built in to that Settings class; all you need to do is this:
if (Properties.Settings.Default.UpgradeSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeSettings = false;
}
This will copy the user's settings from the previous release into the settings file for the new release.
Why dont you create an event that all three forms listen to and get them to change the background colour when listening to the "change colour" event? And you could store the colour in a static variable so that when the form gets loaded, the background colour could be set to that stored in the variable.
In order for the screen to remember the colour settings, why not store the colour selected in a user preferences file? Try the "IsolatedStorage" functionality to save a preferences file.
You are doing it wrong way.
How will the application remember the user choice of backcolor?
The app runs in memory & shows chosen backcolor till its terminated.
Read on this & take it forward.
EDIT: Also, it is not right thing to use Form1.BackColor in Form2.
Open Form1, change backcolor, close Form1 & open Form2 to see what happens (you might see that Form1 opens again).