Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to add 3 textboxes into 1 label.
And not only that but first the combobox is going to add dear mr / dear miss then when i want to type in the textboxes it should come behind that.
This is the code I have been working with:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "Man")
lblAanhef.Text = "Geachte heer " + txtVnaam.Text +" "+ txtTvoegsel.Text + " " + txtAnaam.Text;
else
lblAanhef.Text = "Geachte mevrouw";
}
private void fOpdracht1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Man");
comboBox1.Items.Add("Vrouw");
}
private void txtVnaam_TextChanged(object sender, EventArgs e)
{
}
private void txtTvoegsel_TextChanged(object sender, EventArgs e)
{
}
private void txtAnaam_TextChanged(object sender, EventArgs e)
{
}
I have been struggling with this all morning but i cant seem to figure out how to do it.
If anyone knows please let me know.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
changeText();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
changeText();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
changeText();
}
private void changeText()
{
if (comboBox1.SelectedItem.ToString() == "Man")
label1.Text = "Geachte heer " + textBox1.Text + " " + textBox2.Text;
else
label1.Text = "Geachte mevrouw " + textBox1.Text + " " + textBox2.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
comboBox1.Items.Add("Man");
comboBox1.Items.Add("Woman");
comboBox1.SelectedIndex = 0;
}
Like this?
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 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();
}
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;
}
I have a quick and simple question on a small project that I'm starting out on my own in C# for a Windows form program with Visual Studio 2010. I can't seem to find the correct code to transfer the input data that a user enters into a textbox with a method where they hit the enter key and it automatically enters a message in that label on the same form.
Such as in the following code (which has been edited as suggestions are provided):
namespace MovieFinders2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
//Named "Enter a Year"
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
label2.Text = textBox1.Text;
label2.Text = "Movies released before " + textBox1.Text;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
label2.Text = textBox1.Text;
label2.Text = "Movies released before " + textBox1.Text;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
label3.Text = textBox1.Text;
label3.Text = "Movies released in or after " + textBox1.Text;
}
}
}
private void label3_Click(object sender, EventArgs e)
{
label3.Text = textBox1.Text;
label3.Text = "Movies released in or after " + textBox1.Text;
}
}
}
I know that this program is in the early stages, but I"m trying to take this one step at a time and this is the road block that I have encoutered at this point; so any and all help would be greatly appreciated. Right now when I click the mouse on the lable it displays the message in that label. I need this to appear in the label when the user presses the enter key.
Try this:
void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
label2.Text = textBox1.Text;
label2.Text = "Movies released before " + textBox1.Text;
}
}
TextBox.KeyDown event
I want to search a file by the month of birth and display the results in label7. So what I want is to enter the number 11 into textbox5 press button4 and display all the enteries with a birthmonth of 11 into label7.text. The filename.txt is created in the first part of the program I now what to be able to search that filename.txt. Another example of what i am trying to do is. When the file was created data was entered Firstname, lastname, birthday, and birth month. I want to search that file by birth month and display the results in label7.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void close_Click(object sender, EventArgs e)
{
Close();
}
private void button1_Click(object sender, EventArgs e)
{
writetext();
reset();
}
public void writetext()
{
using (TextWriter writer = File.AppendText("filename.txt"))
{
writer.WriteLine("First name, {0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text);
MessageBox.Show(String.Format("First Name,{0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text));
}
}
public void reset()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
maskedTextBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
private void button2_Click(object sender, EventArgs e)
{
readfile();
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string[] lines = ...
try
{
int month = Int32.parse(textBox5.Text);
label7.Text = String.Format("Month of Birth {0}", lines[month]);
}
catch(Exception e){
label7.Text = "Invalid input";
}
}
public void readfile()
{
string[] lines = File.ReadAllLines("filename.txt");
label6.Text = String.Join(Environment.NewLine, lines);
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
}
}
Instead
label7.Text = (String.Format("Month of Birth{4}", textBox5.Text));
Use
label7.Text = (String.Format("Month of Birth{0}", textBox5.Text));
The {0} 0 in curly brace means the 0-positioned argument in String.Format argument list, in this case, refers to textBox5.Text
--Update--
Seems you need to print the [month]-th line of the text file to Label7, the code should be:
string[] lines = ...
try{
int month = Int32.parse(textBox5.Text);
label7.Text = String.Format("Month of Birth {0}", lines[month]);
}
catch(Exception e){
label7.Text = "Invalid input";
}
Judging by your comment on xandy's answer, it is impossible for us to help you without knowing the file format of filename.txt. However, you probably want something like this.
private void button4_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines("filename.txt");
string result = GetResultFromLines(lines, textBox5.Text);
label7.Text = (String.Format("Month of Birth{0}", result));
}
You will have to write the GetResultFromLines function yourself, based on how you want to retrieve your data from file.
The number in brackets is not the field width - it's the index of the parameter to use.