[RESOLVED] Graphic programing for beginner help
I am trying to write an application that locate the box based on user input. For example, I have ten boxes (naming 1 to 10 in order) and user input 2 Once user hit button "Find" then the box number 2 will turn GREEN or what color we decide to. I know this is a simple application but if anyone can point me to where I can read or basic tutorial like this one, I would appreciate it. I have a background in VC++.
Thanks,
NODO
[440 byte] By [
nodo] at [2007-11-20 10:53:18]

# 1 Re: [RESOLVED] Graphic programing for beginner help
Hi and welcome to dev-archive !
You may use a private field int lastBoxUsed in which you set the TabIndex of each Textbox in the TextBoxes TextChanged event like for example
TextBox1_TextChanged(object sender EventArgs e){
this.lastBoxUsed = TextBox1.TabIndex
}
This way everytime changing a textbox sets its own index as the last used Index into this field. Then in the Findbuttons Click Delegate you can run trough your controls collection checking if the control is of type textBox and if so checking the TabIndex. The result of this you may change the BackColor and or Forecolor of that Box whatever you want
Have Fun:wave:
# 2 Re: [RESOLVED] Graphic programing for beginner help
First of all; what do you mean by boxes? Is it textboxes or graphic rectangles?
dahwan at 2007-11-9 11:37:03 >

# 3 Re: [RESOLVED] Graphic programing for beginner help
First of all; what do you mean by boxes? Is this textboxes or graphic rectangles?Good question, but in that case I think he speaks about TextBoxes as there is a big difference between rectangles or 'Boxes' simple by the language expression.
If he means rectangles and ownerdrawing instead then I suggest him to read my article about ' Creating a Dockable Panel-Controlmanager Using C#, Part 1' where a lot about ownerdrawing is explained.
BTW there is just a voting about best articles of the month August running and this article is one of them, so i think its worth to read this article, especially as its adressed to 'beginners'. ( programmers with a basic knowledge in C#)
# 4 Re: [RESOLVED] Graphic programing for beginner help
I have a similar question to JonnyPoet.
This program is "aaallmost" what nodo is asking for. When i hit enter, the numbered rectangle doesnt turn green. How ever; if i write the number (f. inst. "2"), click enter and then click the "alt" button, the numbered rectangle turns green! Why does it turn green when i click alt? I was thinking it has something to do with the focus, but i donno.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace namespace_Program
{
#region Main
class class_Main
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new GraphicsProgram());
}
}
#endregion
class GraphicsProgram : Form
{
private Brush Box1_Color = Brushes.Black;
private Brush Box1
{
get { return Box1_Color; }
set { value = Box1_Color; }
}
private Brush Box2_Color = Brushes.Black;
private Brush Box2
{
get { return Box2_Color; }
set { value = Box2_Color; }
}
private Brush Box3_Color = Brushes.Black;
private Brush Box3
{
get { return Box3_Color; }
set { value = Box3_Color; }
}
private Brush Box4_Color = Brushes.Black;
private Brush Box4
{
get { return Box4_Color; }
set { value = Box4_Color; }
}
private Brush Box5_Color = Brushes.Black;
private Brush Box5
{
get { return Box5_Color; }
set { value = Box5_Color; }
}
private Panel TopPanel = new Panel();
private Panel BottomPanel = new Panel();
private TextBox MainTextBox = new TextBox();
private Label InfoLabel = new Label();
public GraphicsProgram()
{
this.Text = "Graphics rectangle program";
this.ClientSize = new Size(310, 100);
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
TopPanel.Parent = this;
TopPanel.Dock = DockStyle.Top;
TopPanel.Height = 30;
BottomPanel.Parent = this;
BottomPanel.Dock = DockStyle.Bottom;
BottomPanel.Height = 70;
BottomPanel.Paint += new PaintEventHandler(TopPanel_Paint);
MainTextBox.Parent = TopPanel;
MainTextBox.Location = new Point(10, 10);
MainTextBox.Multiline = false;
MainTextBox.KeyDown += new KeyEventHandler(MainTextBox_KeyDown);
InfoLabel.Parent = TopPanel;
InfoLabel.Location = new Point(113, 14);
InfoLabel.Text = "Input a number from 1 to 5 and hit enter.";
InfoLabel.AutoSize = true;
}
void AllBrushesBlack()
{
Box1_Color = Brushes.Black;
Box2_Color = Brushes.Black;
Box3_Color = Brushes.Black;
Box4_Color = Brushes.Black;
Box5_Color = Brushes.Black;
}
void TopPanel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Box1, 10, 10, 50, 50);
e.Graphics.FillRectangle(Box2, 70, 10, 50, 50);
e.Graphics.FillRectangle(Box3, 130, 10, 50, 50);
e.Graphics.FillRectangle(Box4, 190, 10, 50, 50);
e.Graphics.FillRectangle(Box5, 250, 10, 50, 50);
}
void MainTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (MainTextBox.Text == "1")
{
AllBrushesBlack();
Box1_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "2")
{
AllBrushesBlack();
Box2_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "3")
{
AllBrushesBlack();
Box3_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "4")
{
AllBrushesBlack();
Box4_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "5")
{
AllBrushesBlack();
Box5_Color = Brushes.Lime;
this.Invalidate();
}
}
}
}
}
If you figure out why this app doesnt work, nodo can use it as an example of what he was asking for.
dahwan at 2007-11-9 11:39:07 >

