|
Richard.
I changed the types. Thanks for your help.
|
|
|
|
|
Could, in addition to the ideas from the other respondents, be a culture thing. In some cultures, ',' is a thousands separator so your 113,54 could be treated as a misplaced thousands separator and assumed to be 11,354 which is 11354. If in your culture ',' is a decimal separator (which I am guessing is why you are dividing by 100 to get 113.54 [using my locale's decimal separator]) and your program is not using the same culture then it will misinterpret the value.
See, for example, c# - float.Parse() doesn't work the way I wanted - Stack Overflow[^]
See, for example, Single.Parse Method (System) | Microsoft Docs[^] (I know this is for single, rather than for float; but they work the same way and the version for float is playing hide-and-seek).
|
|
|
|
|
Hi, jsc42.
I tried what you suggested and it aparently works.
Thanks.
|
|
|
|
|
Hello.
I'm programming in C# with Visual Studio using forms.
I have a form with 8 textboxes, inside a groupbox. Depending on the data I retrieve from a database, I need to make some of them invisible. So, at running time I have to make them invisible. The names of the Textboxes are Tb_1, Tb_2 an so on. For example, if I retrive the number of fields as 6, I must make 2 of them invisible (the last 2). Can anybody help me do this?
Thanks.
|
|
|
|
|
|
To add to what Gerry has said, you can set the visibility without knowing the names, provided they are added to the GroupBox in the right order (you can reorder them by editing the myCodeFile.designer.cs file carefully):
int tbNo = 0;
foreach (control c in MyGroupBox.Controls)
{
if (c is TextBox tb)
{
tb.Visible = tbNo++ < numberYouWantToSee;
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi OriginalGriff.
I think your solution works fine. Thank you.
|
|
|
|
|
A better design is to make them disabled (control.enabled = false) instead of playing with the visibility. Then you avoid having controls in different places or holes in your form.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
A generalization. Nothing wrong with collapsing part of the UI if it provides more real estate where it's needed and wanted depending on the context. You just need a (anchor) "plan" that doesn't disorient. No different than toggling ribbons, toolbars, menus, popups and the like.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Of course it is a generalisation but disabling is still generally the accepted solution. If real estate is such a problem then introduce the OP to tabs!
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi,
In the past I've dealt with this issue (UI holes) in different ways, depending on layout; in a TableLayoutPanel, you can hide rows or columns by setting width or height to #0.
In a Panel, docking can be used to resize visible controls when other controls are hidden: accordion style controls are easily created.
I don't think the OP here needs these techniques, or, I'd post an example.
Depending on context, and end-user, a "hole" may be better than "pretty" ... its "absence" can convey information.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Hi,
The following code causes error when the active sheet has a name other than "DataSet1". I want to handle errors in these codes:
IWorkbook workbook = spreadsheetControl.Document;
Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
try
{
spreadsheetControl.Invoke((MethodInvoker)delegate { workbook.Worksheets.ActiveWorksheet = workbook.Worksheets["DataSet1"]; });
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(ex.ToString());
return;
}
But, there is runtime error inside Try block that says:
Quote: System.ArgumentOutOfRangeException: 'Worksheet with this name is not found.
How can I control error handing in this situation?
modified 31-Jul-21 23:07pm.
|
|
|
|
|
Don't use hard coded sheet names. Use the relevant Workbook method to find the names of the Worksheets within it.
|
|
|
|
|
Hi,
I want to handle error when there is no CreateTable.sql script file in the defined path by giving an alarm in MessageBox.
if (!File.Exists(databaseName))
{
using (SQLiteConnection sqliteCon = new SQLiteConnection(@"Data Source = " + databaseName))
{
sqliteCon.Open();
try
{
string dbCreationScript = File.ReadAllText(@"CreateTable.sql");
}
catch (Exception)
{
MessageBox.Show("Script file not found");
}
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = dbCreationScript;
cmd.Connection = sqliteCon;
cmd.ExecuteNonQuery();
sqliteCon.Close();
}
}
dbCreationScript is out of current context.
How can I control error handling for CreateTable.sql script file?
Variables declared inside Try/Catch block are out of scope.
modified 31-Jul-21 5:22am.
|
|
|
|
|
You can't "pass" a variable - any variable - from inside a scope to outside it: that is what scope is there for!
What that means is that if you declare a variable inside a pair or curly brackets, it is available to all code within those brackets (including nested blocks of code with their own curly brackets) but at the closing curly bracket of the block the variable ceases to exist and can no longer be accessed.
You would have to move the creation of the variable outside the try...catch block in order to use it.
But ... that code is pretty bad:
1) Never, ever create empty catch blocks. All that does is hide the error from running code until it's too late to do anything about it, and throw away all the information you as the developer need to fix the problem when you test it!
At a bare minimum, provide a Exception variable and log it to the Debug stream:
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
} This give you the developer a warning that it happened, and provides the exception detail for you to look at with the debugger.
2) The try block should cover the whole operation, right up to and including the ExecuteNonQuery - because if an error is going to occur with the data you are reading from your file, that's where it will happen!
3) You don't need to close the connection: the end of the using block will do that.
4) Don't hardcode filenames! Use a variable (or const ) so you only have to change it in one place!
5) Don't store your data in the application folder! That generally fails in production becasue teh app folder is write protected ...
See here: Where should I store my data?[^] for some better ideas.
So try it like this:
if (!File.Exists(databaseName))
{
using (SQLiteConnection sqliteCon = new SQLiteConnection(@"Data Source = " + databaseName))
{
sqliteCon.Open();
string dbCreationScript = null;
try
{
dbCreationScript = File.ReadAllText(pathToFile);
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.CommandText = dbCreationScript;
cmd.Connection = sqliteCon;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How about declaring the
string dbCreationScript outside the try block?
|
|
|
|
|
Hi.
I'm using C# in Visual Studio and I installed the ItextSharp package to deal with PDF files. It's working fine. My problem is that when I instaled the executable file in anothe machine, it showed an error when I tried to generate a PDF file. I think the problem is that I didn't copy files for the itextSharp. Can anybody tell me which files I should copy to solve the problem?
Thanks.
|
|
|
|
|
In the bin\Release folder of your project, you'll find the .DLL's that need to be shipped with your executable.
|
|
|
|
|
Dave. This folder is empty. The folder bin\debug has some files starting with itextsharp. But only with these files it didn't work. An there are some other files there. Do you have other suggestion?
Thanks.
|
|
|
|
|
You really need to learn how Visual Studio works.
If the Release folder is empty, you're still compiling a Debug version of your code. Then all of the files are in the bin\Debug folder. This is usually just for development and debugging of your project. Changing the build target to Release will build a Release version of the project with optimized code.
|
|
|
|
|
Hi,
I have 15 toggle switches in my project (Using DevExpress). All of them are related to column 2 of the DataTable and are used for filtering. I want to filter the DataTable based on those toggle switches the user turns on. I don't know what algorithm should I use to set my filtering and cover all the toggle switches that are on.
DataView dv = new DataView(my_table3);
dv.RowFilter = $"Column2 = '{checked_item2[0]}' OR Column2 = '{checked_item2[1]}' OR Column2 = '{checked_item2[2]}'";
|
|
|
|
|
I don't think string interpolation will work there: interpolated strings are a syntactic sugar for the older method using numbered parameters and are evaluated when the interpolated string is constructed, not when it is used:
using System;
public class Program
{
static string name = "???";
static string xxx = $"Hello {name}!";
static string yyy = "Hello {0}!";
public static void Main()
{
name = "Paul";
Console.WriteLine($"Hello {name}!");
Console.WriteLine(xxx);
Console.WriteLine(yyy, name);
}
} Will give you:
Hello Paul!
Hello ???!
Hello Paul! So setting a RowFilter to the value of a variable doesn't mean that it will change the View at all whcn checks change: it will only use the values at the moment when the string is created.
What you need to do is change the filter each time the user changes the check boxes.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This filter is a part of a bigger calculations. I export a huge Excel file into DataTable and filter rows based I toggle switches and continue the calculations. This process is done when the user clicks a button. So I don't need to have synchronous filtering.
My problem is that I don't know how many toggle switches will be on to set them in my Filter
String. Please guide me.
|
|
|
|
|
If you don't know, then you need to process them.
Create a List<string> and loop through your switches: if it's set, add the condition string to the collection:
if (myCheck.Checked)
{
myList.Add($"Column2 = '{checked_item2[n]}'");
} Then when you have them all, use string.Join[^] with a separator of " OR " to produce your filter.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How can I join List<> elements parametrically?
For example I don't know the count of checked toggle switches at first but during the process I find out that n = 5.
Sting.Join(" OR ", checked_item2[0], .....);
|
|
|
|