How to add text to a TextBox with Button? I know how to do it but if there are many textboxes I can't .if anyone know please help me .
Private void button1_Click(object sender, Even target e)
{
Textbox1.text=Textbox1.text+"a";
}
This is only for one textbox and for others textboxes I also want to use this button to add text to textbox.
Is this what you are looking for ?
Private void button1_Click(object sender, Even target e)
{
Textbox1.text = Textbox1.text + "a";
Textbox2.text = Textbox2.text + "a";
Textbox3.text = Textbox3.text + "a";
}
Related
I have some problem with writing file to TextBox in C#. Something what I want is when I press button, date and time will be written to textbox and automatically stop writing even though the button still be pressed. What should I do ? I can't stop writing file to TextBox.
private void button1_Click(object sender, EventArgs e)
{
countermerah1++;
if (countermerah1 == 1)
{
StatusBox.Text += "B" + "\r\n";
countermerah1 = 0;
}
}
Would this meet your requirements?
private void button1_Click(object sender, EventArgs e)
{
StatusBox.Text += "B" + "\r\n";
StatusBox.Enabled = false;
}
I need your help in making text box at run time and taking values from these text boxes that user enter. i have two button and one rich_text_box , when user click on one button it creates 3 text boxes and then user click on other button it should take value from text boxes and how in rich text box .
this is code i am using to create dynamic textbox
private void create_textbox_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Clear();
for(i=1;i<=3;i++)
{
TextBox text = new TextBox();
text.Name = "Text Box" + i.ToString();
//text.Text = "Text Box " + i.ToString();
flowLayoutPanel1.Controls.Add(text);
}
}
and this code i am using to take values from new created text boxes and displaying in rich text box .
private void get_value_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
for (i = 1; i <= 3; i++)
{
string value = text.Text + i.ToString();
richTextBox1.SelectedText = "\r\n" + value;
}
}
This should solve your problem:
private void get_value_Click(object sender, EventArgs e)
{
for (var c in flowLayoutPanel1.Controls)
{
var t = c as TextBox;
if (t != null)
{
richTextBox1.SelectedText = "\r\n" + t.Text;
}
}
}
In your method get_value_Click you aren't using any of the text boxes that were added to the flow layout panel. Something similar to Wolfgang Ziegler's answer should work but you will need to check the Type and Name in case you have other controls in the flow layout panel.
private void get_value_Click(object sender, EventArgs e)
{
for (i = 1; i <= 3; i++)
{
string value = this.flowLayoutPanel1.Controls["Text Box" + i].Text;
richTextBox1.SelectedText = "\r\n" + value;
}
}
This ought to do it.
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
I am making a program that should just continue if 2 conditions are given.
The first one, 2 TextBoxs have the same word in and a Button was clicked, which opens a new Form. Now I have the event for the "complete" button.
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == textBox3.Text && ???)
{
StreamWriter myWriter = File.CreateText(#"c:\Program Files\text.txt");
myWriter.WriteLine(textBox1.Text);
myWriter.WriteLine(textBox2.Text);
}
]
My problem is, I can't find a method that gives something like `button1.Clicked or something similar.
I hope someone can help me here..
Click is an event that fires immediately after you release the mouse button. So if you want to check in the handler for button2.Click if button1 was clicked before, all you could do is have a handler for button1.Click which sets a bool flag of your own making to true.
private bool button1WasClicked = false;
private void button1_Click(object sender, EventArgs e)
{
button1WasClicked = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == textBox3.Text && button1WasClicked)
{
StreamWriter myWriter = File.CreateText(#"c:\Program Files\text.txt");
myWriter.WriteLine(textBox1.Text);
myWriter.WriteLine(textBox2.Text);
button1WasClicked = false;
}
}
These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.
if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)
{
//Do not reload the gridview.
}
else
{
reload my gridview.
}
SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked
button1, button2 and button3 have same even handler
private void button1_Click(Object sender, EventArgs e)
{
Button btnSender = (Button)sender;
if (btnSender == button1 || btnSender == button2)
{
//some code here
}
else if (btnSender == button3)
//some code here
}
i am very new to this website. I am an undergraduate student, doing my Bachelor Of Computer Application.
I am doing a simple program in Visual Studio using C# and I came across the same problem, how to check whether a button is clicked?
I wanted to do this,
if(-button1 is clicked-) then
{
this should happen;
}
if(-button2 is clicked-) then
{
this should happen;
}
I didn't know what to do, so I tried searching for the solution in the internet. I got many solutions which didn't help me. So, I tried something on my own and did this,
int i;
private void button1_Click(object sender, EventArgs e)
{
i = 1;
label3.Text = "Principle";
label4.Text = "Rate";
label5.Text = "Time";
label6.Text = "Simple Interest";
}
private void button2_Click(object sender, EventArgs e)
{
i = 2;
label3.Text = "SI";
label4.Text = "Rate";
label5.Text = "Time";
label6.Text = "Principle";
}
private void button5_Click(object sender, EventArgs e)
{
try
{
if (i == 1)
{
si = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)) / 100;
textBox4.Text = Convert.ToString(si);
}
if (i == 2)
{
p = (Convert.ToInt32(textBox1.Text) * 100) / (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text));
textBox4.Text = Convert.ToString(p);
}
I declared a variable "i" and assigned it with different values in different buttons and checked the value of i in the if function.
It worked. Give your suggestions if any. Thank you.
Basically I'm making a simple program to help take notes at my job. I have a one line textbox1, and a multiple line textbox2.
I want to be able to type whatever in textbox1, and then press "enter" and it show up in the first line in textbox2. Any help would be appreciated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
//in form constructor, or InitializeComponent method
textBox1.Validated += DoValidateTextBox;
//in another place of your class
private void DoValidateTextBox(object sender, EvenArgs e) {
textBox2.Text = ((TextBox)sender).Text + Environment.NewLine + textBox2.Text;
}
This should work:
private void textBox1_KeyDown(object sender, KeyEventArgs e) // Keydown event in Textbox1
{
if (e.KeyCode == Keys.Enter) // Add text to TextBox2 on press Enter
{
textBox2.Text += textBox1.Text;
textBox2.Text+= "\r\n"; // Add newline
textBox1.Text = string.Empty; // Empty Textbox1
textBox1.Focus(); // Set focus on Textbox1
}
}
If you want to add text at the firstline of your textbox, then replace in the code above:
textBox2.Text = textBox1.Text + "\r\n" + textBox2.Text;
It depends on what you want the final result to be. If all you want is the first line of the second textbox to equal the first then:
void myEvent()
{
textbox2.Text = textbox1.Text;
}
If however you want whatever is in textbox1 to be appended to textbox2 every time you press a button, then you are better off using a ListView:
void myEvent()
{
myListView.Items.add(textbox1.Text);
}
If you specifically want a textbox though (with the data always appended to the first line):
void myEvent()
{
textbox2.Text = textbox1.Text + Environment.NewLine + textbox2.Text;
}