I am trying to make my text inside the box disappear when a new value is typed inside the box. I figured out how to make it disappear but my reset to default value no longer works. Which is the problem
I tried multiple += signs and if statements ideas but I am not sure how to make this work together.
private void BtnResetDefault_Click(object sender, EventArgs e)
{
txtElapsedTime.Text = "2";
txtCurrentAmount.Text = "25";
txtInitalAmount.Text = "100";
}
private void TxtInitalAmount_TextChanged(object sender, EventArgs e)
{
if (txtInitalAmount.Text.Contains("100") && txtElapsedTime.Text.Contains("2") && txtCurrentAmount.Text.Contains("25"))
{
txtElapsedTime.Text = "100";
txtCurrentAmount.Text = "2";
txtInitalAmount.Text = "25";
}
else if (txtCurrentAmount.Text != null)
{
txtInitalAmount.Text = "";
txtElapsedTime.Text = "";
txtCurrentAmount.Text = "";
}
}
I want to have when the user types in a value in the text box all 3 text boxes disappear and when the user clicks the reset to default button the default text comes back inside the text boxes. Instead when I hit reset to default it stays the same.
You can try to unsubscribe from event "txtInitalAmount.TextChanged", and then resubscribe to it.
private void BtnResetDefault_Click(object sender, EventArgs e)
{
txtElapsedTime.Text = "2";
txtCurrentAmount.Text = "25";
txtInitalAmount.TextChanged -= txtInitalAmount_TextChanged;
txtInitalAmount.Text = "100";
txtInitalAmount.TextChanged += txtInitalAmount_TextChanged;
}
private void txtInitalAmount_TextChanged(object sender, EventArgs e)
{
txtInitalAmount.TextChanged -= txtInitalAmount_TextChanged;
txtElapsedTime.Text = "";
txtCurrentAmount.Text = "";
txtInitalAmount.Text = "";
}
How can I make this even always work whether I check a radio button first or if I check combobox item first? Currently it only works if radio button is checked first. Thank you.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ( comboBox1.Text == "Test1" && radioButton1.Checked)
{
StreamReader sr = new StreamReader(#"my path");
string str = sr.ReadToEnd();
textBox1.Text = str;
}
}
I am guessing this event is only associated with the combobox event. You need to move this code into a common function and call it from both the ComboBox SelectedIndexChanged event and the radiobuttons changed event.
If ou want the test to be true whether the combobox text is "Test1" OR the readiobutton is checked, you need to make the test an OR instead of an AND, like so:
if ( comboBox1.Text == "Test1" || radioButton1.Checked)
If both conditions need to be true, try this (pseudocode):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ConditionFulfilled)
{
readThatPuppy();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (ConditionFulfilled)
{
readThatPuppy();
}
}
private bool ConditionFulfilled()
{
return (comboBox1.Text.Equals("Test1") && radioButton1.Checked;
}
private void readThatPuppy()
{
StreamReader sr = new StreamReader(#"my path");
string str = sr.ReadToEnd();
textBox1.Text = str;
}
The reason this only works when the radio button is checked is because you have this condition in your event handler.
Try the following and see if it does what you're looking for.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ( comboBox1.Text == "Test1" || radioButton1.Checked)
{
StreamReader sr = new StreamReader(#"my path");
string str = sr.ReadToEnd();
textBox1.Text = str;
}
}
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 am trying to empty the textbox when selected. I have multiple textboxes. How do i select the specific textbox dynamically? Below is my code:
private void newToDoTextBox_GotFocus(object sender, RoutedEventArgs e)
{
// Clear the text box when it gets focus.
newToDoTextBox.Text = String.Empty;
newToDoTextBox2.Text = String.Empty;
}
try this method please:
// use this method to your 2 textbox
private void yourTextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = string.Empty;
}
I have a serie of textboxes and labels form textbox 1-9 and label 1 to 9. With a click on a any label I clear the correspondant textbox.
I created a methode but it's like a baby toy comparison to my procedures in TP or VB. There must be a shortest well structered way. Any idea wiil be very much appreciated?
What I did :)))
private void label1_Click(object sender, EventArgs e)
{
textBox1.Text = "" ;
}
private void label2_Click(object sender, EventArgs e)
{
textBox2.Text = "" ;
}
private void label3_Click(object sender, EventArgs e)
{
textBox3.Text = "" ;
}
private void label4_Click(object sender, EventArgs e)
{
textBox4.Text = "" ;
}
private void label5_Click(object sender, EventArgs e)
{
textBox5.Text = "" ;
}
private void label6_Click(object sender, EventArgs e)
{
textBox6.Text = "" ;
}
private void label7_Click(object sender, EventArgs e)
{
textBox7.Text = "" ;
}
private void label8_Click(object sender, EventArgs e)
{
textBox8.Text = "" ;
}
private void label9_Click(object sender, EventArgs e)
{
textBox9.Text = "" ;
}
You can utilize Tag property to mark controls. Then you can iterate through them (preferably starting from most parent control - form and with the use of recursion! or, if you are sure, from the container, which holds the group of controls).
// assign tag "1" to "9" to labels and texboxes
// subscribe all labels to same event label_Click
private void label_Click(object sender, EventArgs e)
{
string id = (sender as Control).Tag.ToString();
// iterate or recurse
FindTextboxWithId(id).Clear();
}
// it shouldn't be hard to write FindTextboxWithId
Other possibility is to create private arrays of controls, in the form constructor, just to ease referencing them.
public TextBox[] _textBox;
public Form1()
{
InitializeComponent();
_textBox = new TextBox[] {textBox1, texBox2, ..., textBox9};
}
// assign tag "0" to "8" to labels and texboxes
// subscribe all labels to same event label_Click
private void label_Click(object sender, EventArgs e)
{
int index = int.Parse((sender as Label).Tag);
_textBox[index].Clear();
}
Third possibility is to utilize containers, to example, TableLayoutPanel. You can create 2 column container where first column is Label's and second is TextBox'es. Then just fill 9 rows and have fun in OnClick (to find sender position, to find texbox position, to find textbox and to finally clear it).
Perhaps one handler for all and using Controls.Find:
private void label_Click(object sender, EventArgs e)
{
var label = (Label)sender;
string lastDigits = new string(label.Name.SkipWhile(c => !Char.IsDigit(c)).ToArray());
var textBox = Controls.Find("textBox" + lastDigits, true).FirstOrDefault() as TextBox;
if(textBox != null)
textBox.Text = "" ;
}
Although relying on those meaningless variable names is not best practise.
To make your code less redundant, you can loop over the controls in your application:
Control Class, so when clicking on a label you will have to search for the textBox's Tag
that you will set for each textBox.
foreach (Control C in this.Controls)
{
//Code Here...
}
Quick solution:
Rename your labels like: label_1, label_2, ... label_22, then you can use the following common event-handler for all clicks.
An improvement on this would be to just pass labelNr to a separate number, which would then use that to find the textbox by name, instead of using a swith to check all of them. I don't have time to try that now, but I'm sure you can figure it out somehow.. ;)
private void label1_Click(object sender, EventArgs e)
{
var labelNr = ((Label) sender).Name.Split('_').Last();
switch (labelNr)
{
case "1":
textBox_1.Clear();
break;
case "22":
textBox_22.Clear();
break;
}
}
Update: Seems Tim Schmelter had the answer here. To steal a small detail from him: Use Controls.Find("textBox" + labelNr, true) as he shows above instead of the switch here, and you should be set.
And a javascript solution:
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Label ID="lbl1" runat="server" AssociatedControlID="txt1" onclick="clearTextBox(this)">Clear</asp:Label>
function clearTextBox(sender){
var assocControlId = sender.htmlFor;
var el = document.getElementById(assocControlId);
if (el)
el.value = '';
}
I would suggest you create a UserControl
Arrange a Lable and a TextBox
handle the label_click event
and uses that UserControl on your form instead.
something like this:
public class LableAndTextBox : UserControl
{
public LableAndTextBox()
{
InitializeComponents();
}
public void label_Click(object sender, EventArgs e)
{
textBox.Text = string.Empty;
}
}
Edit - make sure you create the userControl, in a seperate assembly - for compile reasons..
With two solutions of #sinatr I've created one other method because both are given an error message.
private void label_Click (object sender , EventArgs e)
{
string id = (sender as Control).Tag.ToString();
int newidx = Convert.ToInt32(id);
_textBox[newidx].Clear();
}
THIS WORKS!
Sure! I've added juste here this
namespace WindowsFormsApplication1
{
public partial class
DefBiscuit : Form
{
public TextBox[] _textBox;
And
In form_load this
_textBox = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 };
If you don't like to write code much, i have a program can write it fast.
For example, if you input "lable1.Text = textbox1.Text;" and "15" the program will output into a textbox:
lable1.Text = textbox1.Text;
lable2.Text = textbox2.Text;
lable3.Text = textbox3.Text;
lable4.Text = textbox4.Text;
lable5.Text = textbox5.Text;
lable6.Text = textbox6.Text;
...
lable15.Text = textbox15.Text;
Go here to know more and download: Download Counter Replacer