C
C#2y ago
Bocens

✅ TextBox problem.

Hello, I'm new to programming, I'm trying WinForms now. So I have a TextBox with 10 rows and 3 columns for a total of 30 TextBoxes. I don't know how to solve the problem that I don't always fill in all the boxes, and when I don't fill in any one box, it immediately crashes
19 Replies
Omnissiah
Omnissiah2y ago
so your code...
Pobiega
Pobiega2y ago
Very unclear exactly what you are asking for help with here. You have 30 textboxes, and I'm guessing your code doesnt handle empty strings from them at all, causing the crash.
Bocens
BocensOP2y ago
yes, does not handle empty strings
SG97
SG972y ago
why 30 textboxes?
Pobiega
Pobiega2y ago
So you'll need to make your code handle empty strings then.
Bocens
BocensOP2y ago
When I'm at home I'll try to post a code of how I'm doing As an example, I want to make such a program, there is no problem with empty cells in excel, but in winform, empty fields are not allowed
Bocens
BocensOP2y ago
Pobiega
Pobiega2y ago
seems to me you are probably trying to parse numbers from empty strings which will indeed crash Also, as snowgirl probably wanted to point out, this type of program is probably not best built with 30 textboxes, rather a DataGridView?
Bocens
BocensOP2y ago
I have a code 'int x1 = Convert.ToInt32(this.TextBox1.Text); int y1 = Convert.ToInt32(this.TextBox2.Text); int k1 = Convert.ToInt32(this.TextBox3.Text); int x2 = Convert.ToInt32(this.TextBox4.Text); int y2 = Convert.ToInt32(this.TextBox5.Text); int k2 = Convert.ToInt32(this.TextBox6.Text); int sum = (x1y1)k1; suma.Text = sum.ToString();' I want empty fields x2, y2, k2, but the program does not allow me to ignore them
Pobiega
Pobiega2y ago
yeah Convert.ToInt32(this.TextBox4.Text) will crash if TextBox4.Text is empty I would recommend using int.TryParse instead, and checking the return value
Bocens
BocensOP2y ago
Ok, I will try this option in the evening How to upload the code here neatly?
Pobiega
Pobiega2y ago
$code or $paste
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Bocens
BocensOP2y ago
I created the code:
int x1, y1, k1,
x2, y2, k2 = 0;

int i = 0;


x1 = int.TryParse(this.x1.Text, out i) ? Convert.ToInt32(this.x1.Text) : 0;
y1 = int.TryParse(this.y1.Text, out i) ? Convert.ToInt32(this.y1.Text) : 0;
k1 = int.TryParse(this.k1.Text, out i) ? Convert.ToInt32(this.k1.Text) : 0;

x2 = int.TryParse(this.x2.Text, out i) ? Convert.ToInt32(this.x2.Text) : 0;
y2 = int.TryParse(this.y2.Text, out i) ? Convert.ToInt32(this.y2.Text) : 0;
k2 = int.TryParse(this.k2.Text, out i) ? Convert.ToInt32(this.k2.Text) : 0;

Console.WriteLine("x1: " + x1);
Console.WriteLine("y1: " + y1);
Console.WriteLine("k1: " + k1);
Console.WriteLine("x2: " + x2);
Console.WriteLine("y2: " + y2);
Console.WriteLine("k2: " + k2);




decimal trezultatas = (x1 * y1) * k1;
decimal trezultatas2 = (x2 * y2) * k2;

decimal rezultatas = trezultatas / 1000000;
decimal rezultatas2 = trezultatas2 / 1000000;

decimal brezultatas = rezultatas + rezultatas2;

Console.WriteLine("trez: " + trezultatas);
Console.WriteLine("rez :" + brezultatas);

label8.Text = brezultatas.ToString("0.00 m\u00b2");
int x1, y1, k1,
x2, y2, k2 = 0;

int i = 0;


x1 = int.TryParse(this.x1.Text, out i) ? Convert.ToInt32(this.x1.Text) : 0;
y1 = int.TryParse(this.y1.Text, out i) ? Convert.ToInt32(this.y1.Text) : 0;
k1 = int.TryParse(this.k1.Text, out i) ? Convert.ToInt32(this.k1.Text) : 0;

x2 = int.TryParse(this.x2.Text, out i) ? Convert.ToInt32(this.x2.Text) : 0;
y2 = int.TryParse(this.y2.Text, out i) ? Convert.ToInt32(this.y2.Text) : 0;
k2 = int.TryParse(this.k2.Text, out i) ? Convert.ToInt32(this.k2.Text) : 0;

Console.WriteLine("x1: " + x1);
Console.WriteLine("y1: " + y1);
Console.WriteLine("k1: " + k1);
Console.WriteLine("x2: " + x2);
Console.WriteLine("y2: " + y2);
Console.WriteLine("k2: " + k2);




decimal trezultatas = (x1 * y1) * k1;
decimal trezultatas2 = (x2 * y2) * k2;

decimal rezultatas = trezultatas / 1000000;
decimal rezultatas2 = trezultatas2 / 1000000;

decimal brezultatas = rezultatas + rezultatas2;

Console.WriteLine("trez: " + trezultatas);
Console.WriteLine("rez :" + brezultatas);

label8.Text = brezultatas.ToString("0.00 m\u00b2");
Is this a logical code? Because everything works, now i can empty fields
Pobiega
Pobiega2y ago
no no no tryparse both parses AND tells you if it succeeded or not. if you want a default value if it fails, I'd suggest making a helper method
public static int ParseOrDefault(string str, int defaultValue = default)
{
return int.TryParse(str, out var i) ? i : defaultValue;
}
public static int ParseOrDefault(string str, int defaultValue = default)
{
return int.TryParse(str, out var i) ? i : defaultValue;
}
now each of your assignments is much simpler:
int x1 = ParseOrDefault(x1.Text);
int x1 = ParseOrDefault(x1.Text);
Bocens
BocensOP2y ago
Thank you very much, it is possible to know exactly why my code was not liked, although it works the same.
Pobiega
Pobiega2y ago
"works the same" isnt really true you used tryparse but then ignored the parsed value you also had to specify your default everywhere, which is just more noise parsing a value twice is wasteful
Bocens
BocensOP2y ago
Thanks for the help
Pobiega
Pobiega2y ago
if you feel that the thread is done, please /close it
Want results from more Discord servers?
Add your server