Hi my friends,
I wrote a program in C#.
I want user put a number(n) in textBox and click button and then my
program, draw a polygonal according with (n).
My program include 2 class:
1- class Form1
2- class View (this class use opengl library to draw polygonal)
But i have problem in pass (n) to glDraw method (one methods of View class)
I defined a public variable in Form1 class with this name: n
Then in button method set n equal the number that user entered in textbox.
Then ret() method in Form1 class, return n (the number that user entered)
In glDraw method of View class, i set:
Form1 mt = null;
int m = 3;
m = mt.ret();
To use m in glDraw method.
If i set a static number instead m in glDraw method, program works correctly, but when i was receive number from user (like i explained before) it doesn't work and take an error.
I add my program and error's picture to my post.
I will be thankful if help me to complete my program.
sorry for my long description.
Best regards,
Nima
error picture: http://atn.parsaspace.com/error%20pic.jpg
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CsGL.OpenGL;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public int n;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
n = Int32.Parse(InNumber.Text);
View view = new View();
view.Dock = DockStyle.Fill;
Controls.Add(view);
}
public int ret()
{
return n;
}
}
class View : OpenGLControl
{
public override void glDraw()
{
GL.glClear(GL.GL_COLOR_BUFFER_BIT |
GL.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
GL.glBegin(GL.GL_LINES);
Form1 mt = null;
int m = 3;
m = mt.ret();
int i;
double x0, y0, x1, y1, th = 360 / m;
x0 = 1 * Math.Cos(th * Math.PI / 180);
y0 = 1 * Math.Sin(th * Math.PI / 180);
for (i = 1; i <= m; i++)
{
x1 = 1 * Math.Cos(((i + 1) * th) * Math.PI / 180);
y1 = 1 * Math.Sin(((i + 1) * th) * Math.PI / 180);
GL.glVertex2d(x0, y0);
GL.glVertex2d(x1, y1);
x0 = x1;
y0 = y1;
}
GL.glEnd();
GL.glFlush();
}
protected override void InitGLContext()
{
GL.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
GL.glColor3f(0.0f, 0.0f, 0.0f);
GL.glPointSize(1.0f);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
GL.glMatrixMode(GL.GL_PROJECTION);
GL.glLoadIdentity();
}
}
}