I have a Main Screen form set up, with it's body consisting of one giant panel. I then have a series of User Controls which get erased or loaded into that Main Screen. What I don't know how to do is have the User Controls affect the Main Screen.
Code Example from a User Control:
PartsSystem parts = new PartsSystem(); //a user control
MainMenu.pnlMain.Controls.Clear();
MainMenu.pnlMain.Controls.Add(parts);
This code works when loading the main screen, but I can't seem to access pnlMain from my other user controls. Is it possible to fix this? I have a function set up to grab a user Control within the MainMenu form, but I can't seem to get it to be called from a user control. I'm trying to make it so that when they click a button, they go to the next screen (user control).
I am using C# on Windows 7 with Visual Studio 2010 and all the default settings.
Here is some additional code samples:
public partial class Main_Menu : Form
{
public Main_Menu()
{
InitializeComponent();
MainMenuPanel main = new MainMenuPanel();
panelChange(main);
}
public void panelChange(UserControl newPanel)
{
pnlMain.Controls.Clear();
pnlMain.Controls.Add(newPanel);
}
}
That was Main_Menu. This code works on launch. Now, bringing up my PartsSystem is another issue.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Program_v1._0._0._0._4
{
public partial class MainMenuPanel : UserControl
{
public MainMenuPanel()
{
InitializeComponent();
Main_Menu parentForm = (this.Parent as Main_Menu);
}
public void btnPartsInventory_Click(object sender, EventArgs e)
{
Main_Menu myParent = (this.Parent as Main_Menu);
PartsSystem parts = new PartsSystem();
myParent.pnlMain.Controls.Clear(); //Object reference not set to an instance of an object.
//This is the error I get at this point. It won't let me use the function.
myParent.pnlMain.Controls.Add(parts);
}
}
}
Thank you for the help!
Try using UserControl.Parent to get access the parent control.
access form from usercontrol
Get access to parent control from user control - C#
Related
I am working on an Outlook 2016 VSTO plugin and I'd like to add a WPF window display functionality to a button. So now my code looks something like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows;
namespace TaskManager
{
public partial class RibbonTaskManager
{
private void RibbonTaskManager_Load(object sender, RibbonUIEventArgs e)
{
}
private void ButtonAddGroups_Click(object sender, RibbonControlEventArgs e)
{
FormAddGroups formAddGroups = new FormAddGroups();
formAddGroups.Show();
}
}
}
The problem is that apparently formAddGroups does not contain a definition for "Show". I tried looking for possible missing references to no avail.
I added
System.xaml
WindowsFormsIntegration
PresentationCore
PresentationFramework
I also found this thread, but the accepted answer solves a different problem, I think.
You can't call Show() on a UserControl but could create a window and set its Content property to an instance of your UserControl:
var window = new System.Windows.Window();
window.Content = new FormAddGroups();
win.Show();
I'm trying to create PictureBoxes dynamically at runtime with c# winforms.
My project: I want to write a program, which has a node-GUI (a GUI with various types of nodes, some kind of boxes, which are connected together and process an image, an audio stream or whatever).
Therefor i want to create and delete Pictureboxes dynamically at runtime, but my testing won't work, the form is empty.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AudioNodeGUI
{
public partial class AudioNodeWindow : Form
{
public AudioNodeWindow()
{
InitializeComponent();
}
private void AudioNodeWindow_Load(object sender, EventArgs e)
{
}
private void AudioNodeWindow_Paint(object sender, PaintEventArgs e)
{
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"C:\Users\Benjamin.MBENJAMIN\Pictures\Start.png"),
};
start_picture.Show();
}
}
}
Please help !
You need to add the control you created to the Forms control.
Before you Show() the picturebox, try adding this line:
Controls.Add(start_picture);
Secondly, you don't want to doing this onPaint()!
I would say you need to move it to Load() method instead, that way it will be done when the form loads, rather than everytime it's repainted!
Change:
start_picture.Show();
to:
this.Controls.Add(start_picture);
start_picture.Show();
Controls.Add tells the form that the PictureBox is meant to be part of this specific form.
Also, you will not want to do this in in your Paint event handler. Leaving it there will result in many more picture boxes than you would like I expect...
I have change your code as :
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"D:\test\learn.png"),
};
//start_picture.Show();
Controls.Add(start_picture);
First of all, beginner here, apologies in advance if my question has obvious answer, but until now, I didn't manage to figure it out.
So, I'm trying to make an Windows Form App. In this app I have two forms: the login form and main window form. To make it look more "fancy", after successfully logging in, the main window form must increase its size (same size as the login form) to something bigger. I managed to create the code I need, but the resize effect looks "laggy". Is there a way to make this smother? Or is there another way to make the resize of the form smoother?
PS. I have set the "FormBorderStyle" property to "None"
Bellow, my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SlindingPanel
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
// Enables the timer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width >= 820 && this.Height >= 540) this.timer1.Enabled = false;
else {
// Resizes the form
this.ClientSize = new System.Drawing.Size(Width + 20, Height + 20);
//Centers the form on the screen
this.CenterToScreen();
}
}
}
This may be late, but I suggest using the "anchor" attribute of the UI controls.
I have a little C# program with TextBox and other components. When I examing the TextBox (or other) components with WinSpy++, I get a Control ID which I cannot find at my C# source code. For example - see screenshots: When I examine the TextBox field with number 5, WinSpy++ detects the Control ID 000209AA.
How identifies WinSpy the Control ID? Is there any possibility to set the Control ID at the C# source code?
Cheers,
Achim
GetDlgCtrlID from user32.dll will provide the control ID given the window handle (ie, Control.Handle). Very simple form to demonstrate:
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;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern int GetDlgCtrlID(IntPtr hwnd);
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Control ID: " + GetDlgCtrlID(textBox1.Handle));
}
}
}
I'm not sure that there's any way to set the control ID - I suspect the control ID is set automatically by Windows Forms somehow in the form construction. But being able to retrieve it should be sufficient for most applications (eg direct use of PostMessage/SendMessage from Windows API.)
I have a windows form project, and I want the whole form to change location automatically, but the truth is that I have no idea what to call, and where to call it. I have searched online, and all code I discovered was incomplete. I am fairly new at this, so it did not help me.
Here is the code that I am working with, if it helps:
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;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SoundPlayer _soundplayer;
public Form1()
{
InitializeComponent();
SoundPlayer player = new SoundPlayer(Properties.Resources.sound);
player.Play();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_soundplayer.PlayLooping();
}
}
}
Change the Location for the form:
this.Location = new Point(400, 500);
You just need to decide which event will trigger this code; for example, the Click event of a button.
MSDN: Location
To position forms using the Properties window
In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual.
Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area.
Note Expand the Location property to enter the X and Y subproperty values individually.
Reference MSDN