I would like make a kind of signaling the red lines make it RED to white every a fiew moment with timer.until now i have made only the color RED without flash this is the code :
private void dataGridViewX1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (DateTime.Parse(dataGridViewX1.Rows[e.RowIndex].Cells[8].Value.ToString()).AddMonths(-1).Month == DateTime.Now.Month)
{
e.CellStyle.BackColor = System.Drawing.Color.Red;}}
until now I have only the color red which appears
So I have tried write some code to get glow ( flashing my color Red with white) in event of Timer control timer_tick but its false i have only the same result
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0;
while (i < dataGridViewX1.Rows.Count - 1)
{
if(
dataGridViewX1.Rows[i].DefaultCellStyle.BackColor == Color.Red)
{
dataGridViewX1.Rows[i].DefaultCellStyle.BackColor = Color.White;
}
i++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (dataGridViewX1.RowsDefaultCellStyle.BackColor == Color.Red)
{
dataGridViewX1.RowsDefaultCellStyle.BackColor = Color.White;
return;
}
if (dataGridViewX1.RowsDefaultCellStyle.BackColor == Color.White)
{
dataGridViewX1.RowsDefaultCellStyle.BackColor = Color.Red;
return;
}
}
Related
I am a novice in C# WindowForm projects, and I am facing some issues on how to toggle 'one button' into changing between 2 backcolors in C# WindowForm.
In order to toggle 'one button', I am required to use IF function in this assignment. However, my problem is that I do not know how to transition from color1 to 2 by pressing the button when running the code, I somewhat know the syntax structure of IF and else.
Could anyone help please?
namespace draft
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
bool btnclick = true;
if (btnclick == true)
{
BackColor = Color.Purple;
return;
}
if (btnclick == false)
{
BackColor = Color.Green;
return;
}
}
}
}
As you are declaring btnclick on click event and setting it to true, it will be true every time and only if block will be executed. Check current background color and change it.
private void btn1_Click(object sender, EventArgs e)
{
if (BackColor == Color.Purple)
{
BackColor = Color.Green;
return;
}
else
{
BackColor = Color.Purple;
return;
}
}
Here's the simplest code:
private void btn1_Click(object sender, EventArgs e)
{
BackColor = BackColor == Color.Purple ? Color.Green : Color.Purple;
}
I have a DataGridView with a Column called accepted, its value can be True or False as it has been modified.
I want to change the row color to green if is True or red otherwise. In the database the data type is Bit
This is what I have.
But when you start the application, the color does not change
private void dataReporte_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataReporte.Columns[e.ColumnIndex].Name == "accepted")
{
if (Convert.ToBoolean(e.Value) == true)
{
dataReporte.CurrentRow.DefaultCellStyle.BackColor = Color.GreenYellow;
}
else
{
dataReporte.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
}
}
}
What you want is to change the current style, not the default one, try this
private void dataReporte_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataReporte.Columns[e.ColumnIndex].Name == "accepted")
{
if (Convert.ToBoolean(e.Value) == true)
{
dataReporte.CurrentRow.CellStyle.BackColor = Color.GreenYellow;
}
else
{
dataReporte.CurrentRow.CellStyle.BackColor = Color.Red;
}
I tried to make drag and drop application . I drawn rectangle in run time and I want to detect if user try to move this rectangle or not
this is my code
private bool Mouse_Down = false;
Rectangle re = new Rectangle(100, 100, 60, 60);
private void DrawRegion_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.RoyalBlue), re);
}
private void DrawRegion_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse_Down == true)
{
re.Location = e.Location;
if (re.Right > DrawRegion.Width)
{
re.X = DrawRegion.Width - re.Width;
}
if (re.Top < 0)
{
re.Y = 0;
}
if (re.Left < 0)
{
re.X = 0;
}
if (re.Bottom > DrawRegion.Height)
{
re.Y = DrawRegion.Height - re.Height;
}
Refresh();
}
}
private void DrawRegion_MouseUp(object sender, MouseEventArgs e)
{
Mouse_Down = false;
}
private void DrawRegion_MouseDown(object sender, MouseEventArgs e)
{
Mouse_Down = true;
}
For more details now this rectangle move either user click on this rectangle or in any empty space so I want to detect if clicked location color pixel is rectangle color pixel or not before moving rectangle how to do that ?
Note:DrawRegion is a picturebox
Sorry for bad English
You can use Rect.Contains() to detect if your Rectaingle contain your current location
private void DrawRegion_MouseClick(object sender,MouseEventArgs e)
{
if (re.Contains(e.Location))
Mouse_Down = true;
else
Mouse_Down = false;
}
check this https://msdn.microsoft.com/en-us/library/ms557979(v=vs.110).aspx
private void arrButton_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (turn == 0)
{
button.ForeColor = Color.Green; // Can't change color
button.Text = "X";
button.Enabled = false;
turn = 1;
}
else
{
button.Text = "O";
button.Enabled = false;
turn = 0;
}
}
I used button.ForeColor = new Color.Green but when I test X still can't change green color.
How to change color text in button ?
Disabled component doesn't effect any graphical changes. It must be enabled to reflect the Color change.
You should use any other condition to check disabled button if you want to keep the graphical changes.
For Example:
if(button.ForeColor == Color.Green)
//handle the click event
for wpf:
private void arrButton_Click(object sender, RoutedEventArgs e)
{
button.Foreground= Brushes.Blue;
}
for Winform:
private void arrButton_Click(object sender, EventArgs e)
{
button.BackColor = Color.Red;
}
I have been using code that others online have supplied but for some reason it won't let me drag items from the datagridview to the textbox. I highlight a row in the dataGridView and try to drag it to the textbox but nothing happens. I have also enabled the drop property for the textBox but still no difference. Here's the code that I am using:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
}
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
Here is a small sample that i have done to give you an idea on how to do this... works perfectly for me. I used WinForms here. If WPF, there may be some more events you will need to register to in order for the drag+drop to register...
Note that you will want to add more code here and there to perform what you really want to do when you drag an item from one control to the other.
public partial class Form1 : Form
{
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private DataGridViewRow draggedrow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<StringValue> Items = new List<StringValue>() { new StringValue("1"), new StringValue("2"), new StringValue("3"), new StringValue("4"), new StringValue("5"), new StringValue("6") };
this.dataGridView1.DataSource = Items;
}
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
dataGridView1.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
e.Y - (dragSize.Height / 2)),
dragSize);
this.draggedrow = this.dataGridView1.CurrentRow;
}
else
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
this.textBox1.Text = (string)this.draggedrow.Cells["Value"].Value;
}
}
public class StringValue
{
public StringValue(string s)
{
_value = s;
}
public string Value { get { return _value; } set { _value = value; } }
string _value;
}
can't you use DataGridViewCellMouseEventArgs e instead of hittest for getting row index in dataGridView1_CellMouseDown. below is your code modified hope this helps
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.RowIndex >= 0)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
}
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}