I have a DateTime column in my WinForms DataGridView; currently the field can only be edited by typing the date in manually like "2010/09/02". What would be needed to have a DateTimePicker (or equivalent) used as the editor instead?
DataGridViewDateTimePickerColumn doesn't exist so you have to add the control manually as below.
DateTimePicker dtp = new DateTimePicker();
Rectangle rectangle;
public Form1()
{
InitializeComponent();
dgv.Controls.Add(dtp);
dtp.Visible = false;
dtp.Format = DateTimePickerFormat.Custom;
dtp.TextChanged += new EventHandler(dtp_TextChange);
}
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (e.ColumnIndex)
{
case 5: // Column index of needed dateTimePicker cell
rectangle = dgv.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex,
true);
dtp.Size = new Size(rectangle.Width, rectangle.Height);
dtp.Location = new Point(rectangle.X, rectangle.Y);
dtp.Visible = true;
break;
}
}
private void dtp_TextChange(object sender, EventArgs e)
{
dgv.CurrentCell.Value = dtp.Text.ToString();
}
private void dgv_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dtp.Visible = false;
}
private void dgv_Scroll(object sender, ScrollEventArgs e)
{
dtp.Visible = false;
}
If you need a default date, you can handle it through the RowsAdded event.
private void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
dgv.Rows[e.RowIndex].Cells[5].Value = DateTime.Now.ToShortDateString();
}
Related
How to access the textbox.text from the other method?
private void button_Click(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.Text = "";
textBox.TextChanged += textBox_TextChanged;
}
static void textBox_TextChanged(object sender, EventArgs e)
{
textBox.Text = "0";
}
static void textBox_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.Text = "0";
}
You can cast the sender to TextBox type and use a boolean to avoid re-entry (not necessary but good smell):
private bool IsUpdatingTextBox = false;
static void textBox_TextChanged(object sender, EventArgs e)
{
IsUpdatingTextBox = true;
((TextBox)sender).Text = "0";
IsUpdatingTextBox = false;
}
Don't you want to set the text to "0" in button_Clickand textBox.Enabled to false? In this case you can change the BackColor to Window for the appearance.
I have a DataGridView with a DateTimePicker control hosted in one of the columns. Currently selecting a date assigns it to the value of the cell, but since I'm allowing the cell to be editable to utilize CellBeginEdit and CellEndEdit events, you can also type text into the column that may be invalid. How can I make it so editing the cell is enabled through the DateTimePicker only? See partial solution below:
DateTimePicker dtpVisit;
private void Form_Load(object sender, EventArgs e)
{
dtpVisit = new DateTimePicker();
dtpVisit.Format = DateTimePickerFormat.Short;
dtpVisit.Visible = false;
dtpVisit.Width = 100;
//DataGridView is called activities
activities.Controls.Add(dtpVisit);
dtpVisit.ValueChanged += this.dtpVisit_ValueChanged;
...
}
private void activities_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
if((activities.Focused) && (activities.CurrentCell.ColumnIndex == 5))
{
dtpVisit.Location = activities.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
dtpVisit.Visible = true;
dtpVisit.Focus();
if (activities.CurrentCell.Value != DBNull.Value)
{
dtpVisit.Value = (DateTime)activities.CurrentCell.Value;
}
else
{
dtpVisit.Value = DateTime.Today;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void activities_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if((activities.Focused) && (activities.CurrentCell.ColumnIndex == 5))
{
activities.CurrentCell.Value = dtpVisit.Value.Date;
dtpVisit.Visible = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dtpVisit_ValueChanged(object sender, EventArgs e)
{
activities.CurrentCell.Value = dtpVisit.Text;
}
private void activities_Scroll(object sender, ScrollEventArgs e)
{
dtpVisit.Visible = false;
}
Is it possible to get any value from the currently displayed tabcontrol that is open (displayed)? Trying to highlight the adjacent / corresponding tab / link label.
I am using link labels as navigation for the tabs. The real (ugly top) tabs will hidden when the project is finished.
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
//COLOURS TO BE APPLIED WHEN THE CORRESPONDING TAB IS OPEN
linkLabel1.BackColor = Color.Black;
linkLabel1.ForeColor = Color.White;
linkLabel1.ActiveLinkColor = System.Drawing.Color.White;
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
}
After #Idle_Mind answer I still was not sure how to bind / wireup the event. This is for anyone else with same question:
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
labels_LinkClicked(sender, e);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
labels_LinkClicked(sender, e);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
labels_LinkClicked(sender, e);
}
//METHOD TO CALL ON EACH CLICK OF LINK LABELS
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach (LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Transparent;
}
}
Wire up the LinkClinked() events to the same event handler like below:
private List<LinkLabel> labels;
private void Form2_Load(object sender, EventArgs e)
{
labels = new List<LinkLabel>() { linkLabel1, linkLabel2, linkLabel3 };
}
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach(LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Gray;
}
}
I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}
When I click on a textbox, I want the default text to disappear. Is there any other property that would work for this purpose?
The following Will set Default text in Gray color. Also When you leave the textbox as blank, again set default text to the textbox.
private void Form1_Load(object sender, EventArgs e)
{
this.textBox2.Enter += new EventHandler(textBox2_Enter);
this.textBox2.Leave += new EventHandler(textBox2_Leave);
textBox2_SetText();
}
protected void textBox2_SetText()
{
this.textBox2.Text = "Default Text...";
textBox2.ForeColor = Color.Gray;
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.ForeColor == Color.Black)
return;
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "")
textBox2_SetText();
}
Add a method to the GotFocus event of the TextBox that will change the Text property to ""
private void Form1_Load(object sender, EventArgs e) {
this.textBox1.GotFocus += new EventHandler(textBox1_Focus);
this.textBox1.Text = "some default text...";
}
protected void textBox1_Focus(Object sender, EventArgs e) {
textBox1.Text = "";
}
It sounds like you are wanting a Watermark in your Textbox.
See these articles.
http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark
Watermark in System.Windows.Forms.TextBox
and if you are using wpf something like this http://msdn.microsoft.com/en-us/library/bb613590.aspx