Stop Writing File to TextBox C# - 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;
}

Related

Retrieving text beginning with specific alphabet from the text file and displaying it in textbox

I am creating a console application that will take names as an input and will store it in a text file and then i want to retrieve the names beginning with A.
The console application has two textboxes and two buttons. By hitting button1 the text entered in the textbox1 will be copied in the text file, and by clicking button2 U would like to retrieve the name beginning with A and display it in textbox2.
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("d:\\demo.txt");
txt.Write(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = File.ReadAllText("d:\\demo.txt");
}
I know how to retrieve the entire data of the text file by using
textBox2.Text = File.ReadAllText("d:\\demo.txt");
Please help me to retrieve the names beginning with A.
Change Write to WriteLine and then you can read each name as a separate line. When you do, check the first letter with StartsWith. If it starts with an a, append it to textbox2 using +=:
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("d:\\demo.txt");
txt.WriteLine(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader(#"d:\\demo.txt");
while((line = file.ReadLine()) != null)
{
if (line.ToLower().StartsWith("a"))
{
textBox2.Text += " " + line;
}
}
}
You didn't provide required details but are you looking for something like that; (writing the texts as lines to a file and reading it)
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter(#"d:\\demo.txt", append: true);
txt.WriteLine(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"d:\\demo.txt").Where(x => x.StartsWith("A"));
textBox2.Text = string.Join(",", lines);
}

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

How to disable Spacebar in a TextBox C#

i am trying to use SendKeys.Send() to send textbox1.Text to a application ( like notepad ), and i want to disable or remove spaces from the textbox so there is no spaces in the application.
Any help is appreciated!
private void InfoSend_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
SendKeys.Send(input_info);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
input_info = textBox1.Text;
}
If user can input white spaces to TextBox then you can write this:
private void InfoSend_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
input_info = input_info.Replace(" ", string.Empty);
SendKeys.Send(input_info);
}

C# Checking if button was clicked

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.

How do I prepend text from one textbox to another?

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

Categories

Resources