Xamarin.Forms - change size of control in code behind - c#

I would like to change size of a control in Xamarin.Forms with some event in code behind of a control.
Especially, I would like to change size of a StackLayout when Orientation of device changes.
For now, I have done it like this (but it does not work):
private void SettingsPage_OnSizeChanged(object sender, EventArgs e)
{
var isPortrait = UIHelpers.IsPortrait(this);
if (isPortrait)
RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width;
else
{
RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width/2;
}
this.UpdateChildrenLayout();
this.ForceLayout();
RemoteServerSL.ForceLayout();
}
The purpose is to change Width of my StackLayout to parentWidth if I'm in landscape mode.
How can I handle that?
BTW.I'm working on Android for now.

You should use OnSizeAllocated method override to detect orientation change;
double previousWidth;
double previousHeight;
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (previousWidth != width || previousHeight != height)
{
previousWidth = width;
previousHeight = height;
if (width > height)
{
// landscape mode
}
else
{
// portrait mode
}
}
}

Related

Windows forms button background transparent

I have WPF app with ContentControl where the content is WindowsFormsHost with a Child having custom panel, which renders SDL stream. Now I have added button to disable/enable audio of the stream. Everything works fine, but I cannot make the button icon transparent. How can I do that? Is it possible at all?
AudioButton = new System.Windows.Forms.Button()
{
Enabled = AudioButtonEnabled,
BackColor = Color.Transparent,
Image = Image.FromFile(#".\Images\audioDisabled.png"),
Width = 30,
Height = 30,
FlatStyle = System.Windows.Forms.FlatStyle.Flat
};
AudioButton.FlatAppearance.BorderSize = 0;
AudioButton.Click += (object sender, EventArgs e) =>
{
};
SDLRenderer.AddButton(AudioButton);
The image (icon) is transparent as well.
The workaround can be to create custom WinForms button, override OnPaint event and make the specified color transparent for bitmap by calling Bitmap.MakeTransparent()
public class CustomButton : Button
{
private Color TransparentColor;
public CustomButton() : base()
{
TransparentColor = Color.FromArgb(192, 192, 192);
}
protected override void OnPaint(PaintEventArgs e)
{
if (this.Image != null)
{
Bitmap bmp = ((Bitmap)this.Image);
bmp.MakeTransparent(TransparentColor);
int x = (this.Width - bmp.Width) / 2;
int y = (this.Height - bmp.Height) / 2;
e.Graphics.DrawImage(bmp, x, y);
}
base.OnPaint(e);
}
}

Changing the span of CollectionView while rotating phone isn't working

I'm trying to change the span of CollectionView while rotating my iPhone. To make it easy, just show 2 columns in portrait, and 4 columns in landscape.
It works when rotating from portrait to landscape mode, but when rotating back to portrait mode, it always shows 1 column. My code likes,
VideoCollectionView = new CollectionView()
{
ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical),
};
...
private static double screen_width = 1280.0;
private static double screen_height = 720.0;
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if ((Math.Abs(screen_width - width) > minimum_double) || (Math.Abs(screen_height - height) > minimum_double))
{
screen_width = width;
screen_height = height;
int split;
if (screen_width > screen_height)
{ // landscape mode
split = 4;
}
else
{ // portrait mode
split = 2;
}
VideoCollectionView.ItemsLayout = new GridItemsLayout(split, ItemsLayoutOrientation.Vertical);
}
}
Is this a bug? Or I should use other ways? Thanks for help.
You could use a Singleton for storing the current orientation.Because it is unwise to set the screen size as s static value . It maybe will cause issue on different size of device .
public class CurrentDevice
{
protected static CurrentDevice Instance;
double width;
double height;
static CurrentDevice()
{
Instance = new CurrentDevice();
}
protected CurrentDevice()
{
}
public static bool IsOrientationPortrait()
{
return Instance.height > Instance.width;
}
public static void SetSize(double width, double height)
{
Instance.width = width;
Instance.height = height;
}
}
And in the method
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (CurrentDevice.IsOrientationPortrait() && width > height || !CurrentDevice.IsOrientationPortrait() && width < height)
{
int split;
CurrentDevice.SetSize(width, height);
// Orientation got changed! Do your changes here
if (CurrentDevice.IsOrientationPortrait())
{
// portrait mode
split = 2;
}
else
{
// landscape mode
split = 4;
}
VideoCollectionView.ItemsLayout = new GridItemsLayout(split, ItemsLayoutOrientation.Vertical);
}
}

Control not drawn on panel

