|
I tried to create you an example of drawing a board and changing that drawing based on a mouse event. I hope you can understand it OK, I have tried to make it as simple as possible just to show the concept.
So, I started with a class, which I named ChessSpace - it represents one sqare in the board. I gave it 3 particular pieces of functionality
1) It knows its color and size
2) It can determine when the mouse is over it, based upon mouse position
3) It knows how to draw itself
Here is the class
<pre><code>
public class ChessSpace
{
// the rectangle ocupied by this space
private Rectangle rect;
// the color to draw this space
private Color col;
// true if the mouse is down over this space
private bool isMouseDown;
// Constructor
public ChessSpace(Rectangle rectangle, Color color)
{
this.rect = rectangle;
this.col = color;
}
public void Draw(Graphics g)
{
// create a new brush to use for drawing this space
using(Brush brush = new SolidBrush(this.col))
{
// fill the rectangle with the color
g.FillRectangle(brush,this.rect);
}
// if the mouse is down in this space draw a red X
if(this.isMouseDown)
{
g.DrawLine(Pens.Red,rect.Left,rect.Top,rect.Right,rect.Bottom);
g.DrawLine(Pens.Red,rect.Right,rect.Top,rect.Left,rect.Bottom);
}
}
// Test a mouse position for intersection with this space
public void CheckMouseDown(Point point)
{
isMouseDown = this.rect.IntersectsWith(new Rectangle(point,new Size(1,1)));
}
// Clear the mouse down flag
public void ClearMouseDown()
{
isMouseDown = false;
}
}
</code></pre>
Next I created a simple nested loop to create me a 10x10 space chess board. This uses a class-variable of type ArrayList to store each instance of ChessSpace. It also uses a simple modulo calculation to alternate black and white spaces. This code was placed in the onload handler of my form:
<pre><code>
private ArrayList chessSpaces = new ArrayList();
protected override void OnLoad(EventArgs e)
{
// Get the width of my panel
int width = this.panel1.Width;
int wIncr = width/10;
// get the height of my panel
int height = this.panel1.Height;
int hIncr = height/10;
int count = 0;
for(int x=0;x<=width;x+=wIncr)
{
for(int y=0;y<=height;y+=hIncr)
{
// Get either black or white
Color color = (count%2)==0 ? Color.Black : Color.White;
// Add a new chess space to the collection
chessSpaces.Add(
new ChessSpace(
new Rectangle(new Point(x,y),new Size(wIncr,hIncr)),
color)
);
count++;
}
}
base.OnLoad (e);
}
</code></pre>
As you will note this uses a variable "panel1" which is a Panel I dropped onto my form (I set its width & height to 300 so it was square).
Lastly, I handled 3 events on the panel - Paint, MouseUp & MouseDown
<pre><code>
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
foreach(ChessSpace cs in this.chessSpaces)
{
cs.Draw(g);
}
}
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
foreach(ChessSpace cs in this.chessSpaces)
{
cs.CheckMouseDown(new Point(e.X,e.Y));
}
this.panel1.Invalidate();
}
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
foreach(ChessSpace cs in this.chessSpaces)
{
cs.ClearMouseDown();
}
this.panel1.Invalidate();
}
</code></pre>
The end result is a chess board, which draws a red X in the space where you press the mouse button.
HTH!
|
|
|
|
|
hi!
try calling the Invalidate() method on your mousedown event. This method will force your control to repaint.
hope that's what your looking for.
microsoc
|
|
|
|
|
Hi
i want to read the new mail from microsoft outlook.how can i do this using C#.
This is very urgent.
Thanks & Regards
Alok kumar
Alok
|
|
|
|
|
|
Hi
I have did a program such that , i will have two text box name and age in my form..and a button named save. so that when the values entered r in the text box..a xml file is created with those data..
.
Now I can able to create one set of values…. Now what I need is that when again some other value is entered in the textbox. It should also be written in the xml file..but without deleting the other. I mean it should append on the other..
For example: first I entered first as john and 22 for name and age ..it will added in the xml file..now again if I enter charu and 23 as name and age..
Now xml should have john and 22 as well as charu and 23…
And also one more thing is that if I again enter John. It should give a message that it has already been entered. No values should be repeated again. Please help me to do this…..
The coding which I did is as follows.
In side the button click
//save Button
private void button1_Click(object sender, System.EventArgs e)
{
DataSet dataSet = new DataSet();
//read the schema
dataSet.ReadXmlSchema("..\\..\\PlaygroMenu.xsd");
//create a new row
DataRow oValues;
oValues = dataSet.Tables[0].NewRow();
//enter the values
oValues[0] = txtName.Text;
oValues[1] = Convert.ToInt32(txtAage.Text);
//check for duplication
bool hasalready = false;
foreach(DataRow row in dataSet.Tables[0].Rows)
{
if(row.ItemArray[0].ToString().Equals(txtID.Text))
{
hasalready = true;
}
}
//
if(!hasalready)
{
//add the row to the dataset
dataSet.Tables[0].Rows.Add(oValues);
//write the data to a xml file
dataSet.WriteXml("..\\..\\resultdata.xml", XmlWriteMode.WriteSchema);
MessageBox.Show("Saved");
}
else
{
MessageBox.Show("Already Exists");
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
DataSet dataSet = new DataSet();
//read the schema
dataSet.ReadXmlSchema("..\\..\\Menu.xsd");
System.IO.FileInfo file = new System.IO.FileInfo("..\\..\\mydata.xml");
//check whethere there is result xml file...
if (file.Exists)
{
dataSet.ReadXml("..\\..\\resultdata.xml");
}
}
Please help me to do this..
Dhol
|
|
|
|
|
Hi All,
Wish all of you a very happy new year.....
I want to submit a form based on a condition as I am doing with ASP. I have a user login form and i m checking the username and password with the database (SQLServer 2000).
I want to do it in C# using ASP.net
User will fill the form,
username and password will be checked from the database.
If no record found, form will be submitted on login.aspx, and if record found, it will be submitted on account.aspx.
I m doing this by ASP code, but I want to use this in C# (Code behind) so that no body can see my code as it will be go will dll...
I also don't want to use Response.Redirect()...
function Validate()
{
document.login.submit();
}
Validate();
Is there any way ????
Regards,
Suman Singh
-- modified at 7:46 Thursday 5th January, 2006
|
|
|
|
|
Put runat="server" in the fields and use id to identify them:
<input type="hidden" id="CustNum" runat="server"/><br />
<input type=hidden id="mode" ruant="server"/>
Now you can declare them in code-behind:
protected HtmlInputHidden CustNum, mode;
And set the values:
CustNum.Value = "42";<br />
mode.Value = "Paranoid";
---
b { font-weight: normal; }
-- modified at 8:15 Thursday 5th January, 2006
|
|
|
|
|
and what will be the code to submit the form....
|
|
|
|
|
The same as you have now.
---
b { font-weight: normal; }
|
|
|
|
|
Does anyone has the experience on the vector graphics programming? Now,I wanna find out how to write codes to edit the vector graphics,such as the wmf format graphics file. Anyone could give me some ideas about it or codes on it? I really appreciate it for your help!
Richard
|
|
|
|
|
Hi!
Marc Clifton has published a nice article here on CP: Vector based clock[^]
Definitvely worth a read.
Regards,
mav
|
|
|
|
|
Hello, I wonder where this often appearing error comes from. I'd guess nearly every programmer knows the "...is being used by another process" problem.
My situation:
In my application I need to perform a couple of tasks on a single file like:
- writing some bytes into it
- copy it (I cannot use File.Move() at his point)
- and shortly after that: rename the copy with File.Move()
When executing next command after File.Copy() that also accesses the file (no matter if that is Move(), another Copy() command, or whatever) I get the classic exception with the errormessage "Cannot access file XYZ because it is being used by another process."
If I simply catch the exception - and do nothing within the catch block - the file operations all work SUCCESSFULLY.
So my question is, what is going on there?
Is it a possibly explanation that the execution of the application already continues although File.Copy() has not finished, and so the next file operation command accesses too early?
Is there a more elegant way to solve this other than an empty try...catch block?
I am sure there is... but what?
I'm curious about your opinions.
|
|
|
|
|
Are you closing the file handle after writing bytes to the file? You'll get the exception you mentioned if you don't.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi!
Are you sure the File.Copy() is the reason? If you're "writing some bytes into it" I think it's more likely that you didn't close your File or FileStream properly.
Some code would definitively help.
Regards,
mav
|
|
|
|
|
Yes the stream is closed, I just checked that again.
I'll post some code tomorrow when I get back to work.
|
|
|
|
|
Pls tell me how to put data from an IList to a Dataset.
Regards.
|
|
|
|
|
you are using
.NET 1.1 or 2.0?
regards
GV Ramana
|
|
|
|
|
I'm using .net 1.1. Pls tell me how to put data from an IList to a Dataset, in both .net 1.1 and .net 2.0. Thanks.
-- modified at 6:49 Thursday 5th January, 2006
|
|
|
|
|
Hi undead,
Your question is not proper.
However Where IList Interface is Implemented weather explicitly u r implementing? if yes, If Add method is implemented in your class, take for each loop and with IList.IndexOf(i)store in a DataSet.
Still you not get my answer, give detail question & requirement.
bye
regards
GV Ramana
|
|
|
|
|
Thanks with your help. I'm sorry that I didn't give u detail question & requirement.
I used Oject.Net to get data from a database to an IList (function PersistenceManager.getAllObjectsFromDb(typeof(Student));)
public class Student
{
int m_StudentId;
string m_Name;
string m_Email;
....
}
When I run, it will return an IList containing an array of Student object. After that I bind this IList to a datagrid, it's OK, but I don't know how to sort data in this IList (ex : m_Name DESC) so that I want to change it to a datatable (or dataset) to use the dataview to sort data.
How to do that???
|
|
|
|
|
Hi there!
After build my project it runs on my computer without problems. After installing that on my laptop it doesnt work.
The application uses 2 dlls coded in c++. when the application starts, it cant access the dlls.
http://msdn2.microsoft.com/en-us/library/ms235317.aspx --> desribes a workaround
I have to set a PATH environment variable in System-Advanced-Environment Variable under Windows XP.
But I have no idea how to make it with an *.bat file.
Is it possible to place the path variable in the registry?
|
|
|
|
|
In your installation project, open the Registry view and add your custom values as children to the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" key.
NOTE: As always, you assume risk when changing the registry. Doing so can cause your system to become unstable or unusable. Please, do so at you own risk.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
I have a problem,
i build an aspx page a normal webform, on this i use a control from somewhere deep in the internet, it looks like a textbox, but has a small calendar attached, doesnt matter, works fine. The Problem is, that textbox has no 'TextChanged' Event, but i really need this one.
So my question is, could anybody help me, how to add this event to this custom TextBox, means how to add an event which is fired when the text in this box is changed!
Or in general how to create new events for existing controls/classes or whatever, damn .NET!
Hope someone is genius on that and can help me out soon,
looking forward not to kill myself
regards
Balu
|
|
|
|
|
|
Thanks for the fast reply,
i cant do this in Javascript cause i have to interact with another vb script editor and i cant use scripts on the html side, i need to pass my data from the code behind page, i have to use the event mentioned in the first post, to make an Sql call and then transfering recieved data to a vb script on whether the html side or in this special vb script editor!
I will see if the articel you posted could help me out!
If there are any other articles, how-to´s what ever on this topic, please post it!
Thanks again,
regards
Balu
|
|
|
|