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.
Related
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.
i want to navigate txtBox1.text and bg2.source to another xaml at the same time
i try to use this
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MultiGame), bg2.Source);
this.Frame.Navigate(typeof(MultiGame), txtBox1.text);
}
it doesnt work. I hope someone can help me!!!
Of course this does not work. As you call the Navigate method multiple times the Page MultiGameis loaded 2 times with each one of the parameters.
Why don`t you pack the two variables in one object array, and pass this to the navigate method?
Like
this.Frame.Navigate(typeof(MultiGame),new object[] {bg2.Source, txtBox1.text});
In the MultiGame class you now have to watch to the PageNavigated event. The navigation data is now your array.
Try to find solution for next - on mouseEnter - show picture on TabControl page and on MouseLeave - clear it.
Currently done - just show pic on tab.
There are a lot of question refered to "How to show" picture - this and this or this ant also a lot of others.
For this I use next way - add imageList and just show pic on mouseEnter -
private void tabControlPages_MouseEnter(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = 0;
}
For removing - try to read about some methods on MSDN - but have found nothing (maybe not found).
As variant think, can put in collection transparent icon and just change it on mouseLeave, but think it's not a perfect solution. If use imageListIcons.Images.Clear(); - it's fully clear ImageList - as and expected.
So, are ther is some solution avaliable for correct way to hide/show icon on tab from tabControl ?
Just set ImageIndex = -1 to remove the tab icon:
private void tabControlPages_MouseLeave(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = -1;
}
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);
}
I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).