|
|
I have created a simple animation in c# in which a ellipse moves.I want to create more than one objects of that class contaning OnPaint() method.I have done my best to do that .But nothing work for me.I want that the should be more than one object of my gamecontrol class drawn simultanously on the form.
The code is right here.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace mygame
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
protected System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private mygame.display gcontrol;
public int x,y;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//for double buffering
this.BackColor = Color.Black ;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 40;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
}
public class gamecontrol :Form1
{
public System.Windows.Forms.Timer Timer;
private bool widthgreater,heightgreater;
private System.Windows.Forms.Timer mytimer;
public gamecontrol()
{
}
public gamecontrol(int a,int b)
{
x=a;
y=b;
mytimer = this.timer1 ;
mytimer.Tick += new System.EventHandler (game_Tick);
this.Text = " Hello from constructro";
}
public void game_Tick(object sender, System.EventArgs e)
{
this.Refresh();
update();
this.Text = DateTime.Now.ToString();
}
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics ;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias ;
Pen pen = new Pen(Color.Blue,10);
g.FillEllipse(Brushes.BlueViolet,x,y,20,20);
base.OnPaint(pe);
}
public void update()
{
if(widthgreater)
{
x-=2;
if(x==0)
{
widthgreater=false;
}
}
else
{
x +=2;
if(x >= this.ClientRectangle.Width-20 )
{
widthgreater=true;
}
}
////////////////////////////////////////////////
///for y
if(heightgreater)
{
y -=2;
if(y<=0)
heightgreater=false;
}
else
{
y +=2;
if(y>=this.ClientRectangle.Height -20 )
heightgreater=true;
}
}
}
public class display
{
private mygame.gamecontrol ball;
public display()
{
ball = new gamecontrol(200,44);
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string [] abc)
{
Application.Run(new gamecontrol(33,44));
}
}
}
Any help .
Thanks in advance.
|
|
|
|
|
I want one form with many balls.
|
|
|
|
|
But what if i want to create 10 balls .i just want that how to create a class just like my ball of whom i can create objects just by one statement like just
ball ballobj = new ball();
Would you plz tell me how to create a class like that.
Thanks a million.
|
|
|
|
|
Forgive me if this is not in the SQL section, but its a C# query. I created my own sql connection, that uses a dataReader. I did not want to use a dataAdapter. Anyway, I am new to this, and have found no way of programming your own update command. Any ideas on this?
Background: I did not use the DataAdapter, as I needed to program my own, to display specific data depending on what button a user pressed. This works ok, but now I was wondering, how I can let the user change that data. This is why I need to program my own update. Sorry about this last part, was not sure how much information to include.
|
|
|
|
|
to allow the user to update a database you need to create a insert command. if you used the vs.net data Adapter it would do this for you provided that you didnt have any joins. a insert command looks kinda like. What the C# dataset has a Propertie i think called update ? but what it does is use the data adapters UpdateCommand / Insert Command / delete command depending on how the row was altered. i have never used datareader but check the sdk help and see if it has something simular. Hope this Helps.
<br />
--run this in query analizer to see its effect...alter if you like. Remeber Single Quotes<br />
<br />
use northwind<br />
select * from Categories<br />
<br />
insert into Categories<br />
(CategoryName,[Description])<br />
values<br />
('Beverages','Code Pimp - The only drink that a programmer needs')<br />
<br />
Select * from Categories<br />
so its
Insert into TableNameHere
--Optional Specify Columns
(Column1,Column2)
values
('value1','value2')
The Code Project Is Your Friend...
|
|
|
|
|
Ok, I am now trying to see if I can do this with a dataSet. But, as always, I have a problem. I have filled the dataset, so, I assume this contains all records from the table. However, I want to find out, how I only display 1 row, where an IDfield matches a value specified by a user.
|
|
|
|
|
With the data in a DataSet you can simply set the filter on the DefaultView for the table in the DataSet. If you are not using a strongly typed DataSet and you now there is only one table in your DataSet you can:
myDataSet.Tables[0].DefaultView.Filter = "IDField="+theValueTheyEntered.ToString();
That would give you one item in the DefaultView, and you would bind your grid or whatever you want to display the row to the:
myDataSet.Tables[0].DefaultView
I do not know if you have much experience in this area. If not, you might want to check out the DataForm Wizard in VS.NET (assuming you are using Visual Studio.NET). It will take a table and generate a dialog window with all the controls to load, update, delete and navigate through the table one record at a time. It also will generate a Master/Detail dialog window that allows you to select a master record in display a grid with the matching details all in the same window. Really nice for starters.
Rocky Moore <><
|
|
|
|
|
Thank you, I did not know about that wizard, just shows that you should look around more.
Just one question relating to the wizard. The table I am using, has an ID field, which is set to an autonumber. The problem though, is when you run the app, you cannot add a record. When you click add, it changes the record number at the bottom to show a new record is there, but you cannot select it. I was assuming this has something to do with the autonumber. If you know of anyway to get around this, please let me know.
Added: I forgot to ask, would this be a problem with the way I have created my table with regards to the autonumber?
|
|
|
|
|
you have to make sure that when you use the insert command to add a record that you specif the other columns and EXCLUDE the autonumber. if you try to add a value to the identity field you will get a sql error .i.e the value will not be added. so make sure you specify the columns in the insert statment i showed above and exclude that identity column.
Jesse M
The Code Project Is Your Friend...
|
|
|
|
|
Ok, not sure what I am missing, I might just have my XML file screwed up. I can load "ASP" as a node to my treeview file, however I would like to add a sub-element within the "ASP" node known as "1st Sniplet" and be able to extract the "code" element when "1st Sniplet" is selected. Thanks for any thoughts. First here is a snip from the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<categorie>ASP</categorie>
<sniplet>
<name>1st Sniplet</name>
<code>Response.Write "Hello World!"</code>
</sniplet>
</categories>
C# goodies....
TreeNode treeRoot = new TreeNode();
treeRoot.ImageIndex = 0;
treeRoot.Text = "Code Samples ";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.StartupPath + @"\XMLTree.xml");
try
{
XmlNodeList xmlnodeList, subNodeList;
XmlElement rootNode = xmlDoc.DocumentElement;
xmlnodeList = rootNode.SelectNodes("categorie");
elements = xmlnodeList.Count;
foreach(XmlNode x in xmlnodeList)
{
TreeNode treeNode = new TreeNode();
treeNode.ImageIndex = 1;
treeNode.Text = x.InnerText;
if(x.HasChildNodes)
{
subNodeList = x.SelectNodes("sniplet");
foreach(XmlNode sub in subNodeList)
{
TreeNode subtree = new TreeNode();
subtree.Text = sub.InnerText;
treeNode.Nodes.Add(subtree);
}
}
treeRoot.Nodes.Add(treeNode);
}
}
catch(XmlException e)
{
MessageBox.Show(e.Message + "\n\n" + e.StackTrace);
}
treeRoot.ExpandAll();
this.treeView1.Nodes.Add(treeRoot);
}
-Nick Parker
|
|
|
|
|
How about this instead?
<categories>
<category name="ASP">
<sniplet>
<name>1st Sniplet</name>
<code>Response.Write "Hello World!"</code>
</sniplet>
</category>
</categories>
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi
|
|
|
|
|
jdunlap wrote:
How about this instead?
Thanks, I switched it around and added some more code and it appears to be working fine now. Thanks again.
-Nick Parker
|
|
|
|
|
Im writing a game in C# with .net and DirectX 9. I'm using DirectInput for in-game input, but when it comes to entering text (chat, player names, etc) directX isnt really something you want to use (Im using action mapping btw so that would be a real pain). I'm wondering if anyone has, or has any advice, on creating a simple but effecient way of getting keyboard text in C# w/ .net. When programming under win32 I use to grab the WM_CHAR message and manually process that.
So far I've come up with making a onKeyDown event handler and writing a function to manually add each character, casted from EventArgs.KeyValue, to a string. The problem with this is its a pain to keep track if shift is pressed down, and Im having problems accepting symbols such as . , ; ' etc. It just seems there should be an easier way. I'd like to have case sensitivity, symbols, backspacing, etc without spending too much time writing a robust text grabber.
Any ideas? Just to clarfiry - Im not looking at like a windows control or other type of form component for inputting text into. This is a full screen game and I need to have text input and I cant use DirectX to do it.
Thanks for any help
Dave Ratti
|
|
|
|
|
is it possible to instantiate a .NET type within
Internet Explorer ?
For example I would like to write some component (using c#)
that would expose interface with a single method calculating
something, and I would like to use it within a web page
(interpreted in IExplore) just like i used to make use
of ActiveX components from within JScript code.
Is it possible ?
Thanx for any info
Michal Januszczyk
|
|
|
|
|
Yes! Definitely. Create a Class Library Project (WinForm) and then this DLL you can use from within WebPages.
.NET SDK/.NET Runtime must be available on the client machine running the .NET ActiveX Control.
No ClassIds are there. Instead you will say
NameSpaceName.ClassName as the Identifier.
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
|
One of the guys here posted a good question regarding enums. Basically questioning type safety with enums. Given the following:
using System;
namespace Poker
{
class PokerTable
{
[STAThread]
static void Main(string[] args)
{
PlayingCard fiveOfHearts = new PlayingCard( 4, 1 );
PlayingCard fiveOfHearts = new PlayingCard( (PlayingCard.Rank) 4, (PlayingCard.Suit) 1 );
fiveOfHearts.ViewHand();
PlayingCard rulesCard = new PlayingCard( (PlayingCard.Rank) 343, (PlayingCard.Suit) (-1) );
rulesCard.ViewHand();
PlayingCard emptyHand = new PlayingCard( 0, 0 );
emptyHand.ViewHand();
Console.In.ReadLine();
}
}
public class PlayingCard
{
public enum Suit
{
SPADES, HEARTS, DIAMONDS, CLUBS
}
public enum Rank
{
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING
}
private Rank rank;
private Suit suit;
public PlayingCard( Rank rank, Suit suit )
{
this.rank = rank;
this.suit = suit;
}
public void ViewHand()
{
Console.WriteLine(rank + " " + suit);
}
}
}
Why is PlayingCard( 0, 0 ) acceptable? Shouldn't this have a compile time error as does PlayingCard( 4, 1 )? I would think they should both fail!
Thanks!
ed
Regulation is the substitution of error for chance.
|
|
|
|
|
Enums in C# do dual purpose. They are used for the usual enum use, and they're also used for bit fields. When I'm dealing with bit fields, you often want to AND a value with the bit field and check if it's true.
Our initial rules meant that you had to write:
if ((myVar & MyEnumName.ColorRed) != (MyEnumName) 0)
which we thought was difficult to read. One alernative was to define a zero entry:
if ((myVar & MyEnumName.ColorRed) != MyEnumName.NoBitsSet)
which was also ugly.
We therefore decided to relax our rules a bit, and permit an implicit conversion from the literal zero to any enum type, which allows you to write:
if ((myVar & MyEnumName.ColorRed) != 0)
which is why PlayingCard(0, 0) works.
|
|
|
|
|
Could not the end-developper ideally control that using a compiler attribute?
my 2 cents
R/
|
|
|
|
|
It would be possible to allow the developer to control that behavior, but I think that allowing the user to change the language rules is a bad idea, as it complicates the language. Adding options often makes the user's lives harder, not easier.
|
|
|
|
|
I think it's a good idea. Those who don't like it can just not use it.
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi
|
|
|
|
|
I don't think so.
Isn't 0 valid for both enums, but 4 is invalid for the first?
Aren't the valid numbers for the Suit enum 0 - 3, while the valid numbers for the Rank enum 0 - 12 ?
That's why you shouldn't use "number" to access. Always use them in the true OO model, like Rank.JACK instead of a number.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
You'll always need to explicitly cast an int value to an enum unless the int value is zero.
|
|
|
|
|
Any enum is based on an integer type, which has a defualt value of 0, thus Enum as a ValueType it needs also some kind of default state, thus 0.
<a TITLE="See my user info" href=http:
|
|
|
|
|