# 5 Re: [RESOLVED] Graphic programing for beginner help
Hi Dahwan !
I'm in work now in noon-break, which is just finished, I'll have a look on it in the evening. But why using brushes for textboxes?
# 6 Re: [RESOLVED] Graphic programing for beginner help
Its System.Drawing.Graphics rectangles :)
dahwan at 2007-11-9 11:41:06 >

# 7 Re: [RESOLVED] Graphic programing for beginner help
This is very easy!
Well, You just need to override or subscribe OnPaint() of the form, and do paint work in it.
There are some basic book about GDI+ written in C#. <<Graphics programming with GDI+>> is one of them.
# 8 Re: [RESOLVED] Graphic programing for beginner help
Its System.Drawing.Graphics rectangles :)
Some small changes to your code and it works
using System;
using System.Drawing;
using System.Windows.Forms;
namespace namespace_Program
{
class GraphicsProgram: Form
{
private Brush Box1_Color = Brushes.Black;
private Brush Box1
{
get { return Box1_Color; }
set { value = Box1_Color; }
}
private Brush Box2_Color = Brushes.Black;
private Brush Box2
{
get { return Box2_Color; }
set { value = Box2_Color; }
}
private Brush Box3_Color = Brushes.Black;
private Brush Box3
{
get { return Box3_Color; }
set { value = Box3_Color; }
}
private Brush Box4_Color = Brushes.Black;
private Brush Box4
{
get { return Box4_Color; }
set { value = Box4_Color; }
}
private Brush Box5_Color = Brushes.Black;
private Brush Box5
{
get { return Box5_Color; }
set { value = Box5_Color; }
}
private Panel TopPanel = new Panel();
private TextBox MainTextBox = new TextBox();
private Label InfoLabel = new Label();
public GraphicsProgram()
{
this.Text = "Graphics rectangle program";
this.ClientSize = new Size(310, 130);
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
TopPanel.Parent = this;
TopPanel.Dock = DockStyle.Top;
TopPanel.Height = 30;
//BottomPanel.Parent = this;
//BottomPanel.Dock = DockStyle.Bottom;
//BottomPanel.Height = 70;
MainTextBox.Parent = TopPanel;
MainTextBox.Location = new Point(10, 10);
MainTextBox.Multiline = false;
MainTextBox.KeyDown += new KeyEventHandler(MainTextBox_KeyDown);
InfoLabel.Parent = TopPanel;
InfoLabel.Location = new Point(113, 14);
InfoLabel.Text = "Input a number from 1 to 5 and hit enter.";
InfoLabel.AutoSize = true;
}
void AllBrushesBlack()
{
Box1_Color = Brushes.Black;
Box2_Color = Brushes.Black;
Box3_Color = Brushes.Black;
Box4_Color = Brushes.Black;
Box5_Color = Brushes.Black;
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.FillRectangle(Box1, 10, 50, 50, 50);
e.Graphics.FillRectangle(Box2, 70, 50, 50, 50);
e.Graphics.FillRectangle(Box3, 130, 50, 50, 50);
e.Graphics.FillRectangle(Box4, 190, 50, 50, 50);
e.Graphics.FillRectangle(Box5, 250, 50, 50, 50);
base.OnPaint(e);
}
void MainTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (MainTextBox.Text == "1")
{
AllBrushesBlack();
Box1_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "2")
{
AllBrushesBlack();
Box2_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "3")
{
AllBrushesBlack();
Box3_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "4")
{
AllBrushesBlack();
Box4_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "5")
{
AllBrushesBlack();
Box5_Color = Brushes.Lime;
this.Invalidate();
}
}
}
}
#region Main
class class_Main
{
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new GraphicsProgram());
}
}
#endregion
}
basically I have removed the bottompanel and override OnPaint of the Form. Not having BottomPanel needs to adjust Verical Position of rectangles. But this isn't the question I think he jaust wanting the opposite. he has textboxes and wants to find that one where the last input occured and this works without graphics easily as I have described in post #2
-- edited --
typos in code Ucase instead of Lcase handled
# 9 Re: [RESOLVED] Graphic programing for beginner help
Hi None !
As dont know your level of experence I'll keep it easy.
public partial class Form1 : Form
{
private int _lastInputBox = 0;
public Form1() {
InitializeComponent();
}
// you need to add a TextChanged Delegate to every TextBox
private void textBox1_TextChanged(object sender, EventArgs e) {
_lastInputBox = 1;
}
private void textBox2_TextChanged(object sender, EventArgs e) {
_lastInputBox = 2;
}
private void textBox3_TextChanged(object sender, EventArgs e) {
_lastInputBox = 3;
}
private void textBox4_TextChanged(object sender, EventArgs e) {
_lastInputBox = 4;
}
private void textBox10_TextChanged(object sender, EventArgs e) {
_lastInputBox = 10;
}
private void textBox8_TextChanged(object sender, EventArgs e) {
_lastInputBox = 8;
}
private void textBox7_TextChanged(object sender, EventArgs e) {
_lastInputBox = 7;
}
private void textBox6_TextChanged(object sender, EventArgs e) {
_lastInputBox = 6;
}
private void textBox5_TextChanged(object sender, EventArgs e) {
_lastInputBox = 5;
}
private void textBox9_TextChanged(object sender, EventArgs e) {
_lastInputBox = 9;
}
private void btFind_Click(object sender, EventArgs e) {
UncolorAll();
switch (_lastInputBox) {
Case 1:
textBox1.BackColor = Color.Red;
break;
Case 2:
textBox2.BackColor = Color.Red;
break;
Case 3:
textBox3.BackColor = Color.Red;
break;
Case 4:
textBox4.BackColor = Color.Red;
break;
Case 5:
textBox5.BackColor = Color.Red;
break;
Case 6:
textBox6.BackColor = Color.Red;
break;
Case 7:
textBox7.BackColor = Color.Red;
break;
Case 8:
textBox8.BackColor = Color.Red;
break;
Case 9:
textBox9.BackColor = Color.Red;
break;
Case 10:
textBox10.BackColor = Color.Red;
break;
}
}
private void UncolorAll() {
foreach (Control c in this.Controls) {
Type tp = c.GetType();
if (tp.Name == "TextBox") {
((TextBox)c).BackColor = SystemColors.Window;
}
}
}
}
So you will have to know this isn't the best code. If more experienced you will be able to make this code much more common useable.
You dont need to know the names of all textboxes you can find them for example by their TabIndex This case you can have one and the same delegate for all Textbxes. But I'm frightened you are - asking this question - a very - very beginner, so maybe this code is hard enough to understand. Let me know if you need it in a more advanced way.
# 10 Re: [RESOLVED] Graphic programing for beginner help
Hi Again !
Maybe for all others here is a more advanced version to solve this problem
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsTestApplication
{
public partial class Form1 : Form
{
private int _lastInputBox = 0;
public Form1() {
InitializeComponent();
AddDelegates();
}
private void AddDelegates() {
foreach (Control c in this.Controls) {
Type tp = c.GetType();
// is the control of type TextBox ?
if (tp.Name == "TextBox") {
// Cast the control to a TextBox and add the TextChanged Delegate
// One and the same Delegate for all textboxes
((TextBox)c).TextChanged += new EventHandler(textBox_TextChanged);
}
}
}
void textBox_TextChanged(object sender, EventArgs e) {
// in this delegate when called we set the TabIndex of the calling Textbox
// as the last one who was changed
_lastInputBox = ((TextBox)sender).TabIndex;
}
private void btFind_Click(object sender, EventArgs e) {
UncolorAll();
foreach (Control c in this.Controls) {
Type tp = c.GetType();
// again only for textBoxes
if (tp.Name == "TextBox") {
// if the TabIndex is identic with the last used one
if (((TextBox)c).TabIndex == _lastInputBox) {
// color Background to red
((TextBox)c).BackColor = Color.Red;
}
}
}
}
private void UncolorAll() {
foreach (Control c in this.Controls) {
Type tp = c.GetType();
if (tp.Name == "TextBox") {
// all textboxes are reset to the Defaut Background Color for Textboxes
((TextBox)c).BackColor = SystemColors.Window;
}
}
}
}
}
# 11 Re: [RESOLVED] Graphic programing for beginner help
Hi JonnyPoet,
Thanks for your response and the code. I have an experience working in C++ before but never try C#. At this time, I like to try it so I can learn a new language. I'll look to it later this evening.
Also thanks to dahwan. I didn't expect this quick response but people in this forum are awsome.
To answer your question about the box, I meant it's just a square or rectangle, not a text box. The text box is for user input the location to find.
I appreciate your help.
-nodo
nodo at 2007-11-9 11:46:13 >

