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.
Related
i am making a windows form application in which i used a datagridview.
i want that when i write something in textbox in datagridview,than a messagebox appears containing the string i wrote..
ican't get my text in textchanged event..
all thing must be fired in textchanged event..
here is my code:-
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
//listBox1.Visible = true;
//string firstChar = "";
//this.listBox1.Items.Clear();
//if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
string str = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
if (str != "")
{
MessageBox.Show(str);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
var enteredText = (sender as TextBox).Text
...
}
Showing MessageBox in TextChanged will be very annoying.
Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.
Sample code:
dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
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 !");
}
i have 3 radio buttons:
rbtPercentualmedioanual
rbtPercentualmensal
rbtValorfixo
I would like to change options events for textbox1 according choosed option
If chose rbtValorfixo,
It will uncomment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
//substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
//formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
formatarporcentagem_textBox(sender as TextBox, e);
}
and will comment
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
If choose option rbtPercentualmedioanual or rbtPercentualmensal, it will comment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
and will uncomment: formatarporcentagem_textBox
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
i dont know how comment/uncomment keypress, textchanged or focusleave event using radiobutton check, only say how, not need make all, i can do it, but i have to know if its possible and if is, how ?
Thanks
You can determine if a radiobutton or checkbox is checked by the radioButton.Checked property.
In you case it will be something like this:
private void textbox1_TextChanged(object sender, EventArgs e)
{
if(!rbtValorfixo.Checked)
substituipontovirgula_textBox(sender as TextBox, e);
}
or:
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(rbtValorfixo.Checked)
numeropontoouvirgula_textBox(sender as TextBox, e);
else
formatarporcentagem_textBox(sender as TextBox, e);
}
and so on.
You don't need to comment or uncommment code. Being within a programming language, you need to look out for right structures.
In this rather simple case, you just need an "if" block.
This might help
You may want to use something like this? Not sure as it's quite unclear what you are trying to say.
private void Operation_Changed(object sender, EventArgs e)
{
RadioButton rbt = sender as RadioButton;
if (btn != null)
{
case "rbtPercentualmedioanual":
_operation = new example2();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtPercentualmensal":
_operation = new example3();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtValorfixo":
_operation = new example1();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
default:
break;
And change it to what you want to show
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
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();
}