Silverlight NumericUpDown: Set max character length, or validate against it? - silverlight

I'm attempting to use the key up event to hijack the event and prevent two things. Anything but numeric keys or delete/back as well as once they hit a certain max of character length, inhibit the entry.
This solves the first validation portion, but I can't seem to figure out a way to prevent entry past a certain character length.
private void numericFieldInputField_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 ||
e.Key == Key.D6 || e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 ||
e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 || e.Key == Key.Back || e.Key == Key.Delete ||
e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Tab)
{
e.Handled = true;
}
else
{
//show validation
}
}
When I check the length of the numeric updown's value, cast it to string and look at the length property at this point, the last character they entered won't register. Also, when I set e.Handled to true, it still enters the character into the input. I thought e.Handled = true would keep the UI from putting the character into the input?

I would suggest using a Validator. In the databinding you specify the validator you want, the framework takes care of the rest. Here's one good intro to the topic: Tutorial

Related

How to use string variable as Table name in SQL query

Making a winforms application and I want the name of the table in my SQL query to be equal to a variable I have called TreeCodeName. This is my code:
private void button1_Click(object sender, EventArgs e)
{
string TreeNodeName = treeView1.SelectedNode.Name.ToString();
if ((TreeNodeName == "BOOKS") || (TreeNodeName == "CARDS") || (TreeNodeName == "COMPUTERS") || (TreeNodeName == "CONSOLES") || (TreeNodeName == "DEVICES") || (TreeNodeName == "DONORS") || (TreeNodeName == "MAGAZINES") || (TreeNodeName == "MOTHERBOARDS") || (TreeNodeName == "OTHERS") || (TreeNodeName == "PARTS") || (TreeNodeName == "PERIPHERALS") || (TreeNodeName == "POWER_SUPPLY") || (TreeNodeName == "PROCESSORS") || (TreeNodeName == "RAMS") || (TreeNodeName == "SOFTWARE") || (TreeNodeName == "STORAGE") || (TreeNodeName == "TERMINALS"))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * From Table Where Title = #Title";
command.Parameters.AddWithValue("#Title", TreeNodeName);
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
Whenever I try to test this, I run into an error that says there was a syntax error on FROM. Any way to salvage this?

Error expected identifier before '(' token in c

About
It's a program to calculate the actual age of a person with years, months and days.
I keep getting an error, and I'm not sure how to fix it.
Code:
#include <stdio.h>
int main(){
int dia,mes,anio;
int dia_n,mes_n, anio_n;
int edad_a,edad_m, edad_d;
printf("********Ingrese la fecha actual********\nDia:");
scanf("%d",&dia);
printf("Mes:");
scanf("%d", &mes);
printf("Anio:");
scanf("%d", &anio);
printf("********Ingrese la fecha de nacimiento********\nDia:");
scanf("%d",&dia_n);
printf("Mes:");
scanf("%d", &mes_n);
printf("Anio:");
scanf("%d", &anio_n);
if (dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n){
edad_d=(dia - dia_n) +30;
}
else
if (dia>dia_n) || (dia_n==dia) && (mes>mes_n) || (mes_n>mes) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n){
edad_d=dia-dia_n;
}
if (dia>dia_n) || (dia==dia_n) && (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n) {
edad_m=mes-mes_n;
}
else
if (dia_n>dia)&&(mes_n>mes) || (mes==mes_n) && (anio>anio_n){
edad_m=(mes-mes_n) +11;
}
else
if (dia==dia_n) || (dia>dia_n) && (mes_n>mes) && (anio>anio_n){
edad_m=(mes-mes_n)+12;
}
else
if (dia_n>dia) && (mes>mes_n) && (anio>anio_n) || (anio==anio_n) {
edad_m=(mes-mes_n) -1;
}
if (dia>dia_n) || (dia==dia_n) && (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n) {
edad_a=anio-anio_n;}
else
if (dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n){
edad_a=(anio-anio_n)-1;
}
printf("********Su edad actual es********\n%d anios %d meses y %d dias", edad_a, edad_m, edad_d);
return 0;
}
Error:
22 19 C:\Users\Briansucho\Desktop\Sin Nombre2.c
[Error] expected identifier before '(' token
The problem in your code is your first if statement. Your code states:
if (dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n){
This wrong because all those conditions need to be covered in a final (). What I mean is this:
if ((dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n)){
You just required more (). That is why you are getting that error. Also, your code isn't clean and has a lot of error. I know I should do this, but here is your code all cleaned up error free.
#include <stdio.h>
int main(){
int dia,mes,anio;
int dia_n,mes_n, anio_n;
int edad_a,edad_m, edad_d;
printf("********Ingrese la fecha actual********\nDia:");
scanf("%d",&dia);
printf("Mes:");
scanf("%d", &mes);
printf("Anio:");
scanf("%d", &anio);
printf("********Ingrese la fecha de nacimiento********\nDia:");
scanf("%d",&dia_n);
printf("Mes:");
scanf("%d", &mes_n);
printf("Anio:");
scanf("%d", &anio_n);
if ((dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio == anio_n)){
edad_d=(dia - dia_n) +30;
}
else if ((dia>dia_n) || (dia_n==dia) && (mes>mes_n) || (mes_n>mes) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n)){
edad_d=dia-dia_n;
}
if ((dia>dia_n) || (dia==dia_n) && (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n)) {
edad_m=mes-mes_n;
}
else if ((dia_n>dia)&&(mes_n>mes) || (mes==mes_n) && (anio>anio_n)){
edad_m=(mes-mes_n) +11;
}
else if ((dia==dia_n) || (dia>dia_n) && (mes_n>mes) && (anio>anio_n)){
edad_m=(mes-mes_n)+12;
}
else if ((dia_n>dia) && (mes>mes_n) && (anio>anio_n) || (anio==anio_n)) {
edad_m=(mes-mes_n) -1;
}
if ((dia>dia_n) || (dia==dia_n) && (mes>mes_n) || (mes==mes_n) && (anio>anio_n) || (anio==anio_n)) {
edad_a=anio-anio_n;}
else if ((dia_n>dia) && (mes_n>mes) || (mes>mes_n) || (mes==mes_n) && (anio>anio_n)){
edad_a=(anio-anio_n)-1;
}
printf("********Su edad actual es********\n%d anios %d meses y %d dias", edad_a, edad_m, edad_d);
return 0;
}

Struggling with TicTacToe in C

I tried to program a simple TicTacToe in C. The program itselfs compiles and show no errors.
So the program draws the field, reads the names and let a player put his symbol in a specific field. All this happens in a while loop, the should be run until its a draw or someone wins.
But the program just run the loop one time so there is only one turn then the program stops.
So here is my code:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
#define NAMELENGTH 20
int main(void) {
char names[2][NAMELENGTH];
// field positions
char field[9];
int winner = -1;
getnames(names);
printf("\n\n");
initField(field);
// set field positions to 'empty'
char actualPlayer = (char)(get_random_number()*10.0) % 2;
while (1) {
drawField(field);
turn(actualPlayer, names, field);
winner = isWinner(actualPlayer, field);
drawField(field);
if (winner >= 1) {
printwinner(winner, names);
return 0;
}
else if (winner == 0) {
printDrawGame(names);
return 0;
}
actualPlayer = (actualPlayer + 1) % 2;
}
return 0;
}
game.h Headerfile:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NAMELENGTH 20
#pragma warning(disable:4996)
extern void drawField(char *field);
extern void getnames(char nameField[][NAMELENGTH]);
extern void initField(char *field);
extern void turn(char actualPlayer, char names[][NAMELENGTH], char *field);
extern char isWinner(char actualPlayer, char *field);
extern void printwinner(char winnerNumber, char names[][NAMELENGTH]);
extern void printDrawGame(char names[][NAMELENGTH]);
extern double get_random_number();
and then my game.c where I defined my functions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#define NAMELENGTH 20
#pragma warning(disable:4996)
void drawField(char *field) {
printf("________________________\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[0], field[1], field[2]);
printf("|_______|_______|______|\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[3], field[4], field[5]);
printf("|_______|_______|______|\n");
printf("| | | |\n");
printf("| %c | %c | %c |\n", field[6], field[7], field[8]);
printf("|_______|_______|______|\n");
}
void getnames(char nameField[][NAMELENGTH]) {
printf("Name of the first player: ");
scanf("%s", nameField[0]);
printf("Name of the second player: ");
scanf("%s", nameField[1]);
}
void initField(char *field) {
for (int i = 0; i <= 8; i++)
{
field[i] = i + '1';
}
}
void turn(char actualPlayer, char names[][NAMELENGTH], char *field) {
char symbol = ' ';
int p1fieldnumber;
int p2fieldnumber;
if (actualPlayer == 0)
{
do {
printf("\nIts Player %s's turn.", names[0]);
char symbol = 'X';
printf("\nNumber of the field which you want to put your symbol in: ");
scanf("%d", &p1fieldnumber);
if (field[p1fieldnumber] == 'X' || field[p1fieldnumber] == 'O')
{
printf("\nField is already occupied!");
p1fieldnumber = 10;
}
} while (p1fieldnumber < 1 || p1fieldnumber > 9);
field[p1fieldnumber-1] = 'X';
}
else {
do {
printf("\nIts Player %s's turn.", names[1]);
char symbol = 'O';
printf("\nNumber of the field which you want to put your symbol in: ");
scanf("%d", &p2fieldnumber);
if (field[p2fieldnumber] == 'X' || field[p2fieldnumber] == 'O')
{
printf("\nField is already occupied!");
p2fieldnumber = 10;
}
} while (p2fieldnumber < 1 || p2fieldnumber > 9);
field[p2fieldnumber-1] = 'O';
}
}
char isWinner(char actualPlayer, char *field) {
char pwinner = '3';
if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
(field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O') ||
(field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O') ||
(field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O') ||
(field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O') ||
(field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O') ||
(field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O') ||
(field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O'))
{
pwinner = '2';
}
else if (((field[0] == 'X') && (field[1] == 'X') && (field[2] == 'X')) ||
(field[3] == 'X') && (field[4] == 'X') && (field[5] == 'X') ||
(field[6] == 'X') && (field[7] == 'X') && (field[8] == 'X') ||
(field[0] == 'X') && (field[4] == 'X') && (field[8] == 'X') ||
(field[2] == 'X') && (field[4] == 'X') && (field[6] == 'X') ||
(field[0] == 'X') && (field[3] == 'X') && (field[6] == 'X') ||
(field[1] == 'X') && (field[4] == 'X') && (field[7] == 'X') ||
(field[2] == 'X') && (field[5] == 'X') && (field[8] == 'X'))
{
pwinner = '1';
}
else if (((field[0] == 'X') || (field[0] == 'O')) && ((field[1] == 'X') || (field[1] == 'O')) && ((field[2] == 'X') || (field[2] == 'O')) ||
((field[3] == 'X') || (field[3] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) ||
((field[6] == 'X') || (field[6] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
((field[0] == 'X') || (field[0] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')) ||
((field[2] == 'X') || (field[2] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
((field[0] == 'X') || (field[0] == 'O')) && ((field[3] == 'X') || (field[3] == 'O')) && ((field[6] == 'X') || (field[6] == 'O')) ||
((field[1] == 'X') || (field[1] == 'O')) && ((field[4] == 'X') || (field[4] == 'O')) && ((field[7] == 'X') || (field[7] == 'O')) ||
((field[2] == 'X') || (field[2] == 'O')) && ((field[5] == 'X') || (field[5] == 'O')) && ((field[8] == 'X') || (field[8] == 'O')))
{
pwinner = '0';
}
return pwinner;
}
void printwinner(char winnerNumber, char names[][NAMELENGTH]) {
if (winnerNumber == '1') {
printf("Player %s won!", names[0]);
}
else if (winnerNumber == '2') {
printf("Player %s won!", names[1]);
}
}
void printDrawGame(char names[][NAMELENGTH]) {
printf("Draw!");
}
static int _initialized;
double get_random_number() {
if (!_initialized) {
srand(time(NULL));
_initialized = 1;
}
return (double)rand() / ((double)(RAND_MAX)+1);
}
There are multiple issues
isWinner returns characters '0', '1', '2' and '3'. Replace them with numbers 0, 1, 2, 3, because your are comparing with numbers in main.
In main instead of checking if (winner >= 1) {, which is true even if 3 is returned by isWinner, check for if (winner == 1 || winner == 2) {.
Suggest to use parentheses in isWinner around each condition separated by ||.
Parentheses as shown below for the first condition
if (((field[0] == 'O') && (field[1] == 'O') && (field[2] == 'O')) ||
((field[3] == 'O') && (field[4] == 'O') && (field[5] == 'O')) ||
((field[6] == 'O') && (field[7] == 'O') && (field[8] == 'O')) ||
((field[0] == 'O') && (field[4] == 'O') && (field[8] == 'O')) ||
((field[2] == 'O') && (field[4] == 'O') && (field[6] == 'O')) ||
((field[0] == 'O') && (field[3] == 'O') && (field[6] == 'O')) ||
((field[1] == 'O') && (field[4] == 'O') && (field[7] == 'O')) ||
((field[2] == 'O') && (field[5] == 'O') && (field[8] == 'O')))

Hook CTRL+ALT+TAB

I've made a WPF application and I was able to hook some of the windows combinations. ALT+TAB is hooked and it is doing nothing when my application is running (as expected). The problem is when I press the CTRL+ALT+TAB I get the same effect as ALT+TAB.
Do you guys have any idea on how to hook this kind of combination?
EDIT:
I have already successfully hooked ALT+TAB. I do want to hook CTRL+ALT+TAB. I've tried this project example to make this happen.
Here's the code that makes the hook:
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam,
ref KBHookStruct lParam){
if (nCode == 0)
{
if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) || // Alt+Tab
((lParam.vkCode == 0x1B) && (lParam.flags == 0x20)) || // Alt+Esc
((lParam.vkCode == 0x1B) && (lParam.flags == 0x00)) || // Ctrl+Esc
((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key
((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) || // Right Windows Key
((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) || // Alt+F4
((lParam.vkCode == 0x20) && (lParam.flags == 0x20))) // Alt+Space
{
return new IntPtr(1);
}
}
return CallNextHookEx(hookPtr, nCode, wParam, ref lParam);}
Answer 1
You can try like below it work for CTRL + SHIFT + TAB and CTRL + TAB
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
MessageBox.Show("CTRL + SHIFT + TAB trapped");
}
if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
MessageBox.Show("CTRL + TAB trapped");
}
}
Answer 2
should look something like:
((lParam.flags & 33 == 33) && (lParam.flags & 22 == 22))
32 and 22 are arbitrary in this example. You need to figure out what values ALT and CTRL actually are. They will be 1, 2, 4 ... 16, 32 etc. so that they can be OR'ed together into a single value.

moving objects withe key - diagonal

I need to move an object, I can move it up down and sideways, but fails in a diagonal, it is the way I made my move.
if (e.Key == Key.Up && Canvas.GetTop(Good) > 31)
{
double top = Canvas.GetTop(Good);
Canvas.SetTop(Good, top - 7);
}
if (e.Key == Key.Down && Canvas.GetTop(Good) < CanvasA.ActualHeight - 7 - Good.Height)
{
double down = Canvas.GetTop(Good);
Canvas.SetTop(Good, down + 7);
}
if (e.Key == Key.Left && Canvas.GetLeft(Good) > -2)
{
double left = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, left - 7);
}
if (e.Key == Key.Right && Canvas.GetLeft(Good) < CanvasA.ActualWidth - Good.Width)
{
double right = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, right + 7);
}
}
}
Everything works but can not click on the two buttons to move the character in a diagonal,
The reason that there is less than 31, it's because I did Menu in WPF.

Resources