MouseEnter Stack - silverlight

In the code that follows,was trying get the border that was clicked the name or the text of the child that's a textblock my problem is that without "eaclick.Handled = true;" in the code it starts showing me all the Names of the border that the mouse has entered before the click and not not only the oned that was clicked by adding "eaclick.Handled = true;" shows me alls the first border that the mouse has entered,it seems to me that's saving in a stack all the mouseenters and when Click in leftmousedown it goes get that stack instand of getting me the last mouseenter that I want can anyone explain me how to fix or what I am doing wrong?
for (int i = 0; i < NumPages; i++)
{
Border borderaux = new Border();
borderaux.Name = Convert.ToString(i);
//borderaux.MouseEnter += borderaux_MouseEnter;
Border clicked;
borderaux.MouseEnter += (smouse, eamouse) =>
{
clicked = (Border)smouse;
clicked.Cursor = Cursors.Hand;
MouseLeftButtonDown += (sclick, eaclick) =>
{
if (eaclick.ClickCount == 1)
{
TextBlock opcao = (TextBlock)(clicked).Child;
//string opcao="";
MessageBox.Show("Pressed-->" + opcao.Text);
//MessageBox.Show("Pressed-->" + clicked.Name);
eaclick.Handled = true;
}
};

the problem was that using the MouseLeftButtonDown event inside MouseEnter event that was giving the problem,that when was clicked it would show me x MessageBox showing all the number of borders that the mouse had hovered until the click,with the fix it show me the border that was actualy clicked.
for (int i = 0; i < NumPages; i++)
{
Border borderaux = new Border();
borderaux.Name = Convert.ToString(i);
//borderaux.MouseEnter += borderaux_MouseEnter;
Border clicked;
borderaux.MouseLeftButtonDown += (sclick, eaclick) =>
{
if (eaclick.ClickCount == 1)
{
TextBlock opcao = (TextBlock)((Border)sclick).Child;
//string opcao="";
MessageBox.Show("Pressed-->" + opcao.Text);
//eaclick.Handled = true;
}
};
borderaux.MouseEnter += (smouse, eamouse) =>
{
clicked = (Border)smouse;
clicked.Cursor = Cursors.Hand;
};

Related

Avoid window opening between two screens

I am writing a Photoshop plugin for Windows and want to place the plugin dialog in the center of the main window.
This is my code:
void centre_window(HWND hwnd){
RECT rs, rd;
HWND hw = GetParent(hwnd); // GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow(hwnd,(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 3,
rd.right - rd.left, rd.bottom - rd.top, TRUE);
}
So far, it works. But there is one flaw: If the main window is spread across two screens, then my window is between both screens.
I looked at other Photoshop plugins and they handle it like this:
Place the window at the main window center
If it would be between two screens, choose one of these two and place it at the border of the screen
How can I do that?
With the helpful hint of #IInspectable , I wrote following code:
void _doMonitorAdjustments(LPRECT rcPlugin) {
RECT rcMonitorWork;
HMONITOR hMonitor;
MONITORINFO grMonitorInfo = { sizeof(grMonitorInfo) };
int nXAdjust = 0, nYAdjust = 0, nPluginWidth, nPluginHeight, nMonitorWorkWidth, nMonitorWorkHeight;
hMonitor = MonitorFromRect(rcPlugin, MONITOR_DEFAULTTONEAREST);
if (hMonitor == NULL) return;
if (!GetMonitorInfoA(hMonitor, &grMonitorInfo)) return;
rcMonitorWork = grMonitorInfo.rcWork;
// Don't let the window exit the left/right borders of the monitor
nPluginWidth = rcPlugin->right - rcPlugin->left;
nMonitorWorkWidth = rcMonitorWork.right - rcMonitorWork.left;
if (nPluginWidth > nMonitorWorkWidth) {
// Window larger than screen width. Decrease the width!
rcPlugin->left = rcMonitorWork.left;
rcPlugin->right = rcMonitorWork.right;
}
else if (rcPlugin->left < rcMonitorWork.left) {
nXAdjust = rcMonitorWork.left - rcPlugin->left;
}
else if (rcPlugin->right > rcMonitorWork.right) {
nXAdjust = rcMonitorWork.right - rcPlugin->right;
}
// Don't let the window exit the top/bottom borders of the monitor
nPluginHeight = rcPlugin->bottom - rcPlugin->top;
nMonitorWorkHeight = rcMonitorWork.bottom - rcMonitorWork.top;
if (nPluginHeight > nMonitorWorkHeight) {
// Window larger than screen height. Decrease the height!
rcPlugin->top = rcMonitorWork.top;
rcPlugin->bottom = rcMonitorWork.bottom;
}
else if (rcPlugin->top < rcMonitorWork.top) {
nYAdjust = rcMonitorWork.top - rcPlugin->top;
}
else if (rcPlugin->bottom > rcMonitorWork.bottom) {
nYAdjust = rcMonitorWork.bottom - rcPlugin->bottom;
}
OffsetRect(rcPlugin, nXAdjust, nYAdjust);
}
/*
* Centers a window to the center of its parent form but avoids
* being spread across two screens.
*/
void centre_window(HWND hwnd) {
RECT rcParent, rcWindowOriginal, rcPlugin;
HWND hParent;
hParent = GetParent(hwnd);
if (hParent == NULL) hParent = GetDesktopWindow();
if (!GetWindowRect(hParent, &rcParent)) return;
if (!GetWindowRect(hwnd, &rcWindowOriginal)) return;
rcPlugin.left =
rcParent.left
+ (rcParent.right - rcParent.left) / 2
- (rcWindowOriginal.right - rcWindowOriginal.left) / 2;
rcPlugin.top =
rcParent.top
+ (rcParent.bottom - rcParent.top) / 2
- (rcWindowOriginal.bottom - rcWindowOriginal.top) / 2;
rcPlugin.right =
rcPlugin.left + rcWindowOriginal.right - rcWindowOriginal.left;
rcPlugin.bottom =
rcPlugin.top + rcWindowOriginal.bottom - rcWindowOriginal.top;
// Avoid that the window is spread across two screens
_doMonitorAdjustments(&rcPlugin);
MoveWindow(hwnd,
rcPlugin.left,
rcPlugin.top,
/*width=*/rcPlugin.right - rcPlugin.left,
/*height=*/rcPlugin.bottom - rcPlugin.top,
TRUE);
}

How to paint a specific cell of a stackpanel?

In order to draw the matrix of cells , I used a Stackpanel, defined by this code:
int columns = Convert.ToInt32(columnasText.Text);
int rows = Convert.ToInt32(filasText.Text);
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
SolidColorBrush released = new SolidColorBrush(Colors.White);
for (int i = 0; i < rows; i++)
{
StackPanel stkPanel = new StackPanel();
stkPanel.Orientation = Orientation.Horizontal;
for (int j = 0; j < columns; j++)
{
Label lbl = new Label();
lbl.Height = rejilla.Height / rows;
lbl.Width = rejilla.Width / columns;
lbl.Tag = new Point(i, j);
lbl.BorderBrush = new SolidColorBrush(Colors.Black);
lbl.BorderThickness = new Thickness(1);
lbl.Background = released;
stkPanel.Children.Add(lbl);
}
rejilla.Children.Add(stkPanel);
Once is defined, I need to change the colours of each cell depending on the values of each, and I'm not able to do it.
I used your variable rejilla (I am assuming it is either StackPanel or Grid). Either way, it will work.
Method 1:
The key is using .Children.OfType()
//Get your cell location
int rowIndex = 2;
int columnIndex = 8;
//Get your desired new color
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
//Get list of your row panels
var stackPanels = rejilla.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var labels = stackPanels[rowIndex].Children.OfType<Label>().ToList();
//Check if desired cell exist or not then change background
if (columnIndex < labels.Count && columnIndex >= 0)
labels[columnIndex].Background = selected1;
}
Method 2: Using your Tag that you set (Point)
Not recommended, this method would have been useful if you placed all your labels in one stack panel instead of placing them in multiple stack panels (one stack panel for each row).
//Get your cell location
int rowIndex = 2;
int columnIndex = 8;
//Get your desired new color
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
//Get list of your row panels
var stackPanels = rejilla.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var label = stackPanels[rowIndex].Children.OfType<Label>()
.Where(Item => (int)(Item.Tag as Nullable<Point>).GetValueOrDefault().X == rowIndex
&& (int)(Item.Tag as Nullable<Point>).GetValueOrDefault().Y == columnIndex).FirstOrDefault();
if(label != null)
label.Background = selected1;
}
You can place the code inside a method and pass your stackPanel, color, rowIndex, columnIndex:
private void SetCellColor(StackPanel stackPanel, SolidColorBrush color, int rowIndex, int columnIndex)
{
//Get list of your row panels
var stackPanels = stackPanel.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var labels = stackPanels[rowIndex].Children.OfType<Label>().ToList();
//Check if desired cell exist or not then change background
if (columnIndex < labels.Count && columnIndex >= 0)
labels[columnIndex].Background = color;
}
}
Then call it:
SetCellColor(rejilla, new SolidColorBrush(Colors.Aquamarine), rowIndex, columnIndex);
Good Luck!

