I have a little problem of mine to solve. I have a button which has an image background.I tried to color the whole button but the image can not be seen after coloring the whole button. How can I edit this "imagebutton" like in this example? http://i.stack.imgur.com/XaQQQ.png
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bura
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.BackgroundImage != null)
{
button2.BackgroundImage = null;
button2.BackColor = Color.Black;
}
else {
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
private void button3_Click(object sender, EventArgs e)
{
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
}
This
Just made a button by using the designer with the following code:
this.button1.BackColor = System.Drawing.Color.DodgerBlue;
this.button1.BackgroundImage = global::WindowsFormsApplication.Properties.Resources.ChargeImage;
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button1.Image = global::WindowsFormsApplication.Properties.Resources.DatabaseImage;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 160);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = false;
And that is the result:
So what is your problem?
You can edit your pictures with a method like this:
private static void DrawLinesOnBitmap(Bitmap bmp)
{
using (var p = new Pen(Color.Black, 5))
{
using (var g = Graphics.FromImage(bmp))
{
g.DrawLine(p, 0, 0, bmp.Width, bmp.Height);
}
}
}
This method adds a line from the left top corner to the right bottom corner. Just draw some more lines and you should get your wanted result.
Related
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyTest
{
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private GroupBox lastGB = null;
public Form1()
{
InitializeComponent();
GroupBox gb = new GroupBox();
gb.Location = new Point(100, (lastGB == null ? startingY : lastGB.Bounds.Bottom));
gb.Size = new Size(1220, 400);
gb.BackColor = SystemColors.Window;
gb.Text = "";
gb.Font = new Font("Colonna MT", 12);
this.Controls.Add(gb);
}
It's creating a small line on the top of the groupbox and I don't want this line to show.
And I want to write some text on it in the middle of it.
How can I make it just complete white ? And how to write some text in the middle on the groupbox ?
The main idea is to create over the form a white sheet or box with text inside that's it.
It looks like your intention is to put subsequent boxes just below the last box. As mentioned, a Label would probably be best. I'd also move that code to a method you can call over and over to keep from repeating code elsewhere. You could pass a message to display in the Label. Also, don't forget to update the reference to the "last box" when ever you create a new one:
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private Label lastLbl = null;
public Form1()
{
InitializeComponent();
AddLabel("Hello World");
}
private void button1_Click(object sender, EventArgs e)
{
AddLabel(DateTime.Now.ToString());
}
private void AddLabel(String msg)
{
Label lbl = new Label();
lbl.Location = new Point(100, (lastLbl == null ? startingY : lastLbl.Bounds.Bottom));
lbl.Size = new Size(1220, 400);
lbl.BackColor = Color.White;
lbl.Text = msg;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Font = new Font("Colonna MT", 12);
this.Controls.Add(lbl);
lastLbl = lbl;
}
}
Heres the code I'm using:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Colors_Form
{
public partial class Form1 : Form
{
public int RedValue;
public int GreenValue;
public int BlueValue;
public SolidBrush sb = new SolidBrush(Color.FromArgb(255, 0, 0, 0));
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
RedValue = (int)RedNumBox.Value;
GreenValue = (int)GreenNumBox.Value;
BlueValue = (int)BlueNumBox.Value;
//SolidBrush sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue ));
g.FillRectangle(sb, 10, 150, 300, 200);
}
private void RedNumBox_ValueChanged(object sender, EventArgs e)
{
sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));
}
private void GreenNumBox_ValueChanged(object sender, EventArgs e)
{
GreenValue = (int)GreenNumBox.Value;
sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));
}
private void BlueNumBox_ValueChanged(object sender, EventArgs e)
{
BlueValue = (int)GreenNumBox.Value;
sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));
}
}
}
What I'm trying to do is get the solid brush to change color when the 'valuechanged' event is triggered. Unfortunately, I have been unsuccessful. I have tried variations on:
sb = sb.Color(Color.FromArgb(255,RedValue, GreenValue, BlueValue));
but that code fails as well. I placed the solid brush globally, so there could be access to it from any method created. I have looked everywhere and cannot find any color update instructions and all I see is 'new brush' and now, I'm unsure how to proceed. I appreciate any assistance.
You should move the brush into the paint event and remove the global variable. SolidBrush should be disposed of when its done. Better to do that in one place than all the change events.
using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue )))
{
g.FillRectangle(sb, 10, 150, 300, 200);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Today I've tried Visual Studio + WinForms (I've been using Xamarin)
I dont use designer, just create empty project and link needed libraries.
So, my timer doesn't work properly, it ticks only in minimized form.
App.cs
using System;
using System.Windows.Forms;
namespace Line
{
class App
{
[STAThread]
public static void Main()
{
Application.Run(new Window());
}
}
}
Window.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Line
{
class Window : Form
{
Timer timer;
PictureBox pictureBox;
Bitmap bmp;
public struct Circle
{
public PointF position;
public SizeF size;
};
List<Circle> circles;
public Window()
{
this.Text = "Line";
this.Size = SizeFromClientSize(new Size(640, 480));
pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.BackColor = Color.White;
pictureBox.Paint += new PaintEventHandler(pictureBox_Paint);
this.Controls.Add(pictureBox);
this.Load += new EventHandler(Window_Load);
this.Resize += new EventHandler(Window_Resize);
circles = new List<Circle>();
timer = new Timer();
timer.Interval = 15;
timer.Enabled = true;
timer.Tick += new EventHandler(timer_Tick);
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Black);
foreach (Circle c in circles)
{
g.DrawEllipse(Pens.White, new RectangleF(c.position, c.size));
}
pictureBox.Image = bmp;
}
private void timer_Tick(object sender, EventArgs e)
{
Circle c;
Console.WriteLine("Works");
for (int i = 0; i < circles.Count; i++)
{
c = circles[i];
c.size.Width = (c.size.Width > 200) ? 100 : c.size.Width + 1;
circles[i] = c;
}
pictureBox.Invalidate();
}
private void Window_Load(object sender, EventArgs e)
{
bmp = new Bitmap(this.Width, this.Height);
circles.Add(new Circle());
Circle c = circles[0];
c.position = new PointF(100, 100);
c.size = new SizeF(100, 100);
circles[0] = c;
}
private void Window_Resize(object sender, EventArgs e)
{
bmp = new Bitmap(this.Width, this.Height);
pictureBox.Invalidate();
}
}
}
Same code worked fine in Xamarin.
1) in timer_Tick remove line: pictureBox.Invalidate();
2) cut code from pictureBox_Paint and paste at the end of timer_Tick
3) fix variable name collision c
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Search_Text_In_Files
{
public partial class ListViewCostumControl : UserControl
{
public static ListViewControl lvnf;
public ListViewCostumControl()
{
InitializeComponent();
lvnf = new ListViewControl();
lvnf.Location = new Point(50, 50);
lvnf.Size = new Size(50, 50);
lvnf.View = View.Details;
lvnf.Dock = DockStyle.Fill;
lvnf.SuspendLayout();
lvnf.LabelEdit = true;
lvnf.Sorting = SortOrder.None;
this.Controls.Add(lvnf);
lvnf.ResumeLayout(false);
}
public class ListViewControl : System.Windows.Forms.ListView
{
public ListViewControl()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(System.Windows.Forms.Message m)
{
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
private void ListViewNFTest_Load(object sender, EventArgs e)
{
}
}
}
Then i drag the control in form1 designer from toolbox and adding items to it:
ListViewCostumControl.lvnf.Items.Add(mypro[0].Report1);
The problem is in the constructor of the Costum listview.
If i'm adding columns to the control it will show the items when adding them.
lvnf = new ListViewControl();
lvnf.Location = new Point(50, 50);
lvnf.Size = new Size(50, 50);
lvnf.View = View.Details;
lvnf.Dock = DockStyle.Fill;
lvnf.SuspendLayout();
lvnf.LabelEdit = true;
lvnf.Columns.Add("From", 100, HorizontalAlignment.Left);
lvnf.Columns.Add("Subject", 200);
lvnf.Columns.Add("Date", 300);
But once i remove the Columns then i don't see any items only the vertical scrollbar on the right moving up like it's adding items.
My solution for what i needed:
Hiding the column.
Empty string for the text and width of the control.
lvnf.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
lvnf.Columns.Add("", 984, HorizontalAlignment.Left);
I try to put text from 0 to 11 on 12 pictureboxes have a ball image, but get the text 11 on all.
How can I get the text on the balls from 0 to 11 ?
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wfballs2
{
public partial class Form1 : Form
{
String s;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox[] Shapes = new PictureBox[12];
for (int i = 0; i < 12; i++)
{
s = Convert.ToString(i);
Shapes[i] = new PictureBox();
Shapes[i].Name = "ball" + i.ToString();
Shapes[i].Location = new Point(10 + 45 * i, 300);
Shapes[i].Size = new Size(40, 40);
Shapes[i].Image = Image.FromFile(# "C:\Users\Eiko\Desktop\ball\ball.jpg");
Shapes[i].SizeMode = PictureBoxSizeMode.CenterImage;
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
{
e2.Graphics.DrawString(s, Font, Brushes.Black, 10, 13);
});
}
}
}
}
The variable s is an instance in your form, so its value is shared by the form and all the shapes you create.
Everytime one of your shape is painted, this code:
e2.Graphics.DrawString(s, Font, Brushes.Black, 10, 13);
will run. That s is an instance variable of your Form. It will show for all shapes the last value that it was set to. In your case that happens in the last iteration of your for loop, hence the 11.
To fix this you have to capture the loop variable in a local variable before the anonymous eventhandler and then use that local var to do the Convert.ToString(val) in the event handler, like so:
var val = i; // capture variable
Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
{
// use captured variable here
e2.Graphics.DrawString(Convert.ToString(val), Font, Brushes.Black, 10, 13);
});
This will generate the string with the local variable based on val.
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wfballs2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int k = 0;
PictureBox[] Shapes = new PictureBox[12];
for (int i = 0; i < 12; i++)
{
Shapes[i] = new PictureBox();
Shapes[i].Name = "ball" + i.ToString();
Shapes[i].Location = new Point(10 + 45 * i, 300);
Shapes[i].Size = new Size(40, 40);
Shapes[i].Image = Image.FromFile(# "C:\Users\Eiko\Desktop\ball\ball.jpg");
Shapes[i].SizeMode = PictureBoxSizeMode.CenterImage;
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
{
e2.Graphics.DrawString(k.ToString(), Font, Brushes.Black, 10, 13);
k++;
});
}
}
}
}