How do I place card so each added card overlap in the way I want

T

Tony Johansson

Hello

Here is some code that place a user control called CardGUI in another user
control named PlayerGUI.
Assume width for the CardGUI is 100. The first card is placed at xPos=0 and
y=0.
Now when I place the second card I set xPos = 50 and y = 0.
The problem is that I want the second card to overlap the first so only the
left part of the first card is shown.
I thought this code did this but here only the right part the second card
that is shown.

private void InitialDeal()
{
Control[] ctrl = this.pnlPlayers.Controls.Find(PlayerGUI, false);

for (int cardNr = 0; cardNr < StartDeal; cardNr++)
{
for (int playerNr = 0; playerNr < ctrl.Length; playerNr++)
{
Card card = gm.GetPlayerCard(playerNr, cardNr);
for (int i = 0; i < pnlPlayers.Controls.Count; i++)
{
PlayerGUI pg = ctrl as PlayerGUI;
if (pg.PlayerName == gm.Players[playerNr].Name)
{
string imagePath =
Path.Combine(Environment.CurrentDirectory, @"..\..\Images\" + card.Suit+
".ico");
CardGUI cg = new CardGUI(imagePath,card.RankSymbol());

int xPos = (cardNr % 2) * cg.Width/2;
cg.Location = new Point(xPos, 0);
pg.AddCard(cg);
break;
}
}
}
}
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
Google "z-order". In .NET, BringToFront() and SendToBack() can help you
manage this.

This BringToFront on my user control CardGUI does not bring that control to
front it is still in the back.
I have a panel that I put these user control in and I always want the last
added control to be in the front.
I have read the docs and it doesn't say any strange about this. It says
"The control is moved to the front of the z-order. If the control is a child
of another control, the child control is moved to the front of the z-order.
BringToFront does not make a control a top-level control, and it does not
raise the Paint event."
I don't get any kind of error it just don't do what I have told it to do
which is bring the control in front.

Have I missed something here ?

//Tony
 
T

Tony Johansson

Peter Duniho said:
[...]
Have I missed something here ?

Definitely. But since you haven't bothered to post a concise-but-complete
code example that reliably reproduces the problem, well.you know the rest.

Pete

Here is complete example where I use Button and it doesn't work as expected.
As you can see in the code I first put one button in the panel with a text
First and then
I put a second button in the panel with a text Second. This second Button is
positioned a litte bit to the right
from the first button. I use BringTofront to make the last button the
topmost but instead it will be put under the first button so the first
button is always the topmost
Is this perhaps a bug because it doesn't work as it should ?

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 Slask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

for (int i = 0; i <= 1; i++)
{
Button btn = new Button();

int xPos = (i % 2) * btn.Width / 2;
btn.Location = new Point(xPos, 0);
btn.BringToFront();
if (i == 0)
btn.Text = "First";
else
btn.Text = "Second";

panel1.Controls.Add(btn);
}
}
}

partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 26);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(268, 197);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}

#endregion

private System.Windows.Forms.Panel panel1;
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

//Tony
 
J

Jeff Johnson

for (int i = 0; i <= 1; i++)
{
Button btn = new Button();

int xPos = (i % 2) * btn.Width / 2;
btn.Location = new Point(xPos, 0);
btn.BringToFront();
if (i == 0)
btn.Text = "First";
else
btn.Text = "Second";

panel1.Controls.Add(btn);

I have only glanced at this, but I'm virtually positive that your call to
BringToFront() is useless as-is. The button as not been given a parent yet;
this doesn't happen until you call panel1.Controls.Add(). BringToFront()
only works with sibling controls, i.e., controls with the same parent. So if
the button has no parent, what controls is it supposed to be brought to the
front of?
 
T

Tony Johansson

Jeff Johnson said:
I have only glanced at this, but I'm virtually positive that your call to
BringToFront() is useless as-is. The button as not been given a parent
yet; this doesn't happen until you call panel1.Controls.Add().
BringToFront() only works with sibling controls, i.e., controls with the
same parent. So if the button has no parent, what controls is it supposed
to be brought to the front of?

Yes you are right I just move this row
btn.BringToFront();
after this row
panel1.Controls.Add(btn);
and now it works.

Many thanks

//Tony
 
Top