Visual Studio 2012 Windows Form application - winforms

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

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

Windows Form refresh datagridview

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

Interaction between forms

I am using visual studio 2012 (windows form application) and I have two forms, one with a label and the other with a button. I want it so that when you click the button the label on the other form goes up by one. I already have:
Label1 = Label1 + 1
I just need to know how to make the connection with the two forms. Maybe call a function?
Btw I am new to the program and script so in simple terms plz.
Here is a sample I create for you. Add Fomr2 Like this:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1.Instance.Controls.Find("label1", true).First().Text = "Some thing";
}
}
And Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Instance = this;
}
private static Form1 _Instance;
public static Form1 Instance
{
get { return _Instance; }
set { Instance = value; }
}
private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}

Deleting from a Data Grid View

I have created a windows form application. I want this application to be able to use Linq to SQL to search for a record, and then for that record to be selected from a data grid view and deleted.
The form contains a textbox to enter the parameter, a search button and a delete button and a datagrid.
I have the search part working correctly and the data grid is populated but don't know how to implement clicking on the record in the data grid and deleting it.
Update - I have solved the solution. Changes have only been made to the btn_Delete_Click event handler so I have included the updated code for his button after the main code.
namespace DeleteForm
{
public partial class Form1 : Form
{
LinqtoStudentDataContext linqStud = new LinqtoStudentDataContext();
public Form1()
{
InitializeComponent();
}
private void btnDelete_Click(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
var lastName = from stud in linqStud.Students
where txtFind.Text == stud.LastName
select stud;
dataGridView1.DataSource = lastName;
}
}
}
Updated code -
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
//linqStud.Students.DeleteAllOnSubmit();
linqStud.SubmitChanges();
}
}
First, set selection mode of DataGridView to FullRowSelect. Next, when assigning DataSource you should call ToList() - you can't use query as data source:
private void btnSearch_Click(object sender, EventArgs e)
{
var lastName = txtFind.Text;
var students = from stud in linqStud.Students
where stud.LastName == lastName
select stud;
dataGridView1.DataSource = students.ToList();
}
Get selected rows, and remove databound items (students) from context:
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
var student = row.DataBoundItem as Student;
linqStud.Students.Remove(student);
linqStud.SaveChanges();
}
}

New tab in WPF - items.add from buttons vs methods

OK, so I have a window called PictureWindow which displays pictures (I've cut out the code not related to making tabs). The TabControl is named "itemsTab". Using a button press, I can make a new tab no problem. But using the same operations inside a called method doesn't work. Using the buttonTab_Click method makes a new tab, the newTab method does not.
The only real difference I can see is due to the sender and RoutedEventArgs objects - how do these effect the operation here? Or is there something else I'm missing?
Thanks in advance.
Edit To make things even stranger, the newTab method does make a new tab, but only if it is called in the PictureWindow constructor method. If I have the following a new tab is made.
public PictureWindow(string current)
{
InitializeComponent();
newTab(current);
}
But if I call the method anywhere else it doesn't work.
public partial class PictureWindow : Window
{
public PictureWindow(string current)
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonTab_Click(object sender, RoutedEventArgs e)
{
TabItem newTab = new TabItem();
newTab.Header = "New Tab!";
itemsTab.Items.Add(newTab);
}
public void newTab(string current)
{
TabItem newTab = new TabItem();
itemsTab.Items.Add(newTab);
}
}

Resources