# 12 Re: [RESOLVED] Graphic programing for beginner help
Hi JonnyPoet,
Thanks for your response and the code. I have an experience working in C++ before but never try C#. At this time, I like to try it so I can learn a new language. I'll look to it later this evening.
Also thanks to dahwan. I didn't expect this quick response but people in this forum are awsome.
To answer your question about the box, I meant it's just a square or rectangle, not a text box. The text box is for user input the location to find.
I appreciate your help.
-nodoThen use the code posted in #9 by me Its basically Dahwans code, but corrected from me to get it working. as dahwan asked me to do so. Sorry I didn't get you that you are talking about rectangles when saying Boxes.
# 13 Re: [RESOLVED] Graphic programing for beginner help
Thanks a lot for the correction :D
I'll look more into it later!
Dahwan
dahwan at 2007-11-9 11:48:18 >

# 14 Re: [RESOLVED] Graphic programing for beginner help
JonnyPoet, have you made any tutorials? because i can see you'd be great at that
dahwan at 2007-11-9 11:49:14 >

# 15 Re: [RESOLVED] Graphic programing for beginner help
JonnyPoet, have you made any tutorials? because i can see you'd be great at thatYes I did this
My latest article here in dev-archive
Creating a Dockable Panel-Controlmanager Using C#, Part 1
It can be found here http://www.dev-archive.com/csharp/csha...cle.php/c14179/
I thought you would like it. You may study it and if you find it useful you can also vote for it, because this article was selected for one of the six best in August ( You see the yellow post in the C# Mainpage's Top ? )
I'm in the moment working on the second part of it which will be ready soon.
# 16 Re: [RESOLVED] Graphic programing for beginner help
Jonny,
I maybe laugh at me if I asked you the debug error but this is a first time I run C# so please excuse myself.
Below is the error message if I run debug (F5):
Error 1 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 6 2 WindowsApplication1
Error 2 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 8 11 WindowsApplication1
Error 3 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 9 11 WindowsApplication1
Error 4 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 14 11 WindowsApplication1
Error 5 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 15 11 WindowsApplication1
Error 6 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 20 11 WindowsApplication1
Error 7 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 21 11 WindowsApplication1
Error 8 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 26 11 WindowsApplication1
Error 9 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 27 11 WindowsApplication1
Error 10 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 32 11 WindowsApplication1
Error 11 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 33 11 WindowsApplication1
Error 12 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 38 11 WindowsApplication1
Error 13 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 38 32 WindowsApplication1
Error 14 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 39 11 WindowsApplication1
Error 15 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 39 37 WindowsApplication1
Error 16 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 40 11 WindowsApplication1
Error 17 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 40 33 WindowsApplication1
Error 18 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 44 26 WindowsApplication1
Error 19 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 54 31 WindowsApplication1
Error 20 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 56 31 WindowsApplication1
Error 21 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 58 29 WindowsApplication1
Error 22 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 62 3 WindowsApplication1
Error 23 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 70 22 WindowsApplication1
Error 24 Type or namespace definition, or end-of-file expected C:\Documents and Settings\nodo\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 115 2 WindowsApplication1
Please advise.
Thanks,
nodo
nodo at 2007-11-9 11:51:22 >

# 17 Re: [RESOLVED] Graphic programing for beginner help
Jonny,
I maybe laugh at me if I asked you the debug error but this is a first time I run C# so please excuse myself.
Please show your code as in this debug its referencing to lines in your code so I need to see the code or better zip the project and post the zip. I'll have a look on it. Sometimes you get lots of errors by simple a few errors. Dont be in sorrows show code
# 18 Re: [RESOLVED] Graphic programing for beginner help
Here is the code that I copy from #9 and paste to my form.cs file.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace namespace_Program
{
Class GraphicsProgram: Form
{
private Brush Box1_Color = Brushes.Black;
private Brush Box1
{
get { return Box1_Color; }
set { value = Box1_Color; }
}
private Brush Box2_Color = Brushes.Black;
private Brush Box2
{
get { return Box2_Color; }
set { value = Box2_Color; }
}
private Brush Box3_Color = Brushes.Black;
private Brush Box3
{
get { return Box3_Color; }
set { value = Box3_Color; }
}
private Brush Box4_Color = Brushes.Black;
private Brush Box4
{
get { return Box4_Color; }
set { value = Box4_Color; }
}
private Brush Box5_Color = Brushes.Black;
private Brush Box5
{
get { return Box5_Color; }
set { value = Box5_Color; }
}
private Panel TopPanel = new Panel();
private TextBox MainTextBox = new TextBox();
private Label InfoLabel = new Label();
Public GraphicsProgram()
{
this.Text = "Graphics rectangle program";
this.ClientSize = new Size(310, 130);
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
TopPanel.Parent = this;
TopPanel.Dock = DockStyle.Top;
TopPanel.Height = 30;
//BottomPanel.Parent = this;
//BottomPanel.Dock = DockStyle.Bottom;
//BottomPanel.Height = 70;
MainTextBox.Parent = TopPanel;
MainTextBox.Location = new Point(10, 10);
MainTextBox.Multiline = false;
MainTextBox.KeyDown += new KeyEventHandler(MainTextBox_KeyDown);
InfoLabel.Parent = TopPanel;
InfoLabel.Location = new Point(113, 14);
InfoLabel.Text = "Input a number from 1 to 5 and hit enter.";
InfoLabel.AutoSize = true;
}
void AllBrushesBlack()
{
Box1_Color = Brushes.Black;
Box2_Color = Brushes.Black;
Box3_Color = Brushes.Black;
Box4_Color = Brushes.Black;
Box5_Color = Brushes.Black;
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.FillRectangle(Box1, 10, 50, 50, 50);
e.Graphics.FillRectangle(Box2, 70, 50, 50, 50);
e.Graphics.FillRectangle(Box3, 130, 50, 50, 50);
e.Graphics.FillRectangle(Box4, 190, 50, 50, 50);
e.Graphics.FillRectangle(Box5, 250, 50, 50, 50);
base.OnPaint(e);
}
void MainTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (MainTextBox.Text == "1")
{
AllBrushesBlack();
Box1_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "2")
{
AllBrushesBlack();
Box2_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "3")
{
AllBrushesBlack();
Box3_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "4")
{
AllBrushesBlack();
Box4_Color = Brushes.Lime;
this.Invalidate();
}
if (MainTextBox.Text == "5")
{
AllBrushesBlack();
Box5_Color = Brushes.Lime;
this.Invalidate();
}
}
}
}
#region Main
Class class_Main
{
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new GraphicsProgram());
}
}
#endregion
}
nodo at 2007-11-9 11:53:24 >

