I`m not able to find answer.
I have several text boxes and if user enters/leaves them I check if he has changed something and the code below works.
private void txtRegNazov_Enter(object sender, EventArgs e)
{
if (txtRegNazov.Text == "n/a")
{txtRegNazov.Text = "";}
}
private void txtRegNazov_Leave(object sender, EventArgs e)
{
if (txtRegNazov.Text == "")
{txtRegNazov.Text = "n/a";}
}
I would like to create a function like
public void ClearFieldDataByEnter()
{
thisHlep.text = "";
}
public void FieldDataByleave()
{
thisHelp.text = "n/a";
}
And then in every field event would be something like:
private void txtRegNazov_Enter(object sender, EventArgs e)
{
thisHelp.Name = name of this txtBox;
ClearFieldDataByEnter();
}
This is only an easy example of what I want
... I am looking for principe ... and I`m still new to C#.
Thank you
Rember that the "sender", in this case, is the actual TextBox.
TextBox txtSender = (TextBox)sender;
You can use the sender parameter, like this:
private void txtRegNazov_Enter(object sender, EventArgs e)
{
ClearFieldDataByEnter(sender);
}
private void txtRegNazov_Leave(object sender, EventArgs e)
{
FieldDataByleave(sender);
}
public void ClearFieldDataByEnter(object text)
{
textBox = text as TextBox;
if (textbox == null)
return;
if (textbox.Text == "n/a")
{
textbox.Text = String.Empty;
}
}
public void FieldDataByleave(object text)
{
textBox = text as TextBox;
if (textbox == null)
return;
if (String.IsNullOrEmpty(textbox.Text))
{
textbox.Text = "n/a";
}
}
Related
Is there way of modifying the TextChanged event for a TextBox to ignore the case that has been entered? Currently in my ListView (containing town names) with a TextBox I want to search for any town name I want without having to use capital letters.
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
this.ListTowns.ItemsSource = this.listItemTowns;
}
this.ListTowns.ItemsSource = this.listTowns.Where((item) => { return item.TownTitle.Contains(txtSearch.Text); });
}
This should work:
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
this.ListTowns.ItemsSource = this.listItemTowns;
}
else
{
this.ListTowns.ItemsSource = this.listTowns.Where(item => item.TownTitle.Contains(txtSearch.Text, StringComparison.OrdinalIgnoreCase));
}
}
The following should do the trick:
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
this.ListTowns.ItemsSource = this.listItemTowns;
}
this.ListTowns.ItemsSource = this.listTowns.Where((item) => { return item.TownTitle.ToLower().Contains(txtSearch.Text.ToLower()); });
}
You can case cast your strings either .ToLower() or .ToUpper().
Take care
This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}
I wanna ask about winforms.
Is it possible to pass object like TextBox to a function that we can call it "Checker Validation" ?
because I'm too lazy check 1 by 1 on every textbox so I'm curious about how to check it with a function
public void checker(object)
{
if(Object.Text == ""){ Object.BackColor = Color.Red;}
else{Object.BackColor = Color.White;}
}
public void textbox_TextChanged(object sender,EventArgs e)
{
checker(object);
}
i have no idea to input the object textbox on a function #.#
You can do
public void checker(TextBox txtBox)
{
if(textbox == null)
{
return;
}
if(txtBox.Text == "")
{
txtBox.BackColor = Color.Red;
}
else
{
txtBox.BackColor = Color.White;
}
}
public void textbox_TextChanged(object sender,EventArgs e)
{
checker(sender as TextBox);
}
Yes.. basically just make the param to your function a textbox...
private void Checktext(textbox mytext)
{
if (mytext.Text=="whatever")
{ }
else
{ }
}
Thats psudo code off top of my head, but it will get you there.
I have a DataGridView with text in there (i.e. DataGridViewTextBoxColumn), and every time text changes in one of these fields, some update method has to be called somewhere else. However, I noticed that when you are updating a TextBox, the Value in the Cell is not updated yet.
class MyForm : Form
{
private System.Windows.Forms.DataGridView m_DataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn m_textBoxColumn;
private void m_DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs editEvent)
{
if (editEvent.Control as TextBox != null)
{
TextBox textBox = editEvent.Control as TextBox;
textBox.TextChanged -= new EventHandler(textBox_TextChanged);
textBox.TextChanged += new EventHandler(textBox_TextChanged);
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdateText();
}
private void UpdateText()
{
foreach (DataGridViewRow row in m_DataGridView.Rows)
{
if (row.Cells[1].Value != null)
{
string text = row.Cells[1].Value.ToString();
System.Diagnostics.Debug.WriteLine(text);
}
}
}
}
So to give an example: if the text in a TextBox is currently "F", and you type "oo", I would expect the console to output:
"F"
"Fo"
"Foo"
Instead, what it actually writes is:
"F"
"F"
"F"
Is there a way to access the contents of all TextBoxes from within the in UpdateText() method while the TextBoxes are being edited?
The DataGridViewCell.Value won't be updated right while you type in the Editing control. It's by design. The Value is updated after it's Validated when the CurrentCell is not in edit mode. I think you want something like this:
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdateText(sender as Control);
}
private void UpdateText(Control editingControl)
{
System.Diagnostics.Debug.WriteLine(editingControl.Text);
}
UPDATE
I think you can try some thing like this:
string editingText;
int editingRowIndex = -1;
private void textBox_TextChanged(object sender, EventArgs e)
{
editingRowIndex = ((DataGridViewTextBoxEditingControl)sender).EditingControlRowIndex;
editingText = (sender as Control).Text;
UpdateText();
}
private void UpdateText()
{
foreach (DataGridViewRow row in m_DataGridView.Rows)
{
if (row.Cells[1].Value != null)
{
string text = row.Index == editingRowIndex ?
editingText : row.Cells[1].Value.ToString();
System.Diagnostics.Debug.WriteLine(text);
}
}
}
I'm sync my data from Grid to DataBase using really weird way :
for example :
#region Line methods
private void LinesView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
#endregion
but when I switched some columns to ComboBox I need to make some trick before Update alike that :
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
//deltaTableAdapter.Update(fRIIBDataSet.Delta); TODO
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (lambdacat != null)
{
string selected = (LimView.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell).FormattedValue.ToString();
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
}
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
troubles comes after uncommenting Update method.
When I opening the window with a table I've got error message : Damaged the internal index DataTable: "5. " How can I fix / avoid this error ?
Does modifying it to this work ?
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
fRIIBDataSet.Delta.BeginInit(); //
deltaTableAdapter.Update(fRIIBDataSet.Delta);
fRIIBDataSet.Delta.EndInit();
}