I'm trying to add a control to my Panel. At mouseDown on the panel the point is saved and at mouseUp the point is saved. But at panel mouseUp nothing is drawn. How to solve it?
Ellipse class:
class Ellipse : Control
{
private int x;
private int y;
private int width;
private int height;
public Ellipse(int x, int y, int width, int height)
{
setY(y);
setX(x);
setWidth(width);
setHeight(height);
}
public int getX() { return x;}
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public void setX(int newx) { x = newx; }
public void setY(int newy) { y = newy; }
public void setWidth(int newwidth) { width = newwidth; }
public void setHeight(int newheight) { height = newheight; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Call methods of the System.Drawing.Graphics object.
// Declare and instantiate a new pen.
System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua);
// Draw an aqua rectangle in the rectangle represented by the control.
e.Graphics.FillEllipse(Brushes.Black,x,y,width,height);
}
}
Form1 class
private void panel_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
x = e.X;
y = e.Y;
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
xe = e.X;
ye = e.Y;
Item item;
Enum.TryParse<Item>(menuComboBoxShape.ComboBox.SelectedValue.ToString(), out item);
switch (item)
{
case Item.Pencil:
using (Graphics g = panel.CreateGraphics())
using (var pen = new Pen(System.Drawing.Color.Black)) //Create the pen used to draw the line (using statement makes sure the pen is disposed)
{
g.DrawLine(pen,new Point(x, y), new Point(xe, ye));
}
break;
case Item.Rectangle:
break;
case Item.Ellipse:
Ellipse el = new Ellipse(x,y,xe-x,ye-y);
panel.Controls.Add(el);
break;
default:
break;
}
}
You are inheriting your Ellipse class from Control, but in fact you're not using it as a control - you're not adding it in Controls collection of form, so in fact it is invisible, inactive and not receiving any events from form.
Also painting the control from outer code looks like a bad design. Control should paint itself, and you should set it bounds from outer code.
Here is snippet to drive you to the right way:
class Ellipse : Control
{
Point mDown { get; set; }
public Ellipse()
{
MouseDown += shape_MouseDown;
MouseMove += shape_MouseMove;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillEllipse(Brushes.Black, this.Bounds);
}
private void shape_MouseDown(object sender, MouseEventArgs e)
{
mDown = e.Location;
}
private void shape_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Location = new Point(e.X + Left - mDown.X, e.Y + Top - mDown.Y);
}
}
}
And in the form you should create it like:
el = new Ellipse();
el.Bounds = new Rectangle(0, 0, 100, 100);
Controls.Add(el);
Update
Based on your updated code, I can see a couple of issues:
You actually don't need x, y, width, height properties of your Ellipse class and according getter/setter methods, since it's Control, and it has its own Location and Width, Height public properties.
You are drawing your ellipse incorrectly. Assuming it should fill all the area, painting should be e.Graphics.FillEllipse(Brushes.Black,0,0,Width,Height) (here I assuming using Control.Width instead of your width and so on). Otherwise you're additionally shifting your painted ellipse.
Code in panel_MouseUp concerning ellipse creation should be something like
var el = new Ellipse();
panel.Controls.Add(el);
el.Location = new Point(x, y);
el.Width = (xe - x);
el.Height = (ye - y);
Or, if it should be one single ellipse (right now you're creating new one each time) - create this one outside of mouseUp handler and inside of handler just change it's size and location.

Resize textbox and form size according to Text Length

How can I automatically increase/decrease TextBox and Windows Form size according to text Length?
You can try overriding the OnTextChanged event, then changing the Width depending on the size of the text.
protected override OnTextChanged(EventArgs e)
{
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(Text, Font);
Width = (int)Math.Ceiling(size.Width);
}
base.OnTextChanged(e);
}
Try this, it will also work...
Here I have taken 100 as minimum width of textbox. "txt" is TextBox.
const int width = 100;
private void textBox1_TextChanged(object sender, EventArgs e)
{
Font font = new Font(txt.Font.Name, txt.Font.Size);
Size s = TextRenderer.MeasureText(txt.Text, font);
if (s.Width > width)
{
txt.Width = s.Width;
}
}
Hope it helps.
Here is better solution.
Scenario is: I have a textbox that Filled on form (usercontrol). So, I want to change Form Height each time number of line in textBox change, but its height is not less than MinHeight (a constant)
private void ExtendFormHeight()
{
int heightChanged = txtText.PreferredSize.Height - txtText.ClientSize.Height;
if (Height + heightChanged > MinHeight)
{
Height += heightChanged;
}
else
{
Height = MinHeight;
}
}
Hope this help!
set width to Auto in properties

Problem with the scrolling!

I have implemented a picturebox in my in my form.I even implemented the scrollbars to make the image fit in it.so now the problem is when i try to scroll the button down,it scrolls down and immediately when i leave the mouse the button scrolls up ..here is the code implemnted please give some suggestions..
public void DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height >
pictureBox1.Image.Height - this.hScrollBar1.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
private void HandleScroll(Object sender, ScrollEventArgs se)
{
/* Create a graphics object and draw a portion
of the image in the PictureBox. */
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
GraphicsUnit.Pixel);
pictureBox1.Update();
}
public void SetScrollBarValues()
{
// Set the Maximum, Minimum, LargeChange and SmallChange properties.
this.vScrollBar1.Minimum = 0;
this.hScrollBar1.Minimum = 0;
// If the offset does not make the Maximum less than zero, set its value.
if ((this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
{
this.hScrollBar1.Maximum =
this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
}
// If the VScrollBar is visible, adjust the Maximum of the
// HSCrollBar to account for the width of the VScrollBar.
if (this.vScrollBar1.Visible)
{
this.hScrollBar1.Maximum += this.vScrollBar1.Width;
}
this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;
// Adjust the Maximum value to make the raw Maximum value
// attainable by user interaction.
this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
// If the offset does not make the Maximum less than zero, set its value.
if ((this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
{
this.vScrollBar1.Maximum =
this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
}
// If the HScrollBar is visible, adjust the Maximum of the
// VSCrollBar to account for the width of the HScrollBar.
if (this.hScrollBar1.Visible)
{
this.vScrollBar1.Maximum += this.hScrollBar1.Height;
}
this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
// Adjust the Maximum value to make the raw Maximum value
// attainable by user interaction.
this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
}
private void pictureBox1_Resize(object sender, EventArgs e)
{
// If the PictureBox has an image, see if it needs
// scrollbars and refresh the image.
if (pictureBox1.Image != null)
{
this.DisplayScrollBars();
this.SetScrollBarValues();
this.Refresh();
}
}
As I comment above, the correct way of doing this is to put your pictureBox in a panel and set the panel.AutoScroll=true. Also you need to set pictureBox.SizeMode=AutoSize so it is sized equal to the size of the image that it contains. Check: PictureBoxSizeMode Enumeration

Categories

Resources