Use System Images in C# - c#

I am creating a custom messagebox. How can I use system pictures such as Error, Information, Warning and etc, which I see in windows MessageBox? I want to access them directly!

Take a look at System.Drawing.SystemIcons. You should find them there.
Then set your PictureBox (assuming Winforms here) like this:
PictureBox1.Image = System.Drawing.SystemIcons.Warning.ToBitmap();

You need to look into the messagebox class a little further. You can specify a "MessageBoxIcon" when calling the method.
There are some good examples on how to acheive this here:
http://www.dotnetperls.com/messagebox-show

You can draw the System icons in your custom MessageBox by handling the Paint event, e.g.
void MyMessageBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawIcon(SystemIcons.Warning, 16, 16);
}

Related

Functions like Focus() and properties like Focusable not available for silverlight, only .NET?

Short version: Solutions like the following: How Do I Give a Textbox Focus in Silverlight?
Don't seem to work because functions like Focus and properties like Focusable DON'T EXIST for silverlight and only exist for .net apparently.
Background/Long Version: So I've been trying to get my XNA game to work on silverlight. Porting it was a nightmare but I managed to do it somehow. Currently I want to use ViewBox's so my game is as big as a players browser instead of a set size. When I tried to do it, it VISUALLY worked, but it was impossible to send any keyboard commands to the game. I'm pretty sure its preventing me from focusing it. When I google how to give focus, it gives me links like these where people are saying to use functions like .focus() which DON'T EXIST on silverlight 5 only .NET apparently. For example go here: http://msdn.microsoft.com/en-us/library/system.windows.controls.control(v=vs.110).aspx
It shows the .NET 4.5 version. If you change it to silverlight at the top, all the nice functions and settings disappear. Am I missing something here? How do I access the .Net versions of these classes in silverlight? If its not possible why are all those answers mentioned above use Focus() etc?
Assuming C#...
On your silverlight page, select yourPage_Created from write code menu
Add the following:
partial void YourPage_Created() // this line should be autogenerated
{
this.FindControl("YourControl").ControlAvailable += YourFieldAvailable;
}
private void YourFieldAvailable(object sender, ControlAvailableArguments e)
{
((System.Windows.Controls.Control)e.Control).GotFocus += YourRoutine;
}
private void YourRoutine(object sender, System.Windows.RoutedEventArgs e)
{
// do your focus specific code in here
}
You will now have a custom focus event for the specified control.
Hope this helps :)

How to change the background of a button programatically?

I need to change the BackgroundImage of a button on click of another button (In Windows Forms in C#). But I can't find out how to do it!!
I searched on the internet and found many examples and all of them use ImageBrush, ImageSource etc.... but these don't work on my application, it shows me errors every time I Use them.
I read on the internet that I have to add this namespace:
using Windows.UI.Xaml.Media.Imaging;
But it shows me an error on the begging which says to add this System before Windwons and when I add it:
using System.Windows.UI.Xaml.Media.Imaging;
it shows me than the error at UI .... I can't figure it out how to solve this!!
Please help me guys!
To Change Background Image of a button there are two ways i know.
Add the Image to the resources folder of your project and use.
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.ImageName;
}
Use Image.FromFile();
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "//Card1.png");
}
You are trying to use solutions for WPF in Winforms. This will not work.
The class you need is System.Drawing.Image (or System.Drawing.Bitmap, which inherits from Image).
Bitmap b = new Bitmap(#"C:\myBitmap.jpg");
myButton.Image = b;
Be sure to call Dispose on your Bitmap if and when it is no longer in use.

How to use tooltip on toolstripbutton

I'm trying to apply a tooltip to a toolstripbutton but it keeps giving me this error:
Operator '==' cannot be applied to operands of type 'System.Windows.Forms.Control' and 'System.Windows.Forms.ToolStripButton'
Any clue on how to solve this?
UPDATE:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (e.AssociatedControl == tBtn1)
{
using (Font f = new Font("Tahoma", 9))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTip1.GetToolTip(e.AssociatedControl), f);
}
}
}
ToolStripButton derives from ToolStripItem which has a ToolTipText property.
As already explained, the ToolStripItem does not derive from the Control class so provides its own implementation to render tool tips. This post may help you with customising the tooltip.
UPDATE: After trying this out in a Winform project and not having success, I searched other threads on SO, this is one that may be helpful to you:
Showing a tooltip on a non-focused ToolStripItem
The problem with the first is you can't set it to the button directly,
it doesn't inherit from Control, and the tooltip won't show up unless
you're over the strip but not over a button.
elsewhere
I was trying to do the same thing and determined it was going to be
pretty challenging and not worth it. The reason is that internally,
the .NET code is specifically designed to only show the tooltip if the
window is active - they are checking this at a Win32 level so its
going to be hard to fake the code out.
The user never accepted any of the answers as true, and it does seem at a glance that this may be a lot of work for little reward. Is this a project from scratch? If so, maybe you can do it using WPF, which is much more flexible than winforms.
Strangely enough, toolstripbutton class doesnt inherit from Control class unlike other System.Windows.Forms gui components. Perhaps in your code e.AssociatedControl is meant to be used with System.Windows.Forms controls. In short, I think MS hasnt decided to provide a tooltip for strip controls. I do not know your exact requirement, but for some alternative that might click, see this link.

