I see the last character of my string more than once after i string.empty it - textbox

I am writing a production program. And therefore i am working with barcodes.
I put a textbox to read the values from barcode scanner.
When the textbox length is 8 i take the value and do some work. But when i write like this:
textbox1.text=string.empty; i see the last character more than once in my textbox again.
For example i write by keyboard to the textbox 12345678 and hold my finger more than 1 second
even though i string.empty it i see on the textbox like 888888.
How can i solve this problem?
My textbox maxlength is set to 8 and my code is below.
enter code here
if(a<b)
{
TSCLIB_DLL.openport("TSC TTP-244 Plus");
TSCLIB_DLL.sendcommand("SIZE 46 mm, 15 mm");
TSCLIB_DLL.sendcommand("DIRECTION 0,0");
TSCLIB_DLL.sendcommand("REFERENCE 0,0");
TSCLIB_DLL.sendcommand("OFFSET 0 mm");
TSCLIB_DLL.sendcommand("GAP 3 mm");
TSCLIB_DLL.sendcommand("SPEED 10");
TSCLIB_DLL.sendcommand("SET PEEL OFF");
TSCLIB_DLL.sendcommand("SET TEAR ON");
TSCLIB_DLL.sendcommand("CLS");
TSCLIB_DLL.sendcommand("CODEPAGE 1254");
//TSCLIB_DLL.sendcommand("AUTODETECT[120,16]");
TSCLIB_DLL.clearbuffer();
string g = "115";
string h = "150";
string k = "50";
string l = "10";
string serialNumber = b.ToString();
string boxNumber = lblBoxNumber.Text;
TSCLIB_DLL.printerfont(h, l, "1", "0", "3", "3", boxNumber );
TSCLIB_DLL.printerfont(g, k, "1", "0", "2", "2", serialNumber);
TSCLIB_DLL.printlabel("1", "1");
TSCLIB_DLL.closeport();
textBox1.Text =null;
textBox1.Focus();
lblBoxNumber.Text = string.Empty;
}
if(a==b)
{...}
if(a>b)
{...}

OK, since I cannot see the rest of your code I'm just going to point out that the fact that the max length is 8, and you're seeing 88888888, I SUSPECT elsewhere in the code you have something like lblBoxNumber.Text += maxLength. Since you're holding the key down, maybe it's on a keystroke event handler??? Or else there's a serious bug in the framework.
I recommend that you find all references to that maxLength and focus on them.

Related

How to calculate this String text ="2+3-5+1" using Split method? [duplicate]

This question already has answers here:
Calculate string value in javascript, not using eval
(12 answers)
Closed 4 months ago.
When the text was '2+3+5+1', the logic was easy
Split('+') so the string is converted to an array.
loop over the array and calculate the sum.
check the code below
void main() {
const text = '2+3+5+1';
final array = text.split('+');
int res =0;
for (var i=0; i<= array.length -1; i++){
res+=int.parse(array[i]);;
}
print(array);
print(res);
}
Now this String "2+3-5+1" contains minus.
how to get the right response using split method?
I am using dart.
note: I don't want to use any library (math expression) to solve this exercice.
Use the .replace() method.
text = text.replace("-", "+-");
When you run through the loop, it will calculate (-).
You can split your string using regex text.split(/\+|\-/).
This of course will fail if any space is added to the string (not to mention *, / or even decimal values).
const text = '20+3-5+10';
const arr = text.split(/\+|\-/)
let tot = 0
for (const num of arr) {
const pos = text.indexOf(num)
if (pos === 0) {
tot = parseInt(num)
} else {
switch (text.substr(text.indexOf(num) - 1, 1)) {
case '+':
tot += parseInt(num)
break
case '-':
tot -= parseInt(num)
break
}
}
}
console.log(tot)
I see 2 maybe 3 options, definitely there are hundreds
You don't use split and you just iterate through the string and just add or subtract on the way. As an example
You have '2+3-5+1'. You iterate until the second operator (+ or -) on your case. When you find it you just do the operation that you have iterated through and then you just keep going. You can do it recursive or not, doesn't matter
"2+3-5+1" -> "5-5+1" -> "0+1" -> 1
You use split on + for instance and you get [ '2', '3-5', '1' ] then you go through them with a loop with 2 conditions like
if(isNaN(x)) res+= x since you know it's been divided with a +
if(!isNaN(x)) res+= x.split('-')[0] - x.split('-')[1]
isNaN -> is not a number
Ofc you can make it look nicer. If you have parenthesis though, none of this will work
You can also use regex like split(/[-+]/) or more complex, but you'll have to find a way to know what operation follows each digit. One easy approach would be to iterate through both arrays. One of numbers and one of operators
"2+3-5+1".split(/[-+]/) -> [ '2', '3', '5', '1' ]
"2+3-5+1".split(/[0-9]*/).filter(x => x) -> [ '+', '-', '+' ]
You could probably find better regex, but you get the idea
You can ofc use a map or a switch for multiple operators

