Barcode generate doesn't work? - c#

I generate barcode but it is not active with scanner. I am using C# + SQL server 2102 and using font "#IDAHC39M Code 39 Barcode"
I using this code.
private void btngeneratebarcode_Click(object sender, EventArgs e)
{
string id;
Random barcode = new Random();
int i = barcode.Next();
id = i.ToString();
int k = int.Parse(id);
int x = k + 1;
string y = "0" + x + "0";
lbbarcode.Text = y.ToString();
lbbarcode.Visible = true;
textBox1.Text = x.ToString();
}
I display the barcode to a label and then print the label bar for product code, but while I scan with barcode scanner it is no active, no any result.
It is my first barcode generate. I don''t know that this is the right method.
Thnaks in advance.

Barcodes have specific rules to create them. You are using Code 39 for what I see, so you must add the start/stop char at the first and last position.
Usually the start/stop char is represented by the start (*), so if your font accomplishes that you can modify your code with this:
string y = "*0" + x + "0*";
Else you must change the star to the char used as start/stop by your font.

Related

How to write calculation correctly for program that checks what numbers between 1 and 1000 are dividable by entered value ( int variables)

I am writing a program that first lets a user enter a number between 1 and 10, and then checks what numbers between 1 and 1000 are dividable by that number.
both variables used are int variables, so for example if a user enters 4, it should read: 1 8 12 16 etc.
thusfar i have the following code:
private void button1_Click(object sender, EventArgs e)
{
int Getal1 = 1;
int Getal2;
Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2
for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
{
textBox2.Text = textBox2.Text + "\r\n" + Getal1 / Getal2 + "\r\n";
}
}
the textbook mentions using the following code : Getal1 % Getal2.. i tried adding it after the calculation above, but this didnt work.. math is not my strong point at all..
does someone have an explanation on this?
maybe if Getal1 % Getal2 = 0??.. im really guessing here..
thanks for any help in advance,
Stefan
I tried adding code from the textbook, expecting the program to work correctly, right now it does give a result, but not a correct result.. for example if i enter 2 it starts the sequence with the numbers 123..
for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
{
if (Getal1 % Getal2 == 0)
{
textBox2.Text += Getal1.ToString() + Environment.NewLine;
}
}
You can omit the division checking whatsoever by starting with the number introduced by the user and then adding this number in the loop. This way you will never get a number that is not dividable.
For example:
if user inputs 4, you start with 4
than you add 4 to it and get 8
add 4 again, get 12 and so on
private void button1_Click(object sender, EventArgs e)
{
int Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2
for (int Getal1 = Getal2; Getal1 <= 1000; Getal1 += Getal2)//// for loop.
{
textBox2.Text += Getal1 + "\r\n";
}
}

Random Sequence Generator in C#

I want to build a sequence randomiser mobile app in c#. I would like to retrieve the smallest and the largest number in an interval from two diffrent text boxes, and after I click the Generate button to display the random sequence of all the numbers in a new text box.
My code only displays one number. What's wrong with it?
Thanks.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
int seed = 345;
var result = "";
int min = Convert.ToInt32(textBox.Text);
int max = Convert.ToInt32(textBox2.Text);
Random r3 = new Random(seed);
for (int i = min; i < max; i++)
{
ecran.Text = (/*"," + r3.Next(min, max)*/i).ToString();
}
}
To clarify what was wrong with your solution:
Inside the loop you were constantly reassigning the value of ecran.Text.
i.e.
1st loop cycle > ecran.Text = ", " + 77
2nd loop cycle > ecran.Text = ", " + 89
//Value of ecran.Text after 1st cycle is ", 77"
//Value of ecran.Text after 2nd cycle is ", 89"
Overriding the value of ecran.Text with each iteration.
Fixed by adding a plus symbol in front of equals ecran.Text += ", " + LOGIC
This happens because you assign sequence values to ecran.Text in a loop. Instead, you should create a string representation of the sequence, and assign it at the end.
Use Shuffle<T> method from this Q&A:
int min = Convert.ToInt32(textBox.Text);
int max = Convert.ToInt32(textBox2.Text);
if (max < min) return;
var sequence = Enumerable.Range(min, max-min+1).ToList();
Shuffle(sequence);
ecran.Text = string.Join(",", sequence);

What does "str" + x + "str" mean?

