Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,709 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,109 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



mouse events in windows application

 
Reply to this topicStart new topic

mouse events in windows application, designing backgammon game

c#newbiefc
post 14 Jul, 2008 - 07:04 AM
Post #1


New D.I.C Head

*
Joined: 11 Apr, 2008
Posts: 6

hello !
i want to move a picturebox1(containing stone) to a new location on gammon board.when label1 is pressed 2 numbers between 1 and 6 appear.player then moves mouse over picturebox1 which then highlights 2 buttons (from chipmove[24] array).ie if picturebox1 is located in chipmove[2]area and label gives nos 2 and 3 then buttons chipmove[4]and[5] will be highlighted.player then clicks on one of 2 highlighted buttons and the picturebox1 is relocated .this works fine but when player clicks label1 again for 2 further nos the previous two highlighted buttons are still active.how do i disable these buttons?
please help??

csharp
 public void Form1_Load(object sender, EventArgs e)
{

int x = 750; int y = 0; int no = 1;
for (int i = 0; i < 6; i++)
{
chipmove = new Button();
this.Controls.Add(chipmove[i]);

chipmove[i].Location = new Point(x, y);
chipmove[i].Size = new Size(30, 20);
chipmove[i].BackColor = Color.Cornsilk;

chipmove[i].Text = no.ToString();
chipmove[i].Enabled = false;
chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
x -= 60; no++;
}
x = 340;
for (int i = 6; i < 12; i++)
{
chipmove[i] = new Button();
this.Controls.Add(chipmove[i]);

chipmove[i].Location = new Point(x, y);
chipmove[i].Size = new Size(30, 20);
chipmove[i].BackColor = Color.Cornsilk;

chipmove[i].Text = no.ToString();
chipmove[i].Enabled = false;
chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
x -= 60; no++;
}
x=35;y=576;
for (int i = 12; i < 18; i++)
{
chipmove[i] = new Button();
this.Controls.Add(chipmove[i]);

chipmove[i].Location = new Point(x, y);
chipmove[i].Size = new Size(30, 20);
chipmove[i].BackColor = Color.Cornsilk;

chipmove[i].Text = no.ToString();
chipmove[i].Enabled = false;
chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
x += 60; no++;
}
x = 450;
for (int i = 18; i < 24; i++)
{
chipmove[i] = new Button();
this.Controls.Add(chipmove[i]);

chipmove[i].Location = new Point(x, y);
chipmove[i].Size = new Size(30, 20);
chipmove[i].BackColor = Color.Cornsilk;

chipmove[i].Text = no.ToString();
chipmove[i].Enabled = false;
chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
x += 60; no++;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.SandyBrown);
e.Graphics.FillEllipse(brush,0,0, 35, 35);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1, DragDropEffects.Move);
//panel1.Visible =true;panel2.Visible = true;
//pictureBox1.Location = new Point(690, 30);
string box1loc = pictureBox1.Location.X.ToString() + pictureBox1.Location.Y.ToString();
label1.Text = box1loc;
// int counterindex = Array.IndexOf(counterplace, box1loc);


chipmove[die1+counterindex].Enabled = true;
chipmove[die2+counterindex].Enabled = true;
chipmove[die1+counterindex ].BackColor = Color.Yellow;
chipmove[die2+counterindex ].BackColor = Color.Yellow;


}
private void label1_Click(object sender, EventArgs e)
{

die1 = dice.Next(6)+1;
die2 = dice.Next(6)+1;
label1.Text = die1.ToString() + ":" + die2.ToString();
label1.Enabled = false; pictureBox1.Enabled =true;
}

private void btnfinishmove_Click(object sender, EventArgs e)
{
chipmove.
chipmove[die1+counterindex].BackColor = Color.Cornsilk;
chipmove[die2+counterindex].BackColor = Color.Cornsilk;
chipmove[die1 + counterindex].Enabled =false;
chipmove[die2 + counterindex].Enabled =false;

label1.Enabled = true; pictureBox1.Enabled = false;
}


private void chip_MouseClick(object sender,MouseEventArgs e)
{
//chipmove.ToString(sender);
string chipsender;
chipsender =Array.IndexOf(chipmove,sender).ToString();

switch(chipsender )
{
case "0":
pictureBox1.Location = new Point(750, 30);
counterindex = 0;
break;
case "1":
pictureBox1.Location = new Point(690, 30);
counterindex = 1;
break;
case "2":
pictureBox1.Location = new Point(630, 30);
counterindex = 2;
break;
case "3":
pictureBox1.Location = new Point(570, 30);
counterindex = 3;
break;
case "4":
pictureBox1.Location = new Point(510, 30);
counterindex = 4;
break;
case "5":
pictureBox1.Location = new Point(450, 30);
counterindex = 5;
break;
case "6":
pictureBox1.Location = new Point(340, 30);
counterindex = 6;
break;
case "7":
pictureBox1.Location = new Point(280, 30);
counterindex = 7;
break;
case "8":
pictureBox1.Location = new Point(220, 30);
counterindex = 8;
break;
case "9":
pictureBox1.Location = new Point(160, 30);
counterindex = 9;
break;
case "10":
pictureBox1.Location = new Point(100, 30);
counterindex = 10;
break;
case "11":
pictureBox1.Location = new Point(40, 30);
counterindex = 11;
break;
default:
break;
}
pictureBox1.Enabled = false;
}


private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pictureBox1.DoDragDrop(pictureBox1, DragDropEffects.Move);

}

Mod edit: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 14 Jul, 2008 - 08:16 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,530



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Can't really tell from the given code. However, you could just inactivate all of them and go from there. Here's some code:

csharp