# 19 Re: [RESOLVED] Graphic programing for beginner help
Here is the code that I copy from #9 and paste to my form.cs file.
this looks to me as if this code is in cortrary to some other of your code. So please zip full project so I can see what has happened.
You have to know for this that Dahwan doesn't use the standard way of creating a Prgram using the Visual Studio IDE. So you see all on the same page which may be confusing with your own code.
So please zip your project ( only .cs and project files no binary ones
and add it here. I'll have a look on it
# 20 Re: [RESOLVED] Graphic programing for beginner help
Please find an attachment for zip files.
Thanks Jonny.
-nodo
nodo at 2007-11-9 11:55:26 >

# 21 Re: [RESOLVED] Graphic programing for beginner help
Here is the repaired one. Some generals for posting projects.
When I said remove the binary I meant all that what you find in bin and debug folder but not in the Properties !! Never delete settings or resources files. Sorry I didn't explicitly mention that. But thats for the future to know.
b) I said zip, I havn't said rar files. I first had to get a free version of rar decompressor to read your file. zip could be read in any XP OS without any additional program. OK
Regarding errors
C# is case sensitive so you cannot use 'Class' instead of 'class'.
or 'Public' instead of 'public' as you did.
But as I see I had overseen that this errors are from the corrected copy I posted from Dawahns code and not in Dawahns original code..I dont know how this could happen, because I tested it before posting so it may have changed somehow during my posting procedure. Oh yes, Sorry. I just found out
For explanation. Posting C# code directly from my IDE makes lots of troubles with this editor. As in that case I have an empty line after every line and need to repair all the posted code. This way I have found out that copying the text to my old VB6 IDE and from there copying to here in the editor solves that problem, as VB code was everytime Ok to post here. But as I see now. VB may change some code automatically from small letters to UCase letters like Public is a keyword there. Sorry I think I have to find another solution for getting my code from C# IDE to this editor. But back to your code
When you have created Form1 as the classname and in the example the class is named GraphicsProgram then you may decide which one you want to use and then change the other so all is one name in the designer as well as in the code. The same for namesapces.
The last error was what I told you, that Dawahns code is not standard
so he has the Threadstart in the same page as the Form1 code which is very unusual in C# 2005 So I deleted this as you have your threadstarter in your program.cs as it should be.
I have corrected all this and added the full zip to you
# 22 Re: [RESOLVED] Graphic programing for beginner help
Hi Jonny,
I am sorry to cause any convienence for you. I appreciate your help and your patience alot. It is a well explaination. It works now. I'll modify the code to exactly what I want later tonite because I am at work right now. Programming is nothing related to my work but I just want to learn. Again, you're a good helper and I appreciate it.
Thanks,
nodo
nodo at 2007-11-9 11:57:21 >

