Here is my new tutorial for you about C#. In this tutorial i am going to make a little game about guessing result of some number summation, multiplation, division and addition. And i am going to focus on handling the enter keypress event for textfield.

Here as you see from the screenshot of the main window a user should guess the result of this operation. Textbox1 equals the random number “96″, textbox2 equals the random number “73″ and there is an randomly operator which is “-” . User should guess this result and should write down the guess into the textbox3 field. When user press enter button textbox3 handles this event.
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) //Char 13 handles the enter key
{
Console.WriteLine(total); //Here i can check the result from console for testing purposes
if (total.ToString() == textBox3.Text)
{
StringBuilder dogru_str = new StringBuilder();
dogru_str.Append(textBox1.Text);
dogru_str.Append(" " + label1.Text + " ");
dogru_str.Append(textBox2.Text + " = ");
dogru_str.Append(total + "\n");
dogru_str.Append("Your answer was correct...");
MessageBox.Show(dogru_str.ToString(), "Result", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Read more…
VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.11_1134]
Since there are some other ways as well. Here is my little function which can generate a random integer
private int RandomNumber(int min, int max)
{
Random random = new Random();
int rand = random.Next(min, max);
return rand;
}
I need to convert this interger number to a string. And this function takes to parameters minimum & maximum. That helps you to determine the interval of this random numbers.
//Determine about operator
String[] operators = { "+", "-", "/", "*" };
int rand3 = random.Next(1, 5);
//Console.WriteLine(rand3.ToString());
switch (rand3.ToString())
{
case "1":
label1.Text = "+";
total = rand1 + rand2;
break;
case "2":
label1.Text = "-";
total = rand1 - rand2;
break;
case "3":
label1.Text = "/";
total = rand1 / rand2;
break;
case "4":
label1.Text = "*";
total = rand1 * rand2;
break;
default:
label1.Text = "Error";
break;
}
Read more…
VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.11_1134]
This tutorial has two classes. ActionListener and Main class are seperated from each other.
- ParamListener.java
- BtnsListener.java

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* ParamListener.java
*/
public class ParamListener extends JFrame{
public ParamListener() {
JButton btn10 = new JButton("10");
JButton btn20 = new JButton("20");
JButton btn30 = new JButton("30");
JButton btn40 = new JButton("40");
btn10.addActionListener( new BtnsListener ("ten"));
btn20.addActionListener( new BtnsListener ("twenty"));
btn30.addActionListener( new BtnsListener ("thirty"));
btn40.addActionListener( new BtnsListener ("fourty"));
JPanel content = new JPanel();
content.add(btn10);
content.add(btn20);
content.add(btn30);
content.add(btn40);
setContentPane(content);
setTitle("A Tutorial");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public static void main(String[] args) {
new ParamListener();
}
}
Read more…
VN:F [1.9.11_1134]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.11_1134]
This tiny program lets you to choose background color of your pane.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class tut1 extends JFrame{
Color[] colors = {
Color.RED, Color.BLUE, Color.YELLOW, Color.BLACK, Color.ORANGE,
new Color(0,143,222)
};
String[] renk = {
"red", "blue", "yellow", "black", "orange", "turkuaz"
};
public JComboBox cmb = new JComboBox(renk);
public JPanel main = new JPanel();
public tut1() {
cmb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int index = cmb.getSelectedIndex();
main.setBackground(colors[index]);
}
});
cmb.setBackground(Color.blue);
JLabel lbl = new JLabel();
main.add(cmb);
main.add(lbl);
setContentPane(main);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setTitle("my title");
pack();
}
Read more…
VN:F [1.9.11_1134]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.11_1134]
You are going to need this for sure : )
function generatePassword($length=9, $strength=0) {
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvz';
if ($strength & 1) {
$consonants .= 'BDGHJLMNPQRSTVWXZ';
}
if ($strength & 2) {
$vowels .= "AEUY";
}
if ($strength & 4) {
$consonants .= '23456789';
}
if ($strength & 8 ) {
$consonants .= '@#$%';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
and..
Read more…
VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.11_1134]
Recent Comments