Windows Form refresh datagridview - winforms

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();
}
}

Related

Send TextBox text to user control

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
}
}

Visual Studio 2012 Windows Form application

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

Is there a way to keep additional windows active when showing a modal window?

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.

Using a UserControl to Login and then enable menuStrip in Primary form

Ok so here is what i am trying to do.
I have a Primary form in a C# desktop application in which i have a menuStrip and a splitContainer.
On running the application, I am loading a UserControl named 'Login' to the splitContainer.Panel2 while hiding the menuStrip. The Login control contains fields for Username, Password and a button to log in.
http://i.stack.imgur.com/5jcnK.png
Once authenticated (on click of the button) i want to enable the menuStrip and allow other UserControls in the splitContainer.Panel2, while hiding the Login control.
http://i.stack.imgur.com/lwLvP.png
How to i achieve this? I was trying to fire up an event from Login control and somehow make it work in Primary form but unable to implement.
Is this approach even worth trying or should i open multiple forms separately (i would hate to do so!)
Any cleaner approach on how to change views in splitContainer.Panel2 (other than stacking panels one above the other, which would be a design nightmare for me) while keeping splitContainer.Panel1 with same content.
Your attempt sounds like it should have worked.
Are you adding the Login control at design-time or via code?
Here's an example of it being created thru code...worked fine for me.
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
menuStrip1.Visible = false;
Login login = new Login();
login.Authenticated += new EventHandler(login_Authenticated);
splitContainer1.Panel2.Controls.Add(login);
}
void login_Authenticated(object sender, EventArgs e)
{
Login login = (Login)sender;
// ... possibly extract some info from "login" ...
menuStrip1.Visible = true;
login.Dispose();
}
}
Login UserControl:
public partial class Login : UserControl
{
public Login()
{
InitializeComponent();
}
public event EventHandler Authenticated;
private void btnLogin_Click(object sender, EventArgs e)
{
if (true) // if they have authenticated
{
if (Authenticated != null) // only raise the event if we have subscribers
{
Authenticated(this, new EventArgs());
}
}
}
}

Custom Item Template Wizard button click doesn't fire?

I am following this exactly:
http://msdn.microsoft.com/en-us/library/ms185301.aspx
but can't get it to work. The form appears when I try and add my new item, but when I input text and click the button, nothing happens.
For posterity's sake here is my code:
The non-empty methods in the Wizard class which extends IWizard
public void RunStarted(object automationObject,
Dictionary<string, string> replacementsDictionary,
WizardRunKind runKind, object[] customParams)
{
try
{
// Display a form to the user. The form collects
// input for the custom message.
inputForm = new UserInputForm();
inputForm.ShowDialog();
customMessage = inputForm.get_CustomMessage();
// Add custom parameters.
replacementsDictionary.Add("$custommessage$",
customMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// This method is only called for item templates,
// not for project templates.
public bool ShouldAddProjectItem(string filePath)
{
return true;
}
The user input form code:
public partial class UserInputForm : Form
{
private string customMessage;
public UserInputForm()
{
InitializeComponent();
}
public string get_CustomMessage()
{
return customMessage;
}
private void button1_Click(object sender, EventArgs e)
{
customMessage = textBox1.Text;
this.Dispose();
}
}
And the button is indeed named button 1:
this.button1.Location = new System.Drawing.Point(200, 180);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(100, 40);
this.button1.TabIndex = 0;
this.button1.Text = "Click Me";
this.button1.UseVisualStyleBackColor = true;
So I don't have much experience with Windows Forms (do web apps), but I am following the directions on MSDN and it's pretty clear cut. Any suggestions? Can anyone else get this to work?
Okay I figured it out. I had to add the event handler in the form's constructor manually:
public UserInputForm()
{
InitializeComponent();
button1.Click += button1_Click;
}
Why this isn't in the documentation on MSDN boggles my mind.
If you use the WinForms designer mode to drag your button from the Toolbox, and then double-clicked the button in the designer view, it would have added the event handler and stubbed that Click method for you. Just FYI.

Resources