# 23 Re: [RESOLVED] Graphic programing for beginner help
Hi Jonny,
I am sorry to cause any convienence for you.
Not really a problem. I only wanted you to know for the future here in forum we use zip files.
I appreciate your help and your patience alot. It is a well explaination. It works now.
You are welcome.
Programming is nothing related to my work but I just want to learn.
Thats totally ok, if people want to learn things they are very welcome here. If you have any questions you can everytime come back and ask.
;)
Jonny:wave:
# 24 Re: [RESOLVED] Graphic programing for beginner help
The question has been answered. Thanks for all your help (Johnny & Dahwan).
nodo at 2007-11-9 11:59:22 >

# 25 Re: [RESOLVED] Graphic programing for beginner help
The question has been answered. Thanks for all your help (Johnny & Dahwan).Another forum rule is, if a post is answered and finished use thread tools and mark it as resolved, this way forumreaders know, they dont need to read this post anymore.
# 26 Re: [RESOLVED] Graphic programing for beginner help
Hi again !
As you have given 5 stars for this post ( which is much to much in my eyes ) I have done you a code how I would do te same code, so its a bit more advanced and because of this also shorter. Its added in the zip. I'll explain it a bit. I'm using standard designer and in the designer we have a label and a TextBox called txtBoxNo where you put in your textoxnumber you want to color
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myProject {
partial class Form1: Form {
private int _horBoxDist = 60; //the horizontal distance between Rectangles Left Corner
private Size _boxSize = new Size(50,50);
private int _horLocation = 10; // hor Location of the first Rectangle
private Brush _brush = null; // no startColor
private int _actualIndex = 0; // no Rectangle selected
public Form1() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
// we start drawing Boxes on the left position which we have set in _horLocation
int pos = _horLocation;
// now we loop from 1 to 5
for(int i = 1; i <= 5 ; i++){
// if the actual choosen rectangle is the same we are just drawing
if( i == _actualIndex ){
// we use blue Brush to paint
_brush = Brushes.Blue;
}else{
// in every other case we use a Black brush
_brush = Brushes.Black;
}
// now we draw a rectangel of the given Sze
e.Graphics.FillRectangle(_brush ,pos,50,_boxSize.Width, _boxSize.Height);
// now we change the x-position value
pos += _horBoxDist;
}
// when we have drawn all rectangles we paint using the base method.
base.OnPaint(e);
}
private void txtBoxNo_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter){
try {
// we try to convert the inout to an integer ( 32 bit )
_actualIndex = Convert.ToInt32(txtBoxNo.Text);
// if we had no error we now can check the value if its in the range
if ( _actualIndex > 0 && _actualIndex <= 5 ){
// if all is Ok we have the correct value in the _actualIndex Field and we invalidate the Form to get it redrawn
// so this calls OnPaint() !!!
this.Invalidate();
} else{
// errormessage wrong valuesize
MessageBox.Show("Input should be from 1 to 5");
}
} catch {
// as we had an error -no input tranfromable to integer
MessageBox.Show("Invalid Input");
}
}
}
}
}
# 27 Re: [RESOLVED] Graphic programing for beginner help
Hi Jonny,
Since you post a new advance version, I just add the menu strip. How can I call "exit" function and display about info into a new form? I know function call messageBox.show will do but I like to display another form instead of message box. My project zip files is attached.
Thanks,
nodo
nodo at 2007-11-9 12:02:34 >

