Hi.

I have some beginner questions related to C# in general, as well as my first home assignment in C#.

When the .exe file is opened, a window pops up and asks user a question. He has 2 answer options (2 buttons). The task is - make it impossible for the user to click on either of these 2 buttons.

My plan:
1.1.) If user moves cursor on the button, it changes location. Which makes it impossible for the user to click the button by using the mouse.
Done.

1.2.) Disabling tab button which allows user to navigate between buttons in the window. I changed button properties from TRUE to FALSE in the line which enables/disables tab button.
Done.

1.3.) I want to disable arrow keys as well. This is where (probably) some actual code needs to be written.

Which one makes more sense:
* disabling keys (up,down,left,right)
* make the buttons do something else when pressed. Instead of letting the user to switch between buttons, some sort of indicator could appear in the window, saying - "Arrow keys don't work. Sorry".

1.4) Are there other ways to press the button, which I'd have to remove?

http://support.microsoft.com/kb/320584
This is what I hope to use to block arrow keys. I haven't made it to work though.

The fact that Visual Studio 2008 crashed after I installed a security update does not help either. I'm now reinstalling Visual Studio 2008, and hopefully will be able to show you my code tomorrow.

2.) Could there be any version mismatch issues, if I create programs in VS2008, and they are checked by the teacher on VS2005? The 2008 version I installed (and it stopped working later) is legal, which I got for free as a student. I figured 2008 would be a better choice, cause it's free and more up to date.

3.) Care to share a good link to some good VS tutorials? If you know some, that is. I've looked some up but... the first thing you see is not always the best one.

Thanks.

The arrow keys are processed differently as they are caught by the form its self. So in order to do this you must override that method that catches them.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData == Keys.Right) || (keyData == Keys.Left) ||
                (keyData == Keys.Up) || (keyData == Keys.Down))
            {
                //Do custom stuff or nothing.
                //true if key was processed by control, false otherwise
                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

This code snippet is from Sknake here on the forum. He answered a similar question of mine about a year back.

Where exactly do I have to put that code snippet? Compiler returns error when I add it to my code. It says:
Error 1 Expected class, delegate, enum, interface, or struct

Honestly, I don't understand how the override works, and I've only learned about messagboxes in lectures so far. I have basic knowledge of C++ and I consider myself average in PHP. And C# seems very confusing at the moment.

This is what I have now. Buttons are called 'but_ja' and 'but_ne' .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Joks1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Random rnd = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 jaunaForma = new Form2();
            jaunaForma.Show();
        }

        private void but_ja_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Text........","Joke...",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            this.Close();
        }

        private void but_ja_Move(object sender, MouseEventArgs e)
        {
// this part moves the 'yes' button around.
            but_ja.Top=rnd.Next(this.Size.Height-2*but_ja.Size.Height);
            but_ja.Left=rnd.Next(this.Size.Width-2*but_ja.Size.Width);
        }
    }
}

just put it in the body of the form. just like you would any other method.
Overrides replace a method in the class's baseclass, this method overrides the proccess command key method of the control class and stops it if it gets an arrow key.

If you can handle C++ then C# will come simple to you. Don't be afraid to read. There are a MILLION C# books out there. or you can experiment as I do. Visualstudio's intellisense really helps you explore a class's possibilites.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Joks1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData == Keys.Right) || (keyData == Keys.Left) ||
                (keyData == Keys.Up) || (keyData == Keys.Down))
            {
                //Do custom stuff or nothing.
                //true if key was processed by control, false otherwise
                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        Random rnd = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 jaunaForma = new Form2();
            jaunaForma.Show();
        }

        private void but_ja_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Text........","Joke...",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            this.Close();
        }

        private void but_ja_Move(object sender, MouseEventArgs e)
        {
// this part moves the 'yes' button around.
            but_ja.Top=rnd.Next(this.Size.Height-2*but_ja.Size.Height);
            but_ja.Left=rnd.Next(this.Size.Width-2*but_ja.Size.Width);
        }
    }
}

Could your professor have meant something like disabling them as well? That was the first thing that came to my mind, but maybe that's oversimplifying it a bit.

My first thought was the same as jonsca's. If you want to make it impossible for the button to be clicked, set the buttons Enabled property to False. Then it doesnt matter if they can move the pmouse over them or use the keys. The button is visible but disabled so it cannot be clicked by any means.

If this is your first exercise, it seems unlikely that you are expected to tackle complex solutions already. Usually you build up to things like this; and generally your homework assignment will require you to use the code you were introduced to in class.
It always amazes me that people dont grasp that. I saw it frequently at university. We would spend a lecture being shown how a stack works then be asked to design a message queue. It was fairly obvious that we should build the queue using a stack since they spent 2hours teaching us about them, but some people would build the most complex system of nested if's and time checks and arrays instead.

Sorry to ramble, my point is this: your assignments will usually test your understanding of the subject matter covered in class. Aim to use everything you were shown in order to overcome the problem.

In your case, if you weren't shown how to catch key presses and mouse events, its unlikely you are expected to physically prevent the user getting at the buttons the way you are trying to.

If all else fails, ask your lecturer to clarify the objective and the learning outcomes. They are there to help you and noone knows what they are expecting better than they do :)

commented: Good Point! +2

It appeared to me that the OP was trying to prevent the button from getting focus by using the arrow keys. So that the user couldn't cheat and just focus the button and hit enter.

if the button was disabled it would be grayed out and the user probably wouldn't even try to click the button so 1.1 would have been pointless.

But it does seem like an odd homework assignment.

@Ryshad:
You have a point. However, there was not really much being taught during the first 2 lectures.
And the task was making it impossible for the user to click a button. Ways how to do it were up to students. I tried playing with properties but I did not find a way to disable arrow keys. So I ended up here. And now I know 3 ways how to do this task. Not that bad if you ask me :)

@Diamonddrake:
Thanks for your help. Learned quite a bit. Maybe even to much. :)

It appeared to me that the OP was trying to prevent the button from getting focus by using the arrow keys. So that the user couldn't cheat and just focus the button and hit enter.

if the button was disabled it would be grayed out and the user probably wouldn't even try to click the button so 1.1 would have been pointless.

But it does seem like an odd homework assignment.

I agree, disabling the button would render 1.1 pointless, but 1.1 was part of the OP's plan to make the button unclickable, not part of the requriement :)

As for stopping them cheating, it depends on how you interpret the problem. If you have to prevent 'clicking' that only requries that: a) the cursor cannot be placed over the button or b) the click event is not registered. If you just want the button to be unusable, then you either need to prevent focus completely or disable the button.

My concern is the intended purpose of the application; The only scenario i can think of where you would offer the user two buttons but prevent them clicking on either of them is some kind of annoying viral popup that looks like a messagebox but you cannot close it. Close cousin to the really annoying viral messagebox a'la "Are you a completely moron?" where only the Yes button works...*sigh*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.