Click here to Skip to main content
14,458,710 members
Rate this:
Please Sign up or sign in to vote.
I'm new in C# and I'm creating an aplication that can do a chart and also use the key event in the same form. But when I run the application the keyevent doesn't work,I do not know why is not working.

I will really appreciate any help to solve my question, thank you!

What I have tried:

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


namespace Second_ver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            sphere.Visible = true;
            anillo_arriba.Visible = false;
            anillo_abajo.Visible = false;
            simulation.Visible = true;
            contador();
            KeyPreview = false;
        }
           
        //Keypress event
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Variable for the moviment of the pictures
            int up_down, left_right;
            left_right = sphere.Location.X;
            up_down = sphere.Location.Y;
            if (e.KeyChar == 119)
            {
                //Wich picture is able to see
                sphere.Visible = false;
                anillo_arriba.Visible = true;
                anillo_abajo.Visible = false;
                //Points that are going to move the picture
                anillo_abajo.Location = new Point(left_right, up_down - 5);
                anillo_arriba.Location = new Point(left_right, up_down - 5);
                sphere.Location = new Point(left_right, up_down - 5);
            }

            if (e.KeyChar == 115)
            {
                //Wich picture is able to see
                sphere.Visible = false;
                anillo_arriba.Visible = false;
                anillo_abajo.Visible = true;
                //Points that are going to move the picture
                anillo_abajo.Location = new Point(left_right, up_down + 5);
                anillo_arriba.Location = new Point(left_right, up_down + 5);
                sphere.Location = new Point(left_right, up_down + 5);
            }

            double function(double x)
            {
                return (Math.Sin(x)); //Función de gráfica
            }

            chart1.ChartAreas[0].AxisY.ScaleView.Zoom(-10, 10); //Escala en Y
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(-5, 5);//Escala en X
            chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;

            for (int i = -10; i < 10; i++)
            {
               chart1.Series[0].Points.AddXY(i, function(i)); 
               chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
            }
        }
    }
}
Posted
Updated 26-Sep-17 15:22pm
v2
Comments
Graeme_Grant 26-Sep-17 20:27pm
   
BreakPoints are a powerful tool for understanding your own code, and code of others. It is called "debugging". Here is a video that will show you how to debug your code: Basic Debugging with Visual Studio 2010 - YouTube[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100