How do I prepend text from one textbox to another? - c#

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;
}

Related

Clicking button to send info in text box not working

I try to make this: static private long number = (long)Math.Floor(GlobalRandom.NextDouble * 9_000_000_000L) + 1_000_000_000L; to be returned with this:
public string MyValtwo
{
get { return myValtwo; }
set { myValtwo = value; }
}
and finally to be sent to this:
private void button1_Click(object sender, EventArgs e)
{
MyValtwo = textBox2.Text;
}
I tried converting: public string MyValtwo = Convert.ToString(number); but nothing returns or the long (number) can't be converted into string.
The main idea is to be generated a number and automatically putted in form 1 text box
You dont need MyValtwo. Change code to below and it will work:
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = number.ToString();
}
In case you want to store the value of number as text in MyValtwo then you can add below line in button1_Click function.
MyValtwo = number.ToString();

Adding textbox value with button in C#

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";
}

Stop Writing File to TextBox C#

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;
}

How to handle a group of textbox/label in an array

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

how to use checkboxes to change values for TextBox in C#?

I have a form with 2 checkboxes and 3 buttons. When Button3 is clicked the program checks if the checkbox1 is selected, if it is selected the value for textbox 1 changes to "Hello". If the checkbox2 is selected the value changes to "please help".
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "a";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "b";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
If (checkBox1.Checked = true ) ;
{
textBox1.Text += ("hello ");
}
If(checkBox2.Checked = true);
{
textBox1.Text += ("hello ");
}
txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18);
txtRun.Size = new System.Drawing.Size(200, 25);
// Add the textbox control to the form's control collection
this.Controls.Add(txtRun);
}
private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
{
}
}
}
Remove ; in all if statement lines and do as below
if (checkBox1.Checked)
{
textBox1.Text = "hello ";
}
if(checkBox2.Checked)
{
textBox1.Text = "please help";
}
if you do something like yourTextBox.Text +="something" that will append something to current textbox text.
if you need to replace or entirely change the textbox text you can do as yourTextBox.Text ="something" ( without +)
And you have dynamic control but can't find the declaration of it
txtRun = new TextBox();
change that to
TextBox txtRun = new TextBox();
If statement in button3_Click handler is wrong
It must be like
if (checkBox1.Checked == true ) //or if (checkBox1.Checked)
{
textBox1.Text += ("Hello ");
}
if(checkBox2.Checked == true) //or if (checkBox2.Checked)
{
textBox1.Text += ("please help ");
}
remove the semicolons at the end of the "if".
Hope this helps.
I don't know why it's the question, but you have one problem in the if sentence, and note, also you have problem in this case the program run only ";" sentence if the result of if is true... remove the ;
If (checkBox1.Checked == true )
{
textBox1.Text += ("hello ");
}
If(checkBox2.Checked == true)
{
textBox1.Text += ("hello ");
}
The following code will accomplish this
When Button3 is clicked the program checks if the checkbox1 is selected, if it is selected the value for textbox 1 changes to "Hello". If the checkbox2 is selected the value changes to "please help".
private void button3_Click(object sender, EventArgs e)
{
if(checkBox1.Checked)textBox1.Text = ("Hello");
if(checkBox2.Checked)textBox1.Text = ("Please Help");
}
For some reason I have a feeling your question is incomplete and I was only able to answer exactly what you asked, if there is anything else you are trying to accomplish please provide me with more details and I will be glad to extend my answer.
Also as a side note when you dynamically create that textbox in your original code(which might I add is not addressed at all in your question) it will continually create an infinite number of TextBoxes underneath each other since every time you hit the button it creates another in the same spot.

Categories

Resources