I am a developing a c# app that will be used in a touch computer. It has a secret combination to close it in case something goes wrong. TopRight, TopLeft, TopRight.
To achieve that, I created a class that add two panels to the form:
public enum Location
{
Top,
Bottom
};
public SecretCode(Form form, int boxSize, int resetTimer, Location location, bool debug)
{
timerReset.Interval = resetTimer;
timerReset.Tick += TimerReset_Tick;
Panel panelRight = new Panel();
if (debug) panelRight.BackColor = Color.Yellow;
panelRight.Size = new Size(boxSize, boxSize);
panelRight.Location = new Point(form.Width - boxSize, location == Location.Top ? 0 : form.Height - boxSize);
panelRight.Click += PanelTopRight_Click;
Panel panelLeft = new Panel();
if (debug) panelLeft.BackColor = Color.Red;
panelLeft.Size = new Size(boxSize, boxSize);
panelLeft.Location = new Point(0, location == Location.Top ? 0 : form.Height - boxSize);
panelLeft.Click += PanelTopLeft_Click;
form.Controls.Add(panelRight);
form.Controls.Add(panelLeft);
panelLeft.BringToFront();
panelRight.BringToFront();
}
On my main form, I have different panels (they are all fullscreen) that I show / hide. Every panel has its own background image.
Panel Intro
Panel Signup
When the user finishes reading the intro, the whole panel disappears and a the Signup Panel shows up.
Is there any easy way to ensure that the SecretCode panels would always be transparent or do I need to change its parent every time a show a new panel?
Related
I created a WPF Popup Control on my WPF Form.When I click,the Popup Opens and it also moves properly. but when I Clicked on textbox in popup window then popup moves slightly towards the edge of main window. I don't want to move popup when I clicked on textbox in Popup?
private void MyAddJobPopup_MouseMove(object sender,System.Windows.Input.MouseEventArgs e) {
if (!MyAddJobPopup.IsOpen)
MyAddJobPopup.IsOpen = true;
if (e.LeftButton == MouseButtonState.Pressed) {
var mousePosition = e.GetPosition(this.HardwareElectricalTbl);
this.MyAddJobPopup.HorizontalOffset = mousePosition.X;
this.MyAddJobPopup.VerticalOffset = mousePosition.Y;
}
}
When setting the HorizontalOffset and VerticalOffset they use the top left location of the window, which is not the location you want to set it to, you want to set it to the mouse position minus the mouses previous relative position to the window.
To do this save the following locations when you start clicking (for example inside of an onClick event):
//both should be negative so adding will actually remove from the location which is what you want
float offsetX = this.MyAddJobPopup.HorizontalOffset - mousePosition.X;
float offsetY = this.MyAddJobPopup.VerticalOffset- mousePosition.Y;
then when you set the location add them:
this.MyAddJobPopup.HorizontalOffset = mousePosition.X + offsetX;
this.MyAddJobPopup.VerticalOffset = mousePosition.Y + offsetY;
I am trying to build a piano and i have a panel that is designed to house the musical staffs as a background image of this panel.
I have tried to apply these as a Picture Box but this hindered a lot of my other features of this piano. These are the current settings that are related to this particular panel:
this.BorderStyle = BorderStyle.FixedSingle;
this.Size = new Size(750, 75)
this.Location = new Point(125, 75);
this.Anchor = AnchorStyles.None;
this.BackgroundImage = Image.FromFile("..\\..\\..\\Notes-Images\\Notes-Images\\staff2.bmp");
this.Visible = true;
I need to find a way how to actually have control on the location of where the background image is placed so as to lower it from the top left of the panel
I am creating a C# form application. I wanted to show the initial form on a second screen. I want to open the other form pages on the first screen. There is a resolution difference between two screens. How can I show the initial form on the second screen and the other five forms on the main screen?
In matter of window positioning there are no "two screens" but just one "working area".
Meaning if you have two FullHD screens next to each other you have a working area of 3840x1080 (minus some for taskbar and such).
If you then place a window at Left = 200 and Top = 100 it will be placed 200 pixels to the left side of the left screen and if you place it at Left = 2120 and Top = 100 it will be placed at the same position on the right screen.
And for all that to work you need to use StartPosition = FormStartPosition.Manual.
I've used this method, hope it helps you:
public void MaximizeToMonitor(Form frm, int monitorIndex)
{
try
{
Screen screen = Screen.AllScreens[monitorIndex];
if(screen != null)
{
frm.WindowState = FormWindowState.Normal;
var workingArea = screen.WorkingArea;
frm.Left = workingArea.Left + 10;
frm.Top = workingArea.Top + 10;
frm.Width = workingArea.Width + 10;
frm.Height = workingArea.Height + 10;
frm.WindowState = FormWindowState.Maximized;
}
}
catch (Exception ex)
{
MessageBox.Show($"Monitor does not exists. {Environment.NewLine}{ex.Message}");
}
}
I have created a custom ListView control to suit my needs and I'm having an issue that causes the ListView not to show any contents (not drawing anything, just white) when the form first loads.
If I resize the form or click on my control (anything that forces a repaint on the ListView) then it shows up as expected.
As a side note, it used to work just fine until I made a small change today and rebuilt the control. I removed all the changes I made and rebuilt again but the issue still happens. Any ideas as to why it will not show up (paint) when the form is first loaded?
This is what I use to do the custom drawing on my custom ListView control...
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
Image image = e.Item.ImageList.Images[e.Item.ImageIndex];
Size textSize = new Size((int)e.Graphics.MeasureString(e.Item.Text, e.Item.Font).Width, (int)e.Graphics.MeasureString(e.Item.Text, e.Item.Font).Height);
//Get the area of the item to be painted
Rectangle bounds = e.Bounds;
bounds.X = 0;
bounds.Width = this.Width;
//Set the spacing on the list view items
int hPadding = 0;
int vPadding = 0;
IntPtr padding = (IntPtr)(int)(((ushort)(hPadding + bounds.Width)) | (uint)((vPadding + bounds.Height) << 16));
SendMessage(this.Handle, (uint)ListViewMessage.LVM_SETICONSPACING, IntPtr.Zero, padding);
//Set the positions of the image and text
int imageLeft = (bounds.Width / 2) - (image.Width / 2);
int imageTop = bounds.Top + 3;
int textLeft = (bounds.Width / 2) - (textSize.Width / 2);
int textTop = imageTop + image.Height;
Point imagePosition = new Point(imageLeft, imageTop);
Point textPosition = new Point(textLeft, textTop);
//Draw background
using (Brush brush = new SolidBrush(e.Item.BackColor))
e.Graphics.FillRectangle(brush, bounds);
//Draw selected
if (e.Item.Selected)
{
using (Brush brush = new SolidBrush(m_SelectedColor))
e.Graphics.FillRectangle(brush, bounds);
}
//Draw image
e.Graphics.DrawImage(image, imagePosition);
//Draw text
e.Graphics.DrawString(e.Item.Text, e.Item.Font, new SolidBrush(e.Item.ForeColor), textPosition);
}
I also set the following things in the Constructor of my custom control...
public MyListView()
{
this.DoubleBuffered = true;
this.OwnerDraw = true;
this.View = View.LargeIcon;
this.Cursor = Cursors.Hand;
this.Scrollable = false;
}
I also inherit the ListView class...
public class MyListView : ListView
{
//All my source
}
You need to set the set the control to redraw itself when resized. So add this code in constructor of your control:
this.ResizeRedraw = true;
My apologies:
Doing the below reset my event handlers and the problem went away. However, once I hooked them up, I found the Resize event handler that I used to set the ColumnWidth was causing the issue. Why setting the ColumnWidth is causing this, I do not know.
Arvo Bowen's comment on this answer fixed it for me also (.NET 4.8 Framework, VS2022). To be clear, not requiring this.ResizeRedraw = true; from the answer.
So after much headache and time I found that I had absolutely nothing wrong with my control. It was your answer that made me create another control just like my existing one and test it. To my surprise it worked great. I simply copied my existing non-working control and pasted it on the form and then new one worked! Sometimes VS just does weird things... Somehow I managed to muck up the control's creation object or something..
I hope this is an easy task, nonetheless I need some help.
SomeForm f = new SomeForm();
if (someStatement) // relocate pnlRight onto new form f
{
this.Controls.Remove(pnlRight);
f.Controls.Add(pnlRight);
pnlRight.Location = new Point(0, 0); // I want to place pnlRight at (0, 0) on the new form f
this.pnlRight.Visible = false;
f.Show();
}
else // hide form and relocate pnlRight onto main form
{
this.pnlRight.Location = new Point(pnlLeft.Location.X + pnlLeft.Width + 10, pnlLeft.Location.Y);
this.Controls.Add(pnlRight);
this.pnlRight.Visible = true;
frmSettings.Hide();
}
So - I have a main form with two panels: pnlLeft and pnlRight. On button click (here if someStatement is true) pnlRight should be relocated on a newly created form. Works perfectly. BUT: If I relocate pnlRight again onto the main form, it is NOT placed where I want it to be, but instead at (0, 0) above pnlLeft. What's wrong? I checked both panel's properties and can't see any odds. Maybe somebody has a hint for me,
Best
Dennis
If you have 2 panels in your form then you need to set the dock property of both the panels in the sequence you want to display them.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx
Particularly, "Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container."
You tried setting its location after you re-added onto the form?