I need to make a program that takes the input of a textbox (tb_1) and puts it in the second textbox (tb_2, multilined) when a button is pressed. It also needs to paste the code a specific amount of times, depending on the numericupdown digit. I have come this far, what do i do next? Thanks in advance.
(Note: it should probably be done within a class.)
{
public Form1()
{
InitializeComponent();
}
private void tb_1_TextChanged(object sender, EventArgs e)
{
}
private void btn_1_Click(object sender, EventArgs e)
{
string tekst = tb_1.Text;
tb_2.Text = tekst + Environment.NewLine;
tb_1.Clear();
tb_2.Text = tekst + Environment.NewLine;
}
}
}
Related
So essentially, I have a button, when I press the button, the button should delete the most recent row of text embedded into the rich text box. Press it again, it should delete the next line above that etc.
I tried a using .substring, which it worked, but required me to double click the button instead of a single click..
Furthermore, when I press the add text button it should replace the deleted line. Currently, the delete button button1_click just removes all lines, but all I want the it to do is remove a line of text from the richtextbox.
Edit:
Huge apologies before, I forgot to add the code as mentioned in replies, apologies.
public partial class Form1 : Form
{
string[] texts = new string[10];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (TextBox.Text == "")
{
texts[i] = texts[i];
}
else
{
texts[i] += TextBox.Text + "\r\n";
richText.Text += texts[i];
++i;
TextBox.Text = "";
}
}
private void richText_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//i dont know need to do research or im too dumb
richText.Text = "";
for (int i = 0; i < texts.Length; i++)
{
texts[i] = "";
}
i = 0;
//https://stackoverflow.com/questions/22587505/delete-the-last-line-of-rich-text-box
//works, but double click instead of one click
}
}
}
To clarify, the left button adds text from the textbox and the right button is basically the function aforementioned.
enter image description here
Thanks to anyone who helps!
I´m trying to fill positions on one dimesional array in C#, using three controls: one button, one textbox and one label.
public partial class Form1 : Form
{
string[] VECT;
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VECT = new string[4];
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
VECT[i] = Convert.ToString(textBox2.Text);
i++;
label5.Text = Convert.ToString(i);
}
}
When the code is compiled, I start to insert alphanumeric values on the textbox, clicking the button to fill this array, but when loading is done, the array doesn't preserve data on it's positions.
Please, I need your help to do a wrigth loading on this array.
Thank you.
Please see the screenshot below for a MaskedTextBox:
and the code below:
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.ValidatingType = typeof(System.Decimal);
maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
}
void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
if (!e.IsValidInput)
{
MessageBox.Show("Validation failed");
}
else
{
Decimal value = (Decimal)e.ReturnValue;
MessageBox.Show("Validation suceeded");
}
}
}
e.IsValidInput is always false if I enter a value less than 1000. If I enter a value above 1,000 then it works i.e. the cast to a decimal works. I believe this is because the comma is missing with values less than 1,000. How do I make the comma optional?
I've not touched C# in some time, was trying to help a new programmer friend of mine and became utterly stumped by the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
activateEnterButton();
TextBox t = (TextBox)sender;
string theText = t.Text;
MessageBox.Show("text: " +theText);
}
private void activateEnterButton()
{
bool allGood = true;
foreach (Control control in Controls )
{
if (control is TextBox)
{
string test = ((TextBox)control).Text;
if (test.Length == 0)
{
MessageBox.Show("text: " +test);
allGood = false;
break;
}
}
}
btnEnter.Enabled = allGood;
}
Our goals is utterly simple: We have 5 textboxes and each needs to have some text in them before a button is enabled. When each has text, button is enabled.
When I walk through the code while debugging everything is called okay but no matter how much text I put in the textbox the activateEnterButton never knows it's there. The two MessageBoxes show different output as well: the one in the activateEnterButton never has any, the one in the event handler always does.
Any assistance would be greatly appreciated. Thank you.
I have removed the calls to the activateEnterButton(), I have put guts of that code inside the event handler for textBox5 but the button is still not being enabled.
The answer I accepted didn't give me the functionality I wanted (entering data into textbox5 would make the button active)the following code gave me all the functionality I wanted. And lastly, the reason for my errors were because A) foreach iterates from the last control to the first, and B) the last textbox control I have on the form is a ReadOnly textbox control, its text is always "", hence I was always getting dumped out of my earlier code. At any rate, new code:
private void checkMe()
{
bool allGood = true;
foreach (Control control in Controls)
{
// Make sure the ReadOnly textbox doesn't cause false
if (control.Name.Equals("ReadOnlyTextBox"))
{
// MessageBox.Show("hidden textbox: " + ((TextBox)control).Text);
allGood = true;
}
else if (control is TextBox)
{
string test = ((TextBox)control).Text;
//MessageBox.Show("test: " + test);
if (test.Length < 1)
{
allGood = false;
// MessageBox.Show("All textboxes need input");
break;
}
else
{
allGood = true;
}
}
}
btnEnter.Enabled = allGood;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
checkMe();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
checkMe();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
checkMe();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
checkMe();
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
checkMe();
}
In your activateEnterButton() method you looping through the controls and if control is textbox; checking whether it has text or not.
Say if textbox1 has fired the textchanged event; how does that guarantee that other textbox has text in it?
You said in your post The two MessageBoxes show different output as well: .. that should be.
say textbox1 have fired textchanged event and so does in textchanged event you have the text displayed in messagebox but in method activateEnterButton() where you are looping through all controls in form there is no guarantee of order like textbox1 .. 5 (in that order loop will check them) and you are breaking out pf loop once it has no text. So does, in your method you don't see any text in messagebox.
Best way of doing it would be as below (consider that you have TextBox 1..5; have the textchanged on TextBox5 only.)
private void textBox5_TextChanged(object sender, EventArgs e)
{
bool allGood = false;
foreach (Control control in Controls )
{
if (control is TextBox)
{
string test = ((TextBox)control).Text;
if (test.Length > 0)
{
allGood = true;
}
else
{
MessageBox.Show("Fill all textbox first");
break;
}
}
}
btnEnter.Enabled = allGood;
}
Hope this helps.
When I run my program with a listbox everything works after selecting the items and pressing enter, but when I press the clear button and I select the items again and press enter nothing happens. I've tried the following for the clear button and they clear my label text and the selected listbox but I can no longer produce another output when I try pressing enter button again after selecting the items.
public partial class frmLabSix : Form
{
public string strCakes;
public int cakeCost;
public frmLabSix()
{
InitializeComponent();
}
private void lstCakes_SelectedIndexChanged(object sender, EventArgs e)
{
for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
{
strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();
if (lstCakes.SelectedIndices[index] == 0) cakeCost += 18;
if (lstCakes.SelectedIndices[index] == 1) cakeCost += 25;
if (lstCakes.SelectedIndices[index] == 2) cakeCost += 40;
if (lstCakes.SelectedIndices[index] == 3) cakeCost += 30;
}
}
private void lblOrdered_Click(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
double tax = 1.13;
lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");
lblOrdered.Visible = true;
}
private void btnClear_Click(object sender, EventArgs e)
{
lstCakes.SelectedItems.Clear();
lblOrdered.Visible = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
Can someone tell me why this is?
This line of code hides your label.
lblOrdered.Visible = false;
You make your label invisible on clear button click. Do you reset its visibility after?
I don't know, what happens in other part of your code, but it should probably be like this:
// if the label is not visible, the next line won't make it visible implicitly
lblOrdered.Text = ...
//you should set label's visibility explicitly
if (!lblOrdered.Visible)
lblOrdered.Visible = true;
Setting label's text doesn't make it visible. If you hide it with your own code, you should make label visible explicitly as well.