Problem getting the decimal separator setting in Windows

I hope you can help me to solve this.
I am building a Windows VCL application with C++ Builder. I have a Tedit object in which the user should type a temperature value, 25.35 for example. Before processing it, I need to check if the number is written correctly and, in particular, I need to check if the decimal separator is correct comparing it to the Windows 10 settings. In case the separator is wrong, it should be automatically set correctly and re-write the Text of the Tedit object.
I wrote this code:
char separator = std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
AnsiString str = refTemperature->Text;
if (separator == ',') {
str = StringReplace( str, ".", separator, TReplaceFlags() <<rfReplaceAll );
}
else
str = StringReplace( str, ",", separator, TReplaceFlags() <<rfReplaceAll );
refTemperature->Text = str;
Here, separator is always ".", even when my Windows Control Panel settings are Decimal symbol = ',' and Digit grouping symbol = '.'. So, in this case, the code does not work. Have got a solution? Thank you.

box callback functions returning the same string in Rascal

I'm trying to draw some boxes in Rascal and trying to give each box its own callback function. On entering the box with the mouse the corresponding string should get displayed in the text element (so hovering box1 should display box1 etc.).
However, at the moment the text does pop up but just displays "box3" for each of the 3 boxes.
Any ideas?
strings = ["box1", "box2", "box3"];
boxes = [ box(
size(100, 100),
onMouseEnter(void() {
output = s;
})
) | s <- strings];
render(hcat([
vcat(boxes),
text(str () {return output;})
]));
Good question, classical problem. The essence of the problem is that Rascal uses "non-capturing closures": this means that functions that are returned from another function share the same context. In your case this is the variable s introduced by s <- strings. This nearly always happens when you create function values in a loop (as you do here). The solution is to wrap another function layer around the returned function.
Here is a simple example:
list[int()] makeClosures()
= [ int() {return i;} | i <- [0,1,2]];
void wrong(){
lst = makeClosures();
println(lst[0]());
println(lst[1]());
println(lst[2]());
}
which will print surprisingly the values 2,2and2`. The solution is, as said, to introduce another function level:
int() makeClosure(int i)
= int() { return i;};
list[int()] makeClosuresOK()
= [ makeClosure(i) | i <- [0,1,2]];
void right(){
lst = makeClosuresOK();
println(lst[0]());
println(lst[1]());
println(lst[2]());
}
now calling right() will print 1, 2, and 3 as expected.
I leave it as an exercise how this is done in your example, but I am prepared to give a solution when you ask for it. Good luck!

How to get float number in Lua from IUP text entry

I am trying following simple code to get a decimal value from user:
require( "iuplua" )
t1 = iup.text{expand = "YES", padding = "10x10", alignment="ARIGHT", size="40x"}
btn = iup.button {title = "Print:", padding = "10x10", alignment="ACENTER", size="40x"}
qbtn = iup.button{title="Quit", expand = "YES", padding = "10x10", alignment="ACENTER", size="40x"}
function btn:action()
strval1 = string.match (t1.value, "%d+")
print (strval1)
num = tonumber(strval1)
print (num)
end
function qbtn:action()
os.exit()
end
dlg = iup.dialog {
iup.vbox{
iup.hbox{
iup.label{title="Decimal:", padding = "10x10", alignment="ALEFT", size="40x"},
t1 },
iup.hbox{
btn,
qbtn }}}
dlg:show()
iup.MainLoop()
However, it prints out only an integer number (part before decimal- even 25.9999 prints as 25).
How can I get float or decimal value entered by user? Thanks for your help.
The problem with the code is that the pattern "%d+" in btn:action only gets the first run of digits.
You can change the pattern to handle decimal points, but it is hard to write a pattern that works in all cases, including optional sign and decimal point and scientific format.
It is best to let tonumber do its work: it will return nil if the string cannot be converted to a number.
Another option is to use the MASK attribute of the IupText to let the user only be able to enter numbers.

How to generate unique (short) URL folder name on the fly...like Bit.ly

I'm creating an application which will create a large number of folders on a web server, with files inside of them.
I need the folder name to be unique. I can easily do this with a GUID, but I want something more user friendly. It doesn't need to be speakable by users, but should be short and standard characters (alphas is best).
In short: i'm looking to do something like Bit.ly does with their unique names:
www.mydomain.com/ABCDEF
Is there a good reference on how to do this? My platform will be .NET/C#, but ok with any help, references, links, etc on the general concept, or any overall advice to solve this task.
Start at 1. Increment to 2, 3, 4, 5, 6, 7,
8, 9, a, b...
A, B, C...
X, Y, Z, 10, 11, 12, ... 1a, 1b,
You get the idea.
You have a synchronized global int/long "next id" and represent it in base 62 (numbers, lowercase, caps) or base 36 or something.
I'm assuming that you know how to use your web server's redirect capabilities. If you need help, just comment :).
The way I would do it would be generating a random integer (between the integer values of 'a' and 'z'); converting it into a char; appending it to a string; and repeating until we reach the needed length. If it generates a value already in the database, repeat the process. If it was unique, store it in the database with the name of the actual location and the name of the alias.
This is a bit hack-like because it assumes that 'a' through 'z' are actually in sequence in their integer values.
Best I could think of :(.
In Perl, without modules so you can translate more easly.
sub convert_to_base {
my ($n, $b) = #_;
my #digits;
while ($n) {
my $digits = $n % $b;
unshift #digits, $digit;
$n = ($n - $digit) / $b;
}
unshift #digits, 0 if !#digits;
return #digits;
}
# Whatever characters you want to use.
my #digit_set = ( '0'..'9', 'a'..'z', 'A'..'Z' );
# The id of the record in the database,
# or one more than the last id you generated.
my $id = 1;
my $converted =
join '',
map { $digit_set[$_] }
convert_to_base($id, 0+#digits_set);
I needed something similar to what you're trying to accomplish. I retooled my code to generate folders so try this. It's setup for a console app, but you can use it in a website also.
private static void genRandomFolders()
{
string basepath = "C:\\Users\\{username here}\\Desktop\\";
int count = 5;
int length = 8;
List<string> codes = new List<string>();
int total = 0;
int i = count;
Random rnd = new Random();
while (i-- > 0)
{
string code = RandomString(rnd, length);
if (!codes.Exists(delegate(string c) { return c.ToLower() == code.ToLower(); }))
{
//Create directory here
System.IO.Directory.CreateDirectory(basepath + code);
}
total++;
if (total % 100 == 0)
Console.WriteLine("Generated " + total.ToString() + " random folders...");
}
Console.WriteLine();
Console.WriteLine("Generated " + total.ToString() + " total random folders.");
}
public static string RandomString(Random r, int len)
{
//string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //uppercase only
//string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; //All
string str = "abcdefghjkmnpqrstuvwxyz123456789"; //Lowercase only
StringBuilder sb = new StringBuilder();
while ((len--) > 0)
sb.Append(str[(int)(r.NextDouble() * str.Length)]);
return sb.ToString();
}

Resources