private void DisableAllButtons() {
foreach (Button b in chipmove) {
if (b.Enabled) { b.Enabled = false; }
}
}


Also, your chip_MouseClick scared me. Here's one that should do the same thing.
csharp

private void chip_MouseClick(object sender, MouseEventArgs e) {
Button chipButton = (Button)sender;
pictureBox1.Location = new Point(chipButton.Location.X, 30);
counterindex = int.Parse(chipButton.Text) - 1;
pictureBox1.Enabled = false;
}


Hope this helps.
User is online!Profile CardPM

Go to the top of the page

c#newbiefc
post 20 Jul, 2008 - 01:02 AM
Post #3


New D.I.C Head

*
Joined: 11 Apr, 2008
Posts: 6

hello again.thanks for responding to my question.i used your idea of resetting all the buttons in the array to enabled=false. which works.(i dont know why you cant just disable the selected buttons blink.gif )
may i ask you a further question?
i have setup 2 stones using a picturebox array which works ok.but...
when i place the stones on top of one another half of one of the stones is obscured.ie if i move stone1 to position of stone2 i tell stone1 to locate at y+30 say which looks ok on screen.if stone1 is placed above stone2 it looks fine but when stone2 is placed above stone1 only half of stone2 is visible. why is this?
CODE

    x=749;y=30;
            for (int s = 0; s < 2; s++)
            {
                darkstone[s] = new PictureBox();
                this.Controls.Add(darkstone[s]);
                darkstone[s].Location = new Point(x,y);
                darkstone[s].BackColor = Color.Transparent;
                x -= 60;
                darkstone[s].Paint+=new PaintEventHandler(stone_Paint  );              
                darkstone[s].MouseDown += new System.Windows.Forms.MouseEventHandler(stone_MouseDown);
                
            }

//stonetotal tells me how many stones are on each location

            stonetotal.AddRange(new int[]{1,1,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0});
        }

        private void stone_Paint(object sender, PaintEventArgs e)
        {
         SolidBrush brush = new SolidBrush(Color.SandyBrown);
          e.Graphics.FillEllipse(brush, 0, 0, 35, 35);
          brush.Dispose();
        }

   private void nodarkstones()
        {
            stotal = stonetotal[counterindex]; stotal += 1;
            stonetotal.RemoveAt(counterindex); stonetotal.Insert(counterindex, stotal);
              if (stotal == 1)
            {
                y = 30;
            }
            if (stotal ==2)
            {
                y = 65;
            }
                   }

  private void chip_MouseClick(object sender, MouseEventArgs e)
        {
            
            choice = (Button)sender;
          
            int chipsender;
            chipsender = Array.IndexOf(chipmove, sender);    //.ToString();
          
            if( die1+counterindex<24)
            {
                if (choice.Equals(chipmove[die1 + counterindex]))
                {
                    for (int i = 0; i < 24; i++)
                    {
                        chipmove[i].BackColor = Color.Cornsilk;
                        chipmove[i].Enabled = false;
                    }
                    die1used = 1;

                }
            }
            if(die2+counterindex<24)
            {
                if (choice.Equals(chipmove[die2 + counterindex]))
                {
                    for (int i = 0; i < 24; i++)
                    {
                        chipmove[i].BackColor = Color.Cornsilk;
                        chipmove[i].Enabled = false;
                    }
                    die2used = 1;

                }
            }
            #region chipsender
            switch (chipsender)
            {
                case 0:
                    darkstone[whichstone].Location=new Point(749, 30);                  
                    counterindex = 0;
                    break;
                case 1:                                
                    counterindex = 1;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(689,y);
                
                    break;
                case 2:
                    counterindex = 2;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(629,y);                  
                    
                    break;
                case 3:
                    counterindex = 3;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(569,y);                
                  
                    break;
                case 4:
                    counterindex = 4;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(509,y);                
                    
                    break;
                case 5:
                    counterindex = 5;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(449,y);                
                  
                    break;
                case 6:
                   counterindex = 6;
                   nodarkstones();
                    darkstone[whichstone].Location = new Point(339,y);                
                    
                    break;
                case 7:
                    counterindex = 7;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(279,y);
                
                    break;
                case 8:
                    counterindex = 8;
                    nodarkstones();
                    darkstone[whichstone].Location = new Point(219,y);                  
                  
                    break;
                case 9:
                   counterindex = 9;
                   nodarkstones();
                    darkstone[whichstone].Location = new Point(159,y);                
                  
                    break;
//not done all locations yet just testing out code on 1st 9 locations
                      

                if (die1used == 1 && die2used != 1 && die2 + counterindex < 24)
                {
                    chipmove[die2 + counterindex].Enabled = true;
                    chipmove[die2 + counterindex].BackColor = Color.Yellow;
                    //die2used = 1;
                }
                if (die1used != 1 && die2used == 1 && die1 + counterindex < 24)
                {
                    chipmove[die1 + counterindex].Enabled = true;
                    chipmove[die1 + counterindex].BackColor = Color.Yellow;
                    //die1used = 1;
                }
          
        foreach (PictureBox p in darkstone)
            {
                if (p == sender)
                {
                    psender = Array.IndexOf(darkstone, sender);
                    stoneloc =Convert.ToInt32( darkstone[psender].Location.X.ToString() + darkstone[psender].Location.Y.ToString());
                    counterindex = Array.IndexOf(counterplace, stoneloc);
                    whichstone = psender;

                }
         }
                for (int i = 0; i < 24; i++)
                {
                    chipmove[i].BackColor = Color.Cornsilk;
                    chipmove[i].Enabled = false;
                }    
            
          
      
}//chipmouseclick


again thanks for your help
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 03:06AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month