I have a Textbox with MultiLine enabled, in my application this Textbox controller used to insert some text.
All I want to do is to jump to a new line if the user clicks enter.
All I have tried is to find the write command inside my controller Enter event:
private void tbc_Enter(object sender, EventArgs e)
{
}
Is this what you want?
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "This" + Environment.NewLine + "A" + Environment.NewLine + "Multiline" + Environment.NewLine + "Textbox.";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(Environment.NewLine);
textBox1.Focus();
}
Related
Is it possible to add the same string typed in the texbox in the two lines of the listbox, as shown in the image below?
private void metroButton10_Click(object sender, EventArgs e)
{
listBox2.Items.Add("Next station: " + metroTextBox1.Text);
listBox2.Items.Add(Environment.NewLine)
metroTextBox1.Clear();
listBox2.SelectedIndex = 0;
}
Use the following code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Next Station: "+textBox1.Text);
listBox1.Items.Add("Station: " + textBox1.Text);
}
You Can't use new line. Because listbox can contain one item in a line.
Sure you can add same string typed in the textbox in the two lines of the listbox as;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("NextStation: " + textBox1.Text);
listBox1.Items.Add("Station: " +textBox1.Text);
}
I'm learning ASP.NET and I don't understand what is going on here.
The code behind file:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Some more text";
TextBox1.ForColor = System.Drawing.Color.Blue;
}
Essentially all there is just a label that tells you what the color and text of the textbox is. When you hit the button it changes the color to blue, and the page reloads.
Why is it that when you press the button the first time and the page reloads, the label does not update to the correct information? You have to press the button again for it to read that the text box is red.
Can anyone provide an explanation for this behavior? And how to change the Page_Load method to fix this issue?
The Page_Load event is being handled before the control events. See the description of the page lifecycle at http://msdn.microsoft.com/en-us/library/ms178472.aspx. To fix it, modify the code so that both Page_Load and the Button_Click handlers call the same method to set the label value. Only have Page_Load execute if the method isn't a POSTBACK.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetUpLabel();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Some more text";
TextBox1.ForeColor = System.Drawing.Color.Blue;
SetUpLabel();
}
private void SetUpLabel()
{
Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
You Need To Write The Code In Page_Load As Under:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) //This condition allows a code to execute once when your page is load first time.
{
Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Some more text";
TextBox1.ForColor = System.Drawing.Color.Blue;
Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
try it, Simply try this code, this codes alway's work, don't need extra code
bool IsClick=false;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
if(IsClick)
{
TextBox1.Text = "Some more text";
TextBox1.ForeColor = System.Drawing.Color.Blue;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
IsClick=true;
}
This is My Idea
I'm trying to make a program that, when a button is pressed, takes the words typed in the text box and adds it to a text file. This is what I have so far:
private void textBox1_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path, string());
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path, string());
}
The String keeps coming up with error code CS1525 ("invalid expression"). What am I doing wrong?
You will want to use the string from the TextBox.Text property
for example
File.WriteAllText(path, textBox1.Text);
or
File.WriteAllText(path, (sender as TextBox).Text);
And it sounds like you want to create a Button and assign a Click event and use that to save the Text from the TextBox to the file, and for that AppendAllText may be a better option.
private void button1_Click(object sender, EventArgs e)
{
File.AppendAllText(path, textBox1.Text);
}
Try this:-
using (StreamWriter sw1 = new StreamWriter("abc.txt"))
{
sw1.WriteLine(textBox1.Text);
}
or
private void textBox1_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path,textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path,textBox2.Text);
}
I have 3 checkboxes with corresponding message in a textbox. My teacher wants the message to remain in the textbox when the checkbox is still checked and hide the text when it is unchecked. In my case when I checked the 3 checkboxes their 3 corresponding messages will appear but when I unchecked one of the checkboxes and the other two are still checked, all the message will disappear. My problem is when I unchecked one of the checkbox and and the other 2 are still checked the corresponding messages with the remaining two checked checkboxes will remain in their textboxes.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
if (chkCarWheels.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
if (chkCarAcceleration.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
if (chkCarBreakpad.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
Looks like you need to create message depending on checkboxes states. You can create method, which will do the job and call it when state of some checkbox changed.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
Or the better one - create one event handler for all checkboxes:
// use for chkCarWheels, chkCarAcceleration, chkCarBreakpad
private void chkCar_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void BuildMessage()
{
lblMessage.Text = "My " + txtName.Text + " Car";
if (chkCarWheels.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
if (chkCarAcceleration.Checked)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
if (chkCarBreakpad.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
}
You don't need to compare boolean values with true/false. Use those values directly if (chkCarWheels.Checked). And keep in mind that in C# we use CamelCase names form methods. Also consider to use StringBuilder to build whole message and then assign it to label:
private void BuildMessage()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("My {0} Car", txtName.Text);
if (chkCarWheels.Checked)
sb.Append(mycar.hasWheels(4));
if (chkCarAcceleration.Checked)
sb.Append(mycar.Accelerate());
if (chkCarBreakpad.Checked)
sb.Append((mycar.hasBreak());
lblMessage.Text = sb.ToString();
}
Try this:
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
chkCar()
}
private void chkCar()
{
string msg="";
if (chkCarWheels.Checked)
msg=msg+mycar.hasWheels(4);
if(chkCarAcceleration.Checked)
msg=msg+mycar.Accelerate();
if(chkCarBreakpad.Checked)
msg=msg+mycar.hasBreak();
lblMessage.Text=msg;
}
How can I used if (IsPostBack){} to display user's names from two text boxes into another text box?
I have 3 text boxes and a button. The text boxes are named txtLastName, txtGivenName, txtOutput. My button is btnSubmit.
How can I display text from the txtLastName and txtGivenName in the txtOutput text box?
How can I display it as: First (space) Lastname or Last, Firstname in this code.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
}
}
Create an event handler for the Click event on the button and then in the code behind, do like this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}
How can I display text from the txtLastName and txtGivenName in the
txtOutput text box?
Go to the design of your page.
Click the button.
Click F4 or Right click and select properties. This will show you a window for button.
Click the event.
Double click the "Click" action.
This will navigate you to the code behind.
Write the code in this handler
This is how the design will look like for your event handler
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}
How can I display it as: First (space) Lastname or Last, Firstname in
this code.
Write down the code in the handler as below.
txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;
Why don't use use the submit button method to achieve this?
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;
}
try this
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
txtOutput.Text = txtLastName.Text + " " + txtLastName.Text;
}