C# Programming problem :(

NooBieless

Well-Known Member
Joined
Jul 28, 2009
Messages
112
Reaction score
0
Hey all

Im in a bit of a problem... can anyone help me with issue ive got thats if u know C#

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace mySecondApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button5.Left += 5;
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            button5.Left -= 5;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button5.Top += 5;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button5.Top -= 5;
        }
    }
}
 
What's the problem? Usually if you ask for help, you tell us what's going on. What is the program supposed to be doing that it's not. Would love to help, but information black hole!
 
What's the problem? Usually if you ask for help, you tell us what's going on. What is the program supposed to be doing that it's not. Would love to help, but information black hole!

Agreed ... Feels like i'm entering the twilight zone and struggling to understand just what it is i'm looking for ;-)
 
I'm scratching my head on this one. 4 buttons that move a 5th button up down left and right. Tough one this:P I compiled it and it runs fine.... I can move a button around, yay.

So we have established that there is nothing functionally incorrect with the code you pasted above. I have no idea what your problem is, but a few pointers non the less.

1. Name your controls (e.g. button1 should perhaps be "RightButton").
2. Perhaps seperate your code into regions (look at the #region tag). Possible regions here might be "#region Constructor Methods", "#region Event Handlers" (all your button_click handlers would go in Event Handlers), "#region Methods"
3. If you are just starting out, perhaps look at WPF. If you are working for a company that still has a lot of old winforms stuff, sure it's important. If you are still studying (looks like it), then spend your time on WPF. The world is moving on from winforms and you don't want to be left behind.
 
lol sorry forgot to finish editing the post :P

Anyway, I want button 1,2,3,4 to move button5... it loads the form perfectly with no errors or problems... but the buttons 1,2,3,4 dont move button5 in the direction im telling it too. :(

Thanks guys :D
 
Ok... so what is the punchline ? :-)

If the code above is not working then you probably have a problem with the properties of button 5. Delete it and place it on the form again - you can name it button5 so you don't have to redo your work. I am betting on the button being anchored.
 
My WinForms C# is very rusty, but I think you might need to call Application.DoEvents after each change to the UI, otherwise the changes aren't reflected. The UI is usually updated on a different thread then your main worker thread, and so the the updating thread needs to be informed that it needs to process events. You also need to check how each button is Anchored and Docked inside it's container/panel. This could affect whether the button moves or not.
 
Also, try posting your question on StackOverflow.com. It's free, and an awesome resource for programmers, especially for .Net related stuff.
 
lol sorry forgot to finish editing the post :P

Anyway, I want button 1,2,3,4 to move button5... it loads the form perfectly with no errors or problems... but the buttons 1,2,3,4 dont move button5 in the direction im telling it too. :(

Thanks guys :D

I built this based on your code (as I posted earlier), and it works as intended. By "direction im telling it too", do you mean "When I click right, the button moves left?". If so, do you know that the top left of your screen is x=0 and y=0. If you want to place something 10 pixels from the top and 10 pixels from the left of the screen, you place it at (Left=10, Top=10). If you want to move it right and down by 5 pixels, you set it to (Left=15, Top=15).
 
Last edited:
My WinForms C# is very rusty, but I think you might need to call Application.DoEvents after each change to the UI, otherwise the changes aren't reflected. The UI is usually updated on a different thread then your main worker thread, and so the the updating thread needs to be informed that it needs to process events. You also need to check how each button is Anchored and Docked inside it's container/panel. This could affect whether the button moves or not.

He's not doing any long running process in the button click event handler, so Application.DoEvents is not necessary. He might have to do it if we was in a long loop for example. You may be onto something with the placement of his button though.
 
Top
Sign up to the MyBroadband newsletter
X