|
We have made a graphics board on (OnPaint Event) . Our assignment is that
where we click in one of the rectangles of the board the event is triggered changing the color of the rectangle . We dont seem to understand . The coordinates of the rectangle are available through the mouse down event.
How is an interaction possible between the above specified events in order
to achieve the goal of color change event triggering .
|
|
|
|
|
Do the changes needed on your internal data to keep track of the color change, then you use the Invalidate method on the element showing the board. That will create a OnPaint event to redraw the element.
---
b { font-weight: normal; }
|
|
|
|
|
Sorry sir/madam
We are not at a professional level of development of right naow and didnt
understand your response while we do appreciate your concern and helping gesture . Please try to explain more more we really need some help .
|
|
|
|
|
What specifically was it in my response that you didn't understand?
---
b { font-weight: normal; }
|
|
|
|
|
here is one example
http://www.codeproject.com/csharp/chess.asp
check this too it is pretty good example
http://www.csharphelp.com/archives/archive246.html
|
|
|
|
|
Yes but that still is too abstruse considering Valil chess seems to be
broken into so many classes and interfaces . And we dont seem to understand prerogative behind the fragmented code . We just need simple answer to our question mouse click (color changing) event triggering on chess board that is part of our project . Any help will be really appreciated .
|
|
|
|
|
check this too it is pretty good example
http://www.csharphelp.com/archives/archive246.html
|
|
|
|
|
Thank you for response . You still have to understand that the aim of our
current project is to change the colour of the rectangle on the board when an event is triggered . (JUST THAT FOR NOW) . Thanks anyways.
|
|
|
|
|
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
|
|
|
|