Getting new line in wpf

i am having arraylist.let say it contains 15 elements. I am adding these to stack panel. I need to add 3 elements per line. my code is below. I am getting either horizontal or verrtical.Let me know how to do this.
MainWindow w;
public ShopCart(MainWindow m,ArrayList _list)
{
InitializeComponent();
w = m;
int i = 1;
foreach (string cartitems in _list)
{
mystackpanel.Orientation = Orientation.Horizontal;
mystackpanel.Margin.Left.Equals(150);
Label lbl = new Label();
lbl.Name = "Label" + i;
lbl.Height = 30;
lbl.Width = 200;
lbl.Margin.Left.Equals(150);
//lbl.Margin.Top.Equals(150);
lbl.Content = cartitems.ToString();
mystackpanel.Children.Add(lbl);
i++;
int str = mystackpanel.Children.Count;
MessageBox.Show(Convert.ToString(str));
if (str%3 == 0)
{
Button btndelete = new Button();
btndelete.Content = "Delete";
btndelete.Width = 120;
btndelete.Height = 35;
mystackpanel.Children.Add(btndelete);
mystackpanel.Margin.Top.Equals(500);
}
}
Here is some sample code(assuming you already have a list of buttons, and will add outer stack panel to your main control) you can try, you may need to change few things as per your need:
List<Button> buttons = new List<Button>();
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
int count = 0;
StackPanel innerPanel = new StackPanel();
innerPanel.Orientation = Orientation.Vertical;
foreach (Button button in buttons)
{
innerPanel.Children.Add(button);
++count;
if (count % 3 == 0 && count != 0)
{
panel.Children.Add(innerPanel);
innerPanel = new StackPanel();
innerPanel.Orientation = Orientation.Vertical;
}
}
if (panel.Children.Contains(innerPanel) == false)
{
panel.Children.Add(innerPanel);
}
Although in my opinion the best way will be to have a Grid with n*n row and columns and add your buttons to respective row, columns.

How to highlight a datagridview row when a character is typed in the search textbox

Using a textbox to search a value in a given datagridview column, the code below position the selected row on the column containing the text typed.
private void textBox1_TextChanged(object sender, EventArgs e)
{
//if (Char.IsLetter(e.KeyChar))
//{
for (int i = 0; i < (productDataGridView.Rows.Count); i++)
{
if (productDataGridView.Rows[i].Cells[1].Value.ToString().StartsWith(textBox1.Text, true, CultureInfo.InvariantCulture))
{
productDataGridView.FirstDisplayedCell = productDataGridView[1, i];
productDataGridView.CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
return; // stop looping
}
}
}
The problem is that I cannot highlight or change the back color of the required row while typing in the textbox, any help please?
Try this
//restore backcolor of rows to default e.g. loop through grid and set backcolor to white
foreach(DataGridViewRow row in productDataGridView.Rows)
{
if (row.Cells[1].Value.ToString().StartsWith(textBox1.Text, true, CultureInfo.InvariantCulture))
{
//productDataGridView.FirstDisplayedCell = productDataGridView[1, i];
row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
return; // stop looping
}
else
//Set backcolor to default
}
//return; //move return here
Tried this solution, this finally works like a charm:
try
{
productDataGridView.ClearSelection(); //or restore rows backcolor to default
for (int i = 0; i < (productDataGridView.Rows.Count); i++)
{
if (productDataGridView.Rows[i].Cells[1].Value.ToString().StartsWith(textBox1.Text, true, CultureInfo.InvariantCulture))
{
productDataGridView.FirstDisplayedScrollingRowIndex = i;
productDataGridView.Rows[i].Selected = true; //It is also possible to color the row backgroud
return;
}
}
}
catch (Exception)
{
}

WinForms TreeView with both radios and checkboxes

I have a case where I would like TreeView to be able to show radio buttons on multiple root nodes, and checkboxes on their children. There would only be one level of children beneath any root node.
The radios should also behave like a group, ie one root is selected and the others' radios deselect.
I've been trying to fake it with images, but it doesn't look realistic. I originally had a listbox and a separate checkedlistbox, but the usability gods struck it down.
Has anyone implemented this functionality or have another suggestion?
Think of it this way:
(o) McDonalds
[ ] Burger
[ ] Fries
[ ] Drink
(o) Burger King
[ ] Burger
[ ] Fries
[ ] Drink
(*) Wendy's
[x] Burger
[x] Fries
[ ] Drink
You can have one big option, but select 1..n underneath the big option.
I came up with a solution based on the article http://www.codeproject.com/KB/combobox/RadioListBoxDotNetVersion.aspx. My implementation inherits from CheckedListBox and includes the following methods:
protected override void OnDrawItem (DrawItemEventArgs e)
{
// Erase all background if control has no items
if (e.Index < 0 || e.Index > this.Items.Count - 1)
{
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
return;
}
// Calculate bounds for background, if last item paint up to bottom of control
Rectangle rectBack = e.Bounds;
if (e.Index == this.Items.Count - 1)
rectBack.Height = this.ClientRectangle.Top + this.ClientRectangle.Height - e.Bounds.Top;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), rectBack);
// Determines text color/brush
Brush brushText = SystemBrushes.FromSystemColor(this.ForeColor);
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
brushText = SystemBrushes.GrayText;
Boolean bIsChecked = this.GetItemChecked(e.Index);
String strText;
if (!string.IsNullOrEmpty(DisplayMember)) // Bound Datatable? Then show the column written in Displaymember
strText = ((System.Data.DataRowView) this.Items[e.Index])[this.DisplayMember].ToString();
else
strText = this.Items[e.Index].ToString();
Size sizeGlyph;
Point ptGlyph;
if (this.GetItemType(e.Index) == ItemType.Radio)
{
RadioButtonState stateRadio = bIsChecked ? RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal;
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
stateRadio = bIsChecked ? RadioButtonState.CheckedDisabled : RadioButtonState.UncheckedDisabled;
// Determines bounds for text and radio button
sizeGlyph = RadioButtonRenderer.GetGlyphSize(e.Graphics, stateRadio);
ptGlyph = e.Bounds.Location;
ptGlyph.X += 4; // a comfortable distance from the edge
ptGlyph.Y += (e.Bounds.Height - sizeGlyph.Height) / 2;
// Draws the radio button
RadioButtonRenderer.DrawRadioButton(e.Graphics, ptGlyph, stateRadio);
}
else
{
CheckBoxState stateCheck = bIsChecked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
stateCheck = bIsChecked ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
// Determines bounds for text and radio button
sizeGlyph = CheckBoxRenderer.GetGlyphSize(e.Graphics, stateCheck);
ptGlyph = e.Bounds.Location;
ptGlyph.X += 20; // a comfortable distance from the edge
ptGlyph.Y += (e.Bounds.Height - sizeGlyph.Height) / 2;
// Draws the radio button
CheckBoxRenderer.DrawCheckBox(e.Graphics, ptGlyph, stateCheck);
}
// Draws the text
Rectangle rectText = new Rectangle(ptGlyph.X + sizeGlyph.Width + 3, e.Bounds.Y, e.Bounds.Width - sizeGlyph.Width, e.Bounds.Height);
e.Graphics.DrawString(strText.Substring(4), e.Font, brushText, rectText, this.oAlign);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
protected override void OnItemCheck (ItemCheckEventArgs ice)
{
base.OnItemCheck(ice);
if (ice.NewValue == CheckState.Unchecked)
return;
if (this.GetItemType(ice.Index) == ItemType.Radio) // if they selected a root, deselect other roots and their children
{
for (Int32 i = 0; i < this.Items.Count; ++i)
{
if (i == ice.Index)
continue;
if (this.GetItemType(i) == ItemType.Radio)
{
this.SetItemChecked(i, false);
Int32 j = i + 1;
while (j < this.Items.Count && this.GetItemType(j) == ItemType.Checkbox)
{
this.SetItemChecked(j, false);
j++;
}
}
}
}
else if (this.GetItemType(ice.Index) == ItemType.Checkbox) // they selected a child; select the root too and deselect other roots and their children
{
// Find parent
Int32 iParentIdx = ice.Index - 1;
while (iParentIdx >= 0 && this.GetItemType(iParentIdx) == ItemType.Checkbox)
iParentIdx--;
this.SetItemChecked(iParentIdx, true);
}
}
protected ItemType GetItemType (Int32 iIdx)
{
String strText = this.Items[iIdx].ToString();
if (strText.StartsWith("(o)"))
return (ItemType.Radio);
else if (strText.StartsWith("[x]"))
return (ItemType.Checkbox);
throw (new Exception("Invalid item type"));
}

Resources