# 28 Re: [RESOLVED] Graphic programing for beginner help
Hi Jonny,
Since you post a new advance version, I just add the menu strip. How can I call "exit" function ...When you doubleclick on your 'exit' menuebutton at designtime then the designer creates a delagate for you and adds it to the form
This looks like the following
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
}
Now simple insert here the forms close command
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
this.Close();
}Clicking the button then closes the form.
...and display about info into a new form? Simple create a 'AboutMeForm' class derived from a Form. This is done automatically in the IDE opening the project menue and adding a Form to the project naming it AboutMeForm
public class AboutMeForm :Form {
...
}
there you may add all what you want, picture, labels with text...
The About menuebutton creates the following event
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
// Fill it by this
AboutMeForm aboutForm = new AboutMeForm();
aboutForm.ShowDialog();
}
This will bring up the AboutForm on the screen.
# 29 Re: [RESOLVED] Graphic programing for beginner help
Cool. Menu works now. What's next now? I just create it and learn a new function. :)
Thanks,
nodo
nodo at 2007-11-9 12:04:36 >

# 30 Re: [RESOLVED] Graphic programing for beginner help
Cool. Menu works now. What's next now? I just create it and learn a new function. :)...
Simple go on with what you are wanting to create with your code. Or if you want to follow a bigger lecture look to my article which I wrote in August and the next article on the same I'm already working on. You would learn about creating ownerdrawn controls, and lots of other stuff. It can be done if you know thr basics of C# and I tried to explain it carefully with lots of pictures and drawings. ( Look at the bottom of myy post there is a link to the article. The source is always added.:thumb: ;)
# 31 Re: [RESOLVED] Graphic programing for beginner help
I'll take a look at your tutorial and learn from there. I appreciate your help alot.
-nodo
nodo at 2007-11-9 12:06:28 >

# 32 Re: [RESOLVED] Graphic programing for beginner help
I'll take a look at your tutorial and learn from there. I appreciate your help alot.
-nodoIf you need specific help post a comment and send an email there so I can answer it.