in XAML i created button and when i click on the button then background will be changed to red color(because i set it) but if i close my application and then again start application so background is not red. What i need to do is keep background there like before(when i clicked on button) so have red background if i again start application. Your help will be for me very important. Thank you experts. Btw this is code for button now: :).
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
background.Background = new SolidColorBrush(Colors.Red);
}
you need to save the selected color; or save somwething some you can determine what color it has to be. In the constructor or your page read this value and set the right color.
you can use localsettings to save this.
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
for example.
In the constructor:
if ( ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
{
background.Background = new SolidColorBrush(Colors.Red);
}
full sample:
public MainPage()
{
this.InitializeComponent();
if ((string)ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
Related
I tried to change the background of the buttons when a button is clicked it doesn't color the background of the button.
My attempt:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
//rest all red
Ans2.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans3.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans4.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Thread.Sleep(1500);
}
private void Ans1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button; <-- sender is the current button
button.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
}
It seems that your conversion is the problem.
bc.ConvertFromString("#FF3C9C27") most likely returns a System.Windows.Media.Color.
System.Windows.Media.Color as SolidColorBrush returns null however.
This should give you the desired result:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FF3C9C27"));
//rest all red
Ans2.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans3.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans4.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
}
About the Thread.Sleep "issue": you could use a Timer instead.
To change the background color of your button, inside your clicked function that's been auto-generated add the following code:
Ans1.BackColor = Color.Green;
Ans2.BackColor = Color.Red;
Ans3.BackColor = Color.Red;
Ans4.BackColor = Color.Red;
Hope this helps!
I want to add icon inside a button. Here is my code
private void Printbutton_Click(object sender, EventArgs e)
{
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
if (SetupThePrinting())
printDocument1.Print();
}
The problem here is that the icon doesn't appear at first , it appears when I click to the button.
What's wrong here ?
you added the icon in printbutton_click event instead defining it in Form initializecomponents
public Form2()
{
InitializeComponent();
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
}
private void Printbutton_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
printDocument1.Print();
}
Like this
public Form1()
{
InitializeComponent();
// Assign an image to the button.
button1.Image = Image.FromFile("C:\\Yourfolder");
// Align the image and text on the button.
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
button1.FlatStyle = FlatStyle.Flat;
}
private void button1_Click(object sender, EventArgs e)
{
// Your print code.
}
i am having a problem with my textblock navigating to a new page! , well the navigation is not the problem!, i have a viewbox that has storyboards connected with it, so when i click on it, the viewbox expands or collapse, but next to this viewbox i have a textblock that has a colour on it, when i click on that coloured textbox, it should navigate to another screen , but what it does is, it first expands the viewbox then in navigates!, any ideas?
after clicked it opens up , and then navigates,want this to not expand when clicked on the green block
this is coding for viewbox and the textblock_tap event:
private void viewboxHeader_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
vb = (Viewbox)sender;
Storyboard sbExpand = new Storyboard();
Storyboard sbCollapse = new Storyboard();
sbExpand = (Storyboard)vb.TryFindResource("Expand");
sbCollapse = (Storyboard)vb.TryFindResource("Collapse");
string Status = vb.Tag.ToString();
if (Status == "1")
{
descend = false;
sbCollapse.Begin();
vb.Tag = 0;
}
if (Status == "0")
{
descend = true;
sbExpand.Begin();
vb.Tag = 1;
}
}
private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
this.NavigationService.Navigate(new Uri("/UI/frmWFDocumentDetail.xaml", UriKind.Relative));
}
Thanks in advance!
i use Visual Studio 2012/silverlight/windows phone 8 app! windows 8 sdk
Just mark the event as handled to prevent it from propagating to the parent control:
private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
e.Handled = true;
this.NavigationService.Navigate(new Uri("/UI/frmWFDocumentDetail.xaml", UriKind.Relative));
}
I want to make a custom button control (image button is ok) like this one.
I'm a new user, so I can't post image here. So I uploaded the picture here
I'm kind of desperate right now after trying some tutorials
Any suggestion is highly appreciated.
Thanks
Updated 08/10/2019: I asked this question so many years ago, and at that time I didn't have the permission to upload image, so the image I uploaded to the third party site is long gone now. I got many requests about re-uploading the image, so here is what I remember from that project I did eight years ago, I just find some random images about window form that match my memory
This is when the button is in normal state
This is when the button is hovered or clicked, with the rounded border
You could create a class that inherits from Button to keep all your styling in one place. To do the hover and pressed states you can override the mouse enter / leave events of the button and change style.
Here is an example from one of our projects (I changed the colours but your get the idea). Where we change some colours you could switch the images.
public class MossieButton : Button
{
private static Font _normalFont = new Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
private static Color _back = System.Drawing.Color.Grey;
private static Color _border = System.Drawing.Color.Black;
private static Color _activeBorder = System.Drawing.Color.Red;
private static Color _fore = System.Drawing.Color.White;
private static Padding _margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
private static Padding _padding = new System.Windows.Forms.Padding(3, 3, 3, 3);
private static Size _minSize = new System.Drawing.Size(100, 30);
private bool _active;
public MossieButton()
: base()
{
base.Font = _normalFont;
base.BackColor = _border;
base.ForeColor = _fore;
base.FlatAppearance.BorderColor = _back;
base.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
base.Margin = _margin;
base.Padding = _padding;
base.MinimumSize = _minSize;
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
UseVisualStyleBackColor = false;
}
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
if (!_active)
base.FlatAppearance.BorderColor = _activeBorder;
}
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
if (!_active)
base.FlatAppearance.BorderColor = _border;
}
public void SetStateActive()
{
_active = true;
base.FlatAppearance.BorderColor = _activeBorder;
}
public void SetStateNormal()
{
_active = false;
base.FlatAppearance.BorderColor = _border;
}
}
Can't see the picture but I guess you can change the border of the button and set a background image.
button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");
I think the simplest way is set some properties of the button like below and
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
then write the code for
private void button1_Click(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "OnClick";
}
private void button1_MouseEnter(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Enter";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Normal";
}
Update:
I don't know whether I am going correct or not but I think You can also achive your goal by putting a Button and a label inside a panel and arrange them according to your choice. Make the button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat at initial with Label.Text="Normal". Then on Mouse enter to the Panel draw a rectangle with a border around the button and change the text of the label to "Hover". Like that Clicking on the Panel also you change the rectangle border according to you and make the label.Text="OnClick".
I want to add a label to my form , and I want it without any color- I want just it's text to be visible, I don't find this option in the label's properties, can anyone help me please?
Do you want to make the label (except for the text) transparent? Windows Forms (I assume WinForms - is this true) doesn't really support transparency. The easiest way, sometimes, is Label's Backcolor to Transparent.
label1.BackColor = System.Drawing.Color.Transparent;
You will run into problems though, as WinForms really doesn't properly support transparency. Otherwise, see here:
http://www.doogal.co.uk/transparent.php
http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
http://www.daniweb.com/code/snippet216425.html
Setting the parent of a usercontrol prevents it from being transparent
Good luck!
If you picture box in the background then use this:
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
Put this code below InitializeComponent(); or in Form_Load Method.
Ref: https://www.c-sharpcorner.com/blogs/how-to-make-a-transparent-label-over-a-picturebox1
You are right. but here is the simplest way for making the back color of the label transparent
In the properties window of that label select Web.. In Web select Transparent
:)
this.label1.BackColor = System.Drawing.Color.Transparent;
Let's view 2 possible cases.
Background is a color.
Double-Click the form background in VS constructor. Then, write this code:
/*
This code will set all your object's background color to the same as the form.
This should be written in the body of <FormName>_Load(object, EventArgs).
*/
Control[] objs = new Control[] { /* your object list, e. g { myLabel, myPicture } */ };
foreach (Control control in objs) {
control.BackColor = Color.Transparent;
// OR
control.BackColor = this.BackColor;
}
Background is a PictureBox.
This is also easy. We just need to make all objects as children of your PictureBox and set it's color to transparent. Code:
/*
This code will set all your object's background to transparent and show the PBox.
This should be written in the body of <FormName>_Load(object, EventArgs)'s foreach loop.
Put everything before it the same as in 1st code fragment.
*/
control.Parent = back;
control.BackColor = Color.Transparent;
Let's see the pictures.
Color
Before
After
PictureBox
Before
After (here foreground were changed, don't mind this)
Generally, labels and textboxes that appear in front of an image is best organized in a panel. When rendering, if labels need to be transparent to an image within the panel, you can switch to image as parent of labels in Form initiation like this:
var oldParent = panel1;
var newParent = pictureBox1;
foreach (var label in oldParent.Controls.OfType<Label>())
{
label.Location = newParent.PointToClient(label.Parent.PointToScreen(label.Location));
label.Parent = newParent;
label.BackColor = Color.Transparent;
}
This uses Graphics.CopyFromScreen so the control needs to be added when it's visable on screen.
public partial class TransparentLabelControl : Label
{
public TransparentLabelControl()
{
this.AutoSize = true;
this.Visible = false;
this.ImageAlign = ContentAlignment.TopLeft;
this.Visible = true;
this.Resize += TransparentLabelControl_Resize;
this.LocationChanged += TransparentLabelControl_LocationChanged;
this.TextChanged += TransparentLabelControl_TextChanged;
this.ParentChanged += TransparentLabelControl_ParentChanged;
}
#region Events
private void TransparentLabelControl_ParentChanged(object sender, EventArgs e)
{
SetTransparent();
if (this.Parent != null)
{
this.Parent.ControlAdded += Parent_ControlAdded;
this.Parent.ControlRemoved += Parent_ControlRemoved;
}
}
private void Parent_ControlRemoved(object sender, ControlEventArgs e)
{
SetTransparent();
}
private void Parent_ControlAdded(object sender, ControlEventArgs e)
{
if (this.Bounds.IntersectsWith(e.Control.Bounds))
{
SetTransparent();
}
}
private void TransparentLabelControl_TextChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_LocationChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_Resize(object sender, EventArgs e)
{
SetTransparent();
}
#endregion
public void SetTransparent()
{
if (this.Parent!= null)
{
this.Visible = false;
this.Image = this.takeComponentScreenShot(this.Parent);
this.Visible = true;
}
}
private Bitmap takeComponentScreenShot(Control control)
{
Rectangle rect = control.RectangleToScreen(this.Bounds);
if (rect.Width == 0 || rect.Height == 0)
{
return null;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
return bmp;
}
}
An easy way to have a label with a picture behind it is to use the Image Property of the label itself. This will print the text over the top of the picture, and enable you to align the image (top/bottom/left/right/centre) as required.picture