Is there something like an "OnPaint" method in Silverlight?

Is there something like an "OnPaint" method in Silverlight?
Back when I was writing C++, I found that it was easy to use the OnPaint event to customize the display of a class to the screen?
Is there an equivalent in Silverlight? If I want to do something when a UserControl is displayed on the screen, what method would I override?
I noticed this post:
C# WPF OnPaint method alternative?
but it seems that in Silverlight, thre is no "OnRender" method for a UserControl class.
OnPaint was a workaround... to allow you to customise the appearance of controls. That was because you did not have much control over the default appearance of any controls in WinForms applications.
With Silverlight that all changes. Every control is now effectively skinned, using templates and styles, and there are few limitations on how you can customise them. There are far too many links so I just grabbed a couple for you.
Get yourself a good book on Silverlight and learn the proper way to work with it (not around it). This one is one of my favorites.
If you have specific things you are trying to do, to the appearance of user controls, best to list those instead and find out the best way to do it the Silverlight way. :)
You haven't specified what you're trying to do. If you just want to know when a frame is being rendered, the CompositionTarget.Rendering Event will tell you that. If you actually want to draw on the frame being rendered, you cannot do so.
It is LayoutUpdated.
As in:
...
this.LayoutUpdated += new EventHandler(LayoutUpdated);
}
void LayoutUpdated(object sender, EventArgs e)
{}

How can I access my applications images that are in my resources?

I'm going to briefly explain what I want my program to do.
I have a lot of Images on my form and I want the image source to change on MouseEnter event.
So, if a user moves the mouse over the button, I'd like the button to appear to be glowing. Of course I've made two images for the Image control. One normal, and one glowing. I'm trying to make a single event on mouseEnter for all of the images because I don't want to pollute my code with 60+ events all essentially doing the same thing.
Someone suggested I do something like this:
void HeroMouseEnter(object sender, EventArgs e)
{
((PictureBox)sender).Image = GetImage(((PictureBox)sender).Name)
}
Honestly, this would work exactly how I need it to. But I'm a bit confused is about the GetImage() method.
How exactly would I code this? All of my images, both the glowing and non glowing ones are already added to my resources. How would I summon them according to the PictureBox's name?
I tried making a dictionary with the key being the name of the pictureBox and the value being the resource file, but no dice.
Please help!
Something like this?
public Image GetImage(string name)
{
switch (name)
{
case "PictureBox1":
return Properties.Resources.Picture1;
case "PictureBox2":
return Properties.Resources.Picture2;
default:
return null;
}
}

Categories