where's the problem?
I would like to add a new textbox to grid dynamically. (Silverlight)
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBox o = new TextBox();
o.SetValue(Canvas.TopProperty, 160); // margin top, I hope.
o.SetValue(Canvas.LeftProperty, 40); // margin left, I hope.
o.Height = 31;
o.Width = 140;
o.HorizontalAlignment= HorizontalAlignment.Left;
o.TextWrapping = TextWrapping.Wrap;
o.VerticalAlignment = VerticalAlignment.Top;
LayoutRoot.Children.Add(o);
}
Thank you so much.
Regards, Daniele.
I think LayoutRoot is a Grid in XAML:
<Grid Name="LayoutRoot">
In this case, you should remove the assignment of Canvas.Top and Canvas.Left and set the Margin of the TextBox:
TextBox o = new TextBox();
o.Margin = new Thickness(40, 160, 0, 0);
//...
Canvas.-properties has only an effect in the Canvas container. And you should use the corresponding methods to set the attached properties in codebehind:
Canvas.SetTop(o, 160);
Related
I cannot find a way to change the hover color when delopying it programmatically in c# winforms.
I hope someone can help me!
Code:
Button btn = new Button
{
Name = "btn1",
Width = 250,
Height = 250,
Location = new Point(0, 15),
BackColor = Color.Transparent,
FlatStyle = FlatStyle.Flat,
BackgroundImage = img,
BackgroundImageLayout = ImageLayout.Stretch,
};
You can specify the hover color outside of the initialization block:
// We create button
Button btn = new Button
{
...
}
// And then specify hovering behaviour
// Blue while hovering
btn.FlatAppearance.MouseOverBackColor = Color.Blue;
// Red when pressing (uncomment if you want)
// btn.FlatAppearance.MouseDownBackColor = Color.Red;
use MouseEnter and MouseLeave for change background color in button
Button btn = new Button
{
Name = "btn1",
Width = 250,
Height = 250,
Location = new Point(0, 15),
BackColor = Color.Transparent,
FlatStyle = FlatStyle.Flat,
BackgroundImage = img,
BackgroundImageLayout = ImageLayout.Stretch,
};
btn.MouseEnter += OnMouseEnter;
btn.MouseLeave += OnMouseLeave;
private void OnMouseEnter(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
}
private void OnMouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Transparent;
}
Is it possible to set for each side of a border its own EventHandler for mouse-enter or mouse-leave event. For example for the Left-Border of a Grid and Top-Border of a Grid?
What I am actually trying to do is allow the user to resize Grid-Elements inside a Canvas that contain a TextBlock with the mouse.
I am inserting my Grid/Border into the Canvas with the following code:
Border border = new Border();
border.BorderThickness = new Thickness(2);
border.BorderBrush = Brushes.Black;
TextBlock tb = new TextBlock();
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextWrapping = TextWrapping.Wrap;
tb.Padding = new Thickness(5, 5, 5, 5);
tb.Text = fd.LabelText;
Grid grid = new Grid();
grid.Background = labelBackgroundBrush;
grid.Background.Opacity = myOpactiy;
border.DataContext = fd;
grid.Children.Add(tb);
border.Child = grid;
I found a good example at csharphelper.com . Although my implementation is still buggy this was a good inspiration for me. Maybe it can help others who want to do the same.
I need to display a full screen dialog (in application window boundaries) in my UWP application, but can't seems to make it work. I tried with :
ContentDialog only shows vertically stretched with FullSizeDesired="True"
Popup, even trying to set the width and height in code behind its not working
Flyout Placement="Full" only stretch it vertically just like the contentdialog
Can't believe I spend so much time on that thing :(
Thanks
Have you tried something like this:
var c = Window.Current.Bounds;
var g = new Grid
{
Width = c.Width,
Height = c.Height,
Background = new SolidColorBrush(Color.FromArgb(0x20, 0, 0, 0)),
Children =
{
new Rectangle
{
Width = 100,
Height = 100,
Fill = new SolidColorBrush(Colors.White),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 3
}
}
};
var p = new Popup
{
HorizontalOffset = 0,
VerticalOffset = 0,
Width = c.Width,
Height = c.Height,
Child = g
};
p.IsOpen = true; // open when ready
You should see a semi-transparent overlay with a white rectangle in the middle of your screen.
To make a popup Fullscreen I did the following. Hope this helps.
I have a user control I wanted to show as a popup. Its name "URLUserControl"
Step 1: UI of the UC (Just the first bit. Not complete code)
<UserControl>
<Grid>
<Popup x:Name="MPopup" VerticalAlignment="Center">
<Grid x:Name="MainGrid">
Step 2: UC constructor adds the following
private void InitURLUserControl()
{
MPopup.IsOpen = true;
Window.Current.SizeChanged += Current_SizeChanged;
MainGrid.Width = Window.Current.Bounds.Width;
MainGrid.Height = Window.Current.Bounds.Height;
}
Step 3:
private void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
MainGrid.Width = Window.Current.Bounds.Width;
MainGrid.Height = Window.Current.Bounds.Height;
}
The above code will help the popup to be the center of the screen and display in fullscreen in size. Check it out. Happy coding!
I am using the TableLayoutPanel for example if I have 3 rows and 5 columns. I want to draw only the outer border for the entire panel. By default the the panel provides CellBorderStyle which adds all side borders to all the cells available. Is there any way where we can set only outside borders?
I have provided a sample code below.
TableLayoutPanel tblPanel = new TableLayoutPanel;
tblPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lblName;
TextBox txtName;
Button btnAdd;
int colCnt = 0;
for(int rw =0; rw < 3; rw++)
{
lblName = new Label();
lblName.Name = "mylabel" + rw.ToString();
tblPanel.Controls.Add(lblName, colCnt, rw);
colCnt++;
txtName = new TextBox();
txtName.Name = "mytext" + rw.ToString();
tblPanel.Controls.Add(txtName, colCnt, rw);
colCnt++;
btnAdd = new Button();
btnAdd.Name = "mybutton" + rw.ToString();
tblPanel.Controls.Add(btnAdd, colCnt, rw);
colCnt = 0;
}
TableLayoutPanel does in fact support the BorderStyle property, which is what you want. For example:
tableLayoutPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.borderstyle(v=vs.110).aspx
It is decorated with:
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
So Intellisense won't show it to you, but it is documented and it works. I have no insight into why it is non-browsable.
You'd be better off painting the cell border yourself. Something along the following lines, then customize:
public TableForm() {
InitializeComponent();
this.tableLayoutPanel.CellPaint += tableLayoutPanel_CellPaint;
}
private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
var topLeft = e.CellBounds.Location;
var topRight = new Point(e.CellBounds.Right, e.CellBounds.Top);
e.Graphics.DrawLine(Pens.Black, topLeft, topRight);
}
At design-time:
At runtime:
You can achieve by changing the property CellBorderStyle to Single or desired selection.
Property Change :
Sample :
TableLayOutPanel itself does not support a property for border except CellBorderStyle which is not what you want.
I suggest you to put your TableLayOutPanel into a Panel control and set Dock property of your TableLayOutPanel to Fill.
Then Set BorderStyle of Panel to what you want (FixedSingle or Fixed3D)
public TestForm()
{
InitializeComponent();
tableLayoutPanel.Paint += tableLayoutPanel_Paint;
}
private void tableLayoutPanel_Paint(object sender, PaintEventArgs e){
e.Graphics.DrawRectangle(new Pen(Color.Blue), e.ClipRectangle);
}
I have the following code:
TextBlock tmp = new TextBlock
{
Text = displayText,
Foreground = new SolidColorBrush(Colors.Red),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontSize = 30
};
Grid grd = new Grid();
grd.Children.Add(tmp);
// grd.Background = new SolidColorBrush(Colors.LightGray);
Viewbox vb = new Viewbox();
vb.Child = grd;
vb.Width = width;
vb.Height = height;
DrawingCvs.Children.Add(vb);
Canvas.SetLeft(vb, xpos);
Canvas.SetTop(vb, ypos);
Canvas.SetZIndex(grd, 1000);
// we need a second grid for cosmetic reasons. Due to
// how the viewbox works, when we resize we lose the full
// width of the grid which has a textblock in it
Grid cosmeticGrd = new Grid
{
Width = width,
Height = height,
Background = new SolidColorBrush(Colors.LightGray)
};
DrawingCvs.Children.Add(cosmeticGrd);
Canvas.SetLeft(cosmeticGrd, xpos);
Canvas.SetTop(cosmeticGrd, ypos);
Canvas.SetZIndex(grd, 0);
What I want is for the Grid added first in the code to be above the grid added second. What is wrong with my Z-Index property setting here? Or is it something else?
InB4 Change the order you add them in the code--yes I realize this, but as a point of understanding I want to understand the ZIndex property.
Your first SetZindex call:
Canvas.SetZIndex(grd, 1000);
incorrectly applies the setting to the Grid instead of the Viewbox. It should be:
Canvas.SetZIndex(vb, 1000);
because the Viewbox is the child of the Canvas.
Likewise, your second SetZIndex call:
Canvas.SetZIndex(grd, 0);
is applied to the wrong Grid. It should be:
Canvas.SetZIndex(cosmeticGrd, 0);
In summary, the Canvas.ZIndex attached property affects the ordering of Canvas children.