I have a question and I am unsure on how to proceed and am seeking some directions.
Here's my scenario, I have a Form1 with a panel1, I can load 3 different User Controls inside panel1 (UserControl1, UserControl2 and UserControl3) inside each one of these user controls I can open Form2 which has a few TextBoxes.
What I need is to whenever I hit a button on my Form2 all the TextBox text be sent to the User Control that opened Form2.
I'm not sure if my question here is clear, if anyone can help me with that I appreciate, thanks.
It depends how you are creating the new form. Check this it contains the two scenarios.
Child Form:
public partial class Child : Form
{
public event NotifyParentHandler NotifyParent;
public delegate void NotifyParentHandler(string textValue);
public Child()
{
InitializeComponent();
}
private void btnNotify_Click(object sender, EventArgs e)
{
//assuming that you want to send the value when clicking a button
if (this.NotifyParent != null)
{
this.NotifyParent(textBox1.Text);
}
}
}
Parent Form
public partial class Parent : Form
{
private Child childForm;
public Parent()
{
InitializeComponent();
}
private void btnOpenChildForm_Click(object sender, EventArgs e)
{
// Open the child form
childForm = new Child();
childForm.NotifyParent += childForm_NotificationTriggered;
childForm.ShowDialog();
}
void childForm_NotificationTriggered(string textValuePassed)
{
//here you can do anything
}
}
Related
I am working on Windows Form application on Visual Studio 2012. I have 2 forms.
Add_Item_to_DB1
Add_Item_to_DB2
Both of these forms call a third form called SUBMIT. Now, based on where this form is being called from, it has to submit information to a different database. Everything else in the SUBMIT form is EXACTLY the same except, data is inserted to a different database.
Is there a way to find out where the form is being called from? Kinda new to Form applications.
Thank you
If you open the SUBMIT form with the ShowDialog() method you will be able to determine the form that opened the SUBMIT form via the Owner property. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ShowDialog(this);
}
}
public partial class SUBMIT : Form
{
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = this.Owner.Text;
}
}
Alternatively you can expose a public property on your SUBMIT form that can be populated from the parent form. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ParentName = "Add_Owner_To_Db_1";
submitForm.Show();
}
}
public partial class SUBMIT : Form
{
public string ParentName { get; set; }
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = ParentName;
}
}
HTH
I'm afraid the answer is probably no...but some background. To draw a custom border on a window where the sizing logic works beyond the visible border (as it does on windows 10) I added layered windows around the edges to capture the messages and then forward them to the central window. This worked great until the form was shown modaly, at which point all the edge windows were automatically disabled. Obviously this is by design...but I'm not sure if there is some way around it. I tried making the edge windows owned by the central window, but that didn't work.
Or maybe there is a better approach entirely.
Here's a sample of the issue:
public partial class Form1 : Form
{
public Form1()
{
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Form f2 = new Form();
f2.Text = "Non Modal";
f2.Show();
Form f3 = new Form();
f3.Text = "Modal";
f3.ShowDialog(this);
}
}
I think you can fake the modal window, so that it is not modal but disable the caller. I used this in a own project. I did it this way:
//Setup small Interface
public interface IDialog
{
//Our own Event which tell the caller if the Dialog is active/inactive
public event DialogChangedEventArgs DialogChanged;
}
//Setup EventArgs for our own Event
public class DialogChangedEventArgs : EventArgs
{
public bool DialogActive{get;}
public DialogChangedEventArgs(bool dialogActive)
{
DialogActive = dialogActive;
}
}
//Setup the Form which act as Dialog in any other form
public class Form2 : Form, IDialog
{
public event EventHandler<DialogChangedEventArgs> DialogChanged;
//If this Form is shown we fire the Event and tell subscriber we are active
private void Form2_Shown(object sender, EventArgs e)
{
DialogChanged?.Invoke(this, true);
}
//If the user close the Form we telling subscriber we go inactive
private void Form2_Closing(object sender, CancelEventArgs e)
{
DialogChanged?.Invoke(this, false);
}
}
public class Form1 : Form
{
//Setup our Form2 and show it (not modal here!!!)
private void Initialize()
{
Form2 newForm = new Form2();
newForm.DialogChanged += DialogChanged;
newForm.Show();
}
private void Form2_DialogChanged(object sender, DialogChangedEventArgs e)
{
//Now check if Form2 is active or inactive and enable/disable Form1
//So just Form1 will be disabled.
Enable = !e.DialogActive;
}
}
It's really simple. Just use an event to tell your first Form: Hey iam second Form and active. Then you can disable the first Form with while second is active. You have the full control which forms are active or not. Hope this helps.
I am fairly new to c# and Windows forms. My problem is I need to refresh a bound datagridview when I add an appointment or on a timer if no appointment is entered. I have three forms. Form 1 is log in screen which opens and populates the appointments in Form 3. On enter of space bar opens Form 2 which is populated with appointment information. On button click to save Form 2 I need Form 3 to refresh. This is my first post, hope I am making sense, I have tried to solve this for weeks. Thanks in advance for any help.
In Form2, add this for your button clicked event handler which will call back to your Form3 singleton instance:
private void button1_Click(object sender, EventArgs e)
{
Form3.Instance.RefreshGrid();
}
Then in your Form3, you need a property to hold the singleton instance of the form, and a method to refresh the grid:
public partial class Form3 : Form
{
private static Form3 _instance;
public static Form3 Instance
{
get { return _instance; }
}
public Form3()
{
if (_instance == null)
{
_instance = this;
}
InitializeComponent();
}
public void RefreshGrid()
{
this.dataGridView.Refresh();
}
}
I am trying to update a label control, on a "WPF" page, from my own .cs file but not updating .Please help me.I have a page "Measurements.xaml" ,after page load,in button click create object for my own cs file(sample.cs) and called a method in the cs file.In that method i am trying to update lable control on page (Measurements.xaml),as below
((Measurements)System.Windows.Application.Current.MainWindow).label1.Content = "153";
Measurements.xaml is not a window.
Not sure what you have done or how, but this should work:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_OnClick(object sender, RoutedEventArgs e)
{
var sample = new Sample();
sample.UpdateLabel();
}
}
class Sample
{
public void UpdateLabel()
{
var main = (MainWindow) Application.Current.MainWindow;
main.Label1.Content = "153";
}
}
I have an array of pictureboxes named from B11 (co-ords 1,1) to B55 (co-ords 5,5). I would like to hide these all on startup (and in the middle of running). I was thinking of making an array of the names manually but would it be the best solution?
If they all have a common parent control, such as a panel or groupbox (or even the form):
Parent.SuspendLayout()
For Each pbox As PictureBox in Parent.Controls.OfType(Of PictureBox)()
pbox.Visible = False
Next pbox
Parent.ResumeLayout()
The Suspend/Resume-Layout() is to avoid flickering as you modify a bunch of controls at once.
You could extend the PictureBox class and use event handling to accomplish this by:
Adding a public property to the form to tell if the picture boxes should be shown or hidden.
Adding an event to the form that is raised when the show/hide picture box property is changed.
Extending the PictureBox class so that it subscribes to the event of the parent form.
Setting the visible property of the extended PictureBox class to the show/hide property of the parent form.
When the show/hide flag is changed on the parent form all of the picture boxes will change their visibility property accordingly.
Form Code:
public partial class PictureBoxForm : Form {
public PictureBoxForm() {
InitializeComponent();
this.pictureBoxesAdd();
}
private void pictureBoxesAdd() {
MyPictureBox mp1 = new MyPictureBox();
mp1.Location = new Point(1, 1);
MyPictureBox mp2 = new MyPictureBox();
mp2.Location = new Point(200, 1);
this.Controls.Add(mp1);
this.Controls.Add(mp2);
}
public event EventHandler PictureBoxShowFlagChanged;
public bool PictureBoxShowFlag {
get { return this.pictureBoxShowFlag; }
set {
if (this.pictureBoxShowFlag != value) {
pictureBoxShowFlag = value;
if (this.PictureBoxShowFlagChanged != null) {
this.PictureBoxShowFlagChanged(this, new EventArgs());
}
}
}
}
private bool pictureBoxShowFlag = true;
private void cmdFlip_Click( object sender, EventArgs e ) {
this.PictureBoxShowFlag = !this.PictureBoxShowFlag;
}
}
Extended PictureBox Code:
public class MyPictureBox : PictureBox {
public MyPictureBox() : base() {
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.ParentChanged += new EventHandler(MyPictureBox_ParentChanged);
}
private void MyPictureBox_ParentChanged( object sender, EventArgs e ) {
try {
PictureBoxForm pbf = (PictureBoxForm)this.Parent;
this.Visible = pbf.PictureBoxShowFlag;
pbf.PictureBoxShowFlagChanged += new
EventHandler(pbf_PictureBoxShowFlagChanged);
} catch { }
}
private void pbf_PictureBoxShowFlagChanged( object sender, EventArgs e ) {
PictureBoxForm pbf = (PictureBoxForm)sender;
this.Visible = pbf.PictureBoxShowFlag;
}
}
...or just put 'em all on a Panel, and change the panel's visibility.