I have a C# winForm project that uses a ContextMenuStrip. I dynamically add ToolStripMenuItems to the ContextMenuStrip based on use interaction. When I add a new ToolStripMenuItem I set it's Text property and Image property. I don't know how to the set the Image property without getting the image from the location where it's at. How do I add the imagine to my project? Here's an example of what my code is doing
ContextMenuStrip cxtMnuStrp = new ContextMenuStrip;
private void Button_Click(object sender, EventArgs e)
{
// some filtering and logic
// to determine weather to
// create and add a ToolStripMenuItem
// blah, blah, blah...
ToolStripMenuItem item = new ToolStripMenuItem("uniqueName");
item.Image = Image.FromFile(#"C:\MyFolder\MyIcon.ico");
if (cxtMnuStrp.Items.ContainsKey(item) == false)
cxtMnuStrp.Items.Add(item);
}
With "item.Image = Image.FromFile(#"C:\MyFolder\MyIcon.ico")" When I distribute my each machine would have to have the "C:\MyFoler" directory and also have the "MyIcon.ico" on their computer in the "C:\MyFoler" directory.
Plus it doesn't seem right that I have hit the hard drive each time I want to add an icon to my ToolStripMenuItem
You could save your icons in a resource file or save the image as a embedded resource.
Using the resource file.
Adding and Editing Resources (Visual C#)
Adding the images as a embedded resource
Visual Studio: How to store an image resource as an Embedded Resource?
You code will will be as shown below.
private void BuildContextMenuStrip_Click(object sender, EventArgs e)
{
ContextMenuStrip cxtMnuStrp = new ContextMenuStrip();
ToolStripMenuItem item = new ToolStripMenuItem("uniqueName") { Image = WindowsFormsApplication2.Properties.Resources.Search.ToBitmap() };
if (cxtMnuStrp.Items.Contains(item) == false)
cxtMnuStrp.Items.Add(item);
this.ContextMenuStrip = cxtMnuStrp;
}
Note:
If you added a icon to the resource file. You will have to convert it to a image by using .ToBitmap().
The images are now available in intellisense instead of using path strings.
I have added the contextMenuStrip to the form in the example above.
In addition to the information provided on how to add resources in the links above you can add them as follows
Related
I just found DockPanel Suite and am learning to use it.
I downloaded v2.9 and am using it with C# in VS2013. I created a simple Windows Forms MDI app. It has one type of child form which is a simple form with only a rich text box control in it. I loaded a few of these as documents. I added an icon to the document's tab to indicate whether the rich text box text has been saved or not.
I change the tab icon anytime the user types in the rich text box through the use of the text_Changed event:
private void txtCode_TextChanged(object sender, EventArgs e)
{
//The text has changed, set the warning icon.
this.Icon = MyApp.Properties.Resources.Warning;
}
The problem I have is that the icon does not update when running at full speed. It updates fine when I am single stepping through the above event. It also updates fine when I load a file within the form's Load event;
private void frmCode_Load(object sender, EventArgs e)
{
//Get the full file path.
string sFile = Path.Combine(cCoCoIDE.CodeBaseRootFolder, Path.GetFileNameWithoutExtension(cCoCoIDE.CodeBaseFile), this.Text);
//Load the source file into the code window.
//A few validations.
//Make sure the file exists.
if (File.Exists(sFile))
{
//File is there. Is it empty? Load only if not empty.
if (new FileInfo(sFile).Length > 0)
{
txtCode.LoadFile(sFile);
//I had to add the following because the text changed
//event fires when I load the file and I need to start
//off with the green checkmark icon.
this.Icon = CoCoIDE.Properties.Resources.GreenChk;
}
}
}
I tried the following after I change the icon (in the Text Changed event):
this.ShowIcon = true;
this.Refresh();
this.Update();
Note: I tried these one at a time. The above is just a list of methods.
Can anyone help me with this? Thank you! SgarciaV
I found the solution. See here:
Dockpanel Suite: Image not updating
Why are there 2 of the same icons in Visual Studio?
If you type:
<shell:ApplicationBarIconButton Text="new document" IconUri="" /> and then open the Properties pane and open up the ComboBox for the ApplicationBarIconButton element, you'll notice that this comb box has an add button and a new button icon. And when you look at both icons - they're the same.
But why?
I know that, in context, both new, and add, can have different meanings/perform different actions:
E.g. New could create a new document, while add could attach something/add something to the currently-open document.
But then if that's the reasoning - then both icons should be different as this could potentially lead to confusion because the default state of the Application bar icon's is set such that the text of the icon is not visible unless you tap on the ... to the bottom right of the screen. So if I had both an add and a new button in the ApplicationBar menu, while in default state, this could be very confusing and will force the user to open the menu just to see which button is which. Which goes against the purpose of hiding the menubar text in the first place, doesn't it?
I try not to spend a whole lot of valuable development time trying to figure all the reasons as to why Microsoft decides to implement one default image over another in a development environment. It is my responsibility as the developer to choose exactly how I want the program to look and feel.
There are a lot of standard icons to choose from that come bundled with the SDK.
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons\
Additionally, here is how someone can create their own custom Application Bar. One alternative button icon approach is to continuing using the standard add.png image for the New button and use the check.png image for the Add button if it is really necessary to have both types of actions on the same Application Bar.
public partial class MyPage : PhoneApplicationPage
{
public MyPage()
{
InitializeComponent();
BuildApplicationBar();
}
private void BuildApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsVisible = true;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsMenuEnabled = true;
// Create new buttons
ApplicationBarIconButton AppBarAddButton = new ApplicationBarIconButton(new Uri("/Assets/check.png", UriKind.Relative));
AppBarAddButton.Text = "Add";
AppBarAddButton.Click += new EventHandler(AppBarAddButton_Click);
ApplicationBar.Buttons.Add(AppBarAddButton);
ApplicationBarIconButton AppBarNewButton = new ApplicationBarIconButton(new Uri("/Assets/add.png", UriKind.Relative));
AppBarNewButton.Text = "New";
AppBarNewButton.Click += new EventHandler(AppBarNewButton_Click);
ApplicationBar.Buttons.Add(AppBarNewButton);
}
private async void AppBarAddButton_Click(object sender, EventArgs e)
{
//TODO: Do something for the add click action
}
private async void AppBarNewButton_Click(object sender, EventArgs e)
{
//TODO: Do something for the new click action
}
}
So basically what I want is the user presses a browse button and a FolderBroswerDialog pops up. The user then selects a folder and the ViewList is then populated with all the images in that folder in Icon view. How can I do this? The code I currently have will select all the files from a folder and display them in the ListView, however there are no icons. How can I get icons?
Here is the code I currently have...
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog browsefolder = new FolderBrowserDialog();
if (browsefolder.ShowDialog() == DialogResult.OK)
{
listView1.Items.Clear();
string[] myfiles = Directory.GetFiles(folderPicker.SelectedPath);
foreach (string file in myfiles)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem myitem = new ListViewItem(fileName);
myitem.Tag = file;
listView1.Items.Add(myitem);
}
}
}
This is not so easy to do in an accurate and performant way. The quick and dirty way is to use Icon.ExtractAssociatedIcon() and add the returned icon to the ImageList associated with the list view. But you won't get the exact same icons you'd see in Explorer. That requires pinvoking SHGetFileInfo(), painful to do yourself but the code is easy to google.
An entirely different approach is to embed the Explorer window into your own form instead of using a ListView. With the major advantages that you'll get the exact same look and you'll automatically get the background thread that looks up icons while your program keeps responsive. With the disadvantage that this won't work for XP. The classes you need are part of the Windows API Code Pack.
I am writing project in asp.net C#. I want to create Image programmatically by the following code:
protected void Page_Load(object sender, EventArgs e)
{
Image image = new Image();
image.ID = "image11";
image.ImageUrl = "a.jpg";
image.ImageAlign = ImageAlign.AbsMiddle;
image.Visible = true;
}
But nothing is displayed when I run the project.
How to create image from file and display it in the page by writing code in .cs file?
You have created an image control but you have not added it to your form. Write the below code to add the image control to your form.
form1.Controls.Add(image);
At this point, you have just created an image, but you haven't added it to a control or page context to be displayed. You essentially said
int x = 10;
but then never did anything with x.
ASP.NET uses composition, so it maintains a collection of controls, with each control also containing a collection of children nodes. You need to add the image to a container. For instance, if you want to add the image to a panel named myPanel, it would be
myPanel.Controls.Add(image);
Check out this article.
You will need to create panel and then you will have to add that image to panel.
Panel Panel1= new Panel();
form1.Controls.Add(image);
I am developing a window form in C#. In my window form there is a picture box. I want if the user does not select an image then the default image will be load in picture box which is save in my project folder.
thanks in advance
I guess you want to know how you can get the picture from the project folder right?
First add the picture to you project (add existing item) and set the Build Action to Embedded Resource:
then the following code do the trick:
private void SetPicture()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
{
var img = new Bitmap(imgStream);
Picturebox.Image = img;
}
}
where "DataGrid" is MY project-name (you have to insert your own) and "TestImage.jpg" is the name of your Image (if you put it into a folder you might have to give the foldername too).
Picturebox is a PictureBox-Control I set the image to.
Either set an image in the designer and override it in code
for instance you can place this after user selection:
if (someConditionIndicatingUserAction)
pictureBoxControlName.Image = UserSelectedImage;
Or, you can store a default value in a variable and set the picturebox image to it (if the user selects nothing, you don't change the value, and if he does, change it accordingly)