Why would you use "str" + x + "str" in ImageLocation.
private void CreateEnemies()
{
Random rnd = new Random();
int x = rnd.Next(1, kindOfEnemies + 1);
PictureBox enemy = new PictureBox();
int loc = rnd.Next(0, panel1.Height - enemy.Height);
enemy.SizeMode = PictureBoxSizeMode.StretchImage;
enemy.ImageLocation = "Aliens/" + x + ".png";
}
I don't understand why you would use this.
The + operator is used for adding. If used on a string it will not add two strings, but concatenate them:
var text = "Hello" + "World" + "String";
Console.WriteLine(text); // Prints "HelloWorldString"
So the code above just constructs a string. Because the variable x is not of type int, .Net will automatically call .ToString().
int x = 5;
var text1 = "Aliens/" + x +".png"; // is the same as below.
var text2 = "Aliens/" + x.ToString() +".png"; // is the same as above.
Console.WriteLine(text); // Prints "Aliens/5.png"
In C# version 6 and above you can also use string interpolation, which makes things clearer:
var text1 = $"Aliens/{x}.png"; // is the same as below.
var text2 = $"Aliens/{x.ToString()}.png"; // is the same as above.
With string interpolation, you can embed variables into a string, by placing them into curly braces.
Note that the string has to start with a $.
+ is used for string concatenation
This is a way to randomize the image of the alien that you get.
Your solution has a folder called Aliens with files named 0.png, 1.png, 2.png, and so on in it. Each file has an image of an "alien", which your program loads into a PictureBox. Your code picks one of these files at random, using string concatenation.
With C# 6 and newer you can use string interpolation:
enemy.ImageLocation = $"Aliens/{x}.png";
It is concatenating strings together. So "Aliens/" + The string value of 'x' + ".png" are being 'added' together.
Lets say:
int x = 1
The output string would be
"Aliens/1.png"

Add dash '-' on evry four character input in Textbox for store Credit Card Information in Windows phone 8.1 c#

I am developing bank application , I have to store Credit Card information for that
Input card number from user
on every four character dash '-' will be added on that text box
e.g 1234-1234-1234-1234
I have try KeyDown and TextChanged, but didn't get answer.
key down event
txtcardNumber.Focus(FocusState.Keyboard);
string temp = string.Empty;
if (txtcardNumber.Text.Length == 4)
{
temp = txtcardNumber.Text;
txtcardNumber.Text += '-';
txtcardNumber.UpdateLayout();
}
else if (txtcardNumber.Text.Length == 9)
{
txtcardNumber.Text = txtcardNumber.Text + "-";
}
else if (txtcardNumber.Text.Length == 14)
{
txtcardNumber.Text = txtcardNumber.Text + "-";
}
when it add '-' cursor automatically goes at the start , I don't know why
You can use MaskedTextBoxes for such scenarios. and you can set the mask as follows:
mskCardBox.Mask = "0000-0000-0000-0000";
May be this helps you....
Dont wait till dash come after 4 digits.
Take a textbox double click it and write the below code and make that textbox autopostback=true.
write all 16 digits at a time and click tab or enter , dash will come automatically by splitting 4 digits each.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string cardno = TextBox1.Text;
var list = Enumerable
.Range(0, cardno.Length / 4)
.Select(i => cardno.Substring(i * 4, 4))
.ToList();
var resl = string.Join("-", list);
TextBox1.Text = resl;
}
I found simple solution
var insertText = "-";
var selectionIndex = creditcard.SelectionStart;
creditcard.Text = creditcard.Text.Insert(selectionIndex, insertText);
creditcard.SelectionStart = selectionIndex + insertText.Length;

c# Percentage calculator

I have little problem, and I need help..
So here is my problem I have created win form in c# and used numericupdown element to insert my numbers, but I cant calculate percent. Here is the code below:
private void button8_Click(object sender, EventArgs e)
{
int x, y, sum;
x = Convert.ToInt16(numericUpDown7.Value);
y = Convert.ToInt16(numericUpDown8.Value);
sum = x * 3.4528 + 21%;
textBox5.Text = Convert.ToString(sum);
}
What I need to do is to insert x and press button to calculate this formula
example: x * 3.4528 + 21 % = ???
Maby someone has options to help me.
Thanks for all of you, who will help me!
Try this
sum = (x * 3.4528) * 1.21;
private void button1_Click(object sender, EventArgs e)
{
double eng, urdu, math, cs, tot, per;
eng = Convert.ToDouble(txtenglish.Text);
urdu = Convert.ToDouble(txturdu.Text);
math = Convert.ToDouble(txtmath.Text);
cs = Convert.ToDouble(txtcs.Text);
tot = eng + urdu + math + cs;
lbltotal.Text = Convert.ToString(tot);
per = (tot / 400) * 100;
lblpercent.Text = Convert.ToString(per);
}
First off you need to use decimal, float, or double instead of int (you can find many references online about each to help you determine which would be best for you). Otherwise it will just truncate the answer and drop anything after the decimal point. Second you need to use the formula that everyone else has mentioned sum = x * 3.4528 * 1.21.

Categories

Resources