alex3wielki alex3wielki - 1 month ago 21
C# Question

C# textbox programming

I'm trying to program the textboxes, so they do their instructions AFTER the textbox is left. Any ideas on how to do it? I tried smth with KeyUp, but don't really know how it works

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 For_homework
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

var LoopResult = new List<int>();
var LoopParameters = new List<int>();
LoopParameters = TakeInput();
LoopResult = Looping(LoopParameters);
Result(LoopResult);
FinalResult(LoopResult);
}
private List<int> TakeInput()
{
var Loop = new List<int>();
Loop.Add(int.Parse(textBox1.Text));
Loop.Add(int.Parse(textBox2.Text));
Loop.Add(int.Parse(textBox3.Text));
Loop.Add(int.Parse(textBox4.Text));
Loop.Add(int.Parse(textBox5.Text));
Loop.Add(int.Parse(textBox6.Text));
return Loop;
}//Take the input
private List<int> Looping(List<int> LoopParam)
{
var LoopSum = new List<int>();
LoopSum.Add(0);
LoopSum.Add(0);
for (int i = LoopParam[0]; i <= LoopParam[1]; i += LoopParam[2])
{
LoopSum[0]+=i;
}
for(int i = LoopParam[3]; i <= LoopParam[4]; i += LoopParam[5])
{
LoopSum[1]+=i;
}
return LoopSum;
}//Do the loop and summarize
void Result (List<int> LoopResult)
{
resultA.Text = ""+LoopResult[0];
resultB.Text = ""+LoopResult[1];
}//Give result
void FinalResult(List<int> LoopResult)
{
if(LoopResult[0]>LoopResult[1])
{
WhoWins.Text = "Player A wins";
}
else if(LoopResult[1]>LoopResult[0])
{
WhoWins.Text = "Player B wins";
}
else
{
WhoWins.Text = "It's a draw";
}
}//Give final result
private void textBox1_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox1.Text);
if (i < 1 || i > 6)
label3.Text = "Wrong number";
}//Texbox evaluation

private void textBox2_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox2.Text);
if (i < 7 || i > 18)
label4.Text = "Wrong number";
}

private void textBox4_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox4.Text);
if (i < 1 || i > 6)
label7.Text = "Wrong number";
}//Textbox evaluation

private void textBox5_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox5.Text);
if (i < 7 || i > 18)
label8.Text = "Wrong number";
}
}
}


The form itself

It told me my post is mostly code so I need to add more details

Answer

There is LostFocus TextBox Event that might be useful for you in this case:

private void textBox1_LostFocus(object sender, System.EventArgs e)
{
    //Do something
}

And don't forget to register event in your designer file as

textBox1.LostFocus += new EventHandler(textBox1_LostFocus);