Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:37:12+00:00 2026-06-09T19:37:12+00:00

I have a UserControl(CardGUI) that I created for my WPF canvas, and I implemented

  • 0

I have a UserControl(CardGUI) that I created for my WPF canvas, and I implemented MouseEvents on the UserControl like the ones below. Thing is when I drag the UserControl from the toolbox onto the canvas, it works just fine, but when I try to add a new control, CardGUI card = new CardGUI(); in my MainForm, I can no longer move the control, and I can’t figure out why this is.

I tried debugging but the events still get triggered when I click on the newly added control, yet I cannot move it.

public void UserControl_MouseLeftButtonDown(object sender, 
                                            MouseButtonEventArgs e)
{
    if (!inDrag)
    {
        anchorPoint = e.GetPosition(null);
        CaptureMouse();
        inDrag = true;
        e.Handled = true;
    }
}

public void UserControl_MouseMove(object sender, MouseEventArgs e)
{
    if (inDrag)
    {
        currentPoint = e.GetPosition(null);       
        Canvas.SetLeft(this, 
                       Canvas.GetLeft(this) + 
                           (currentPoint.X - anchorPoint.X));
        Canvas.SetTop(this, 
                      Canvas.GetTop(this) + 
                         (currentPoint.Y - anchorPoint.Y));
        anchorPoint = currentPoint;
        e.Handled = true;
    }
}

public void UserControl_MouseLeftButtonUp(object sender, 
                                          MouseButtonEventArgs e)
{
    if (inDrag)
    {
        ReleaseMouseCapture();
        inDrag = false;
        e.Handled = true;
    }
}

I add the control to my mainform like:

        this.deckCard = new CardGUI();            
        this.deckCard.Margin = new Thickness(xDeckCoord, yDeckCoord, 0, 0);
        this.main.Children.Add(this.deckCard);
        this.deckCard.IsHitTestVisible = true;
        this.deckCard.AllowDrop = true;

I figure it might be that since the method adding the control only gets called once, the location doesnt really update eventhough the event is triggered. I didn’t have that problem if I dragged the control from the ToolBox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:37:13+00:00Added an answer on June 9, 2026 at 7:37 pm

    You are adding it to a Canvas, but not setting any position or size

    Try adding:

    this.main.SetLeft(deckCard, 0);
    this.main.SetTop(deckCard, 0);
    

    Or use a Grid instead if you want stuff to auto stretch to the available size (which your use of Margin seems to infer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one UserControl nested inside the other UserControl, something like that: <uc:MyControl1> <uc:MyControl2
I have a UserControl in WPF that contain a textBox. My user control is
I facing very huge problem now, that is I have usercontrol (created by me)
I have a usercontrol in WPF that have 2 buttons inside. The buttons are
I have an UserControl that contains a Button : <UserControl> <Button> </UserControl> I want
I have a UserControl with a Border element within it that I want to
I have a UserControl that has a BaseClass object as a public member. Right
I have a simple usercontrol that uses a simple custom panel where I just
I have a usercontrol (.ascx) like this: <% if (HasAccessMediaPlans) { iPortletCounter++; %> <div
I have a UserControl that becomes a page in an XPS report. Part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.