Retrieving Property from another WPF form - wpf

I am a beginner in WPF and trying to learn,So Sorry for this type of query.
I have added a property named GetName in WPF Form1 as below:
public string GetName
{
get { return this.uname.Text; }
set { this.uname.Text = value; }
}
Trying to retrieve the GetName property in WPF Form2 as below:
public Home()
{
InitializeComponent();
Form1 mn = new Form1 ();
MessageBox.Show(mn.GetName.ToString());
}
But I am getting prompt with blank. What I am doing wrong here. Please help.

You create a new Form:
Form1 mn = new Form1 ();
Then you request the Text value of what I assume is a TextBox control to be shown in a MessageBox:
MessageBox.Show(mn.GetName.ToString());
I would not expect GetNameto return a value, as when a TextBox control is created, unless specified, it will not have a value.
Between creating the Form and showing the MessageBox, you do not show your Form, for example:
mn.Show();
You need to show your Form first, input a value into your uname control and then request its value.
Calling ToString() on GetName is redundant, just use:
MessageBox.Show(mn.GetName);

Related

Issue when adding user control in data grid using wpf programatically

I have created user control and adding to data grid using code behind . For user control i have created 2 constructor to pass the data for showing .
User control constructor:
public OverlayControlView()
{
InitializeComponent();
}
public OverlayControlView(string value, List<Function> functionList)
{
InitializeComponent();
OverlayValue = value;
OverlayMenuItem = functionList;
}
Adding user control to data grid:
var overlayControlView = new OverlayControlView(caption, functionList);
FrameworkElementFactory factory = new FrameworkElementFactory(overlayControlView.GetType());
var dataTemplate = new DataTemplate(typeof(DependencyObject));
dataTemplate.VisualTree = factory;
dataGridTemplateColumn.CellTemplate = dataTemplate;
dataGrid.Columns.Add(dataGridTemplateColumn);
Problem is after adding user control to grid , its always calling parameter less constructor and the value inside user control become empty. It call parameter constructor first when i am adding user control programmatically then it automatically calling parameter less constructor.
How to solve this issue!

Attach control to form from other class c#

In C# win forms- I would like to add controls to form from other class.
How can I do it?
I tried to pass the form as formal parameter to function in the other class, but how can I attach it to the form?
class Class1
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
}
In addition, I have Form1.cs
I would like to add txt to Form1.
In addition, I would like to set the properties of txt from Class1, and it failed..
Thanks!
This should work:
class Class1
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
public void AddTextBoxToForm(Form form)
{
form.Controls.Add(txt);
txt.Text = "Hello World! I've been added to a form.";
}
}
You may also set properties like Location and Size of the TextBox. Note that it will be a bad idea to add the TextBox to different forms, though.
If you have any errors, your question should be more specific about what "failed" means.
In general, all controls of a Form should rather be members of that Form and not be defined in other classes.

how can i pass value to a running form

I have 2 forms : Form1 and Form2
Form1 start running with Application.Run()
and Form1 have a datagridview if user Start Edit the DataGridView
i run Form2 like this in CellBeginEdit event
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
After Form2 finish the task, When i click a button on Form 2 i want to get value return to Form1 and show in the DataGridView on Form1 and close the Form2
i tried to pass the Form1's object
Form2 addStock = new Form2(productNo, stockString, this);
but on Form2 i can't access the Form1's DataGridview with that object
how can i do to accomplish this?
what i suggest made a custom class and make some static members and when you are navigating to form2 jux check them.
class CustomClass
{
public static int ProductNo;
public static string StockString;
}
//these are call to form2 from form1
CustomClasss.ProductNo=12; //some value
CustomClass.StockString="Some Value";
Form2 addStock = new Form2();
addStock.Activate();
addStock.Show();
now call these custom class members in form 2 and validate according to ur need..
like
if(CustomClass.ProductNo==2 && CustomClass.StockString=="Some String")
{
//do something
}
Hope it helpx..
Add public properties to Form2. After finishing the computation whatever you are doing within Form2 set the values to those properties. Now these values are accessible within From1 with the same From2 instance you have created to show the Form2.
Now within From1 access those property values of Form2.
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
//Reading the property value of Form2
string stockValue = addStock.StockStringProperty;

Cannot edit values of DataGridView bound to a BindingList

I have trouble editing a databound bindinglist. Let me illustrate it with the following:
Say I have the Person class:
public Class Person{
private string m_firstname;
private string m_lastname;
public string FirstName{get;set;}
public string LastName{get;set;}
public Person{ ... }
}
I then have a containing class called Population:
public class Population{
private BindingList<Person> m_lstPerson = new BindingList<Person>();
private string m_countryName;
public BindingList<Person> ListPerson{get; set;}
public string CountryName { get; set; }
}
I then have on one form a first datagridview with DataSource = m_lstPopulation (BindingList). The binding works like a charm when working with the Population objects. When I double click, it opens up a dialog form showing the object details. One tab in the details holds a datagridview bound to that population's ListPerson.
The second datagridview displays fine. However, I cannot edit or add cells in this datagridview. None of the columns is set to read-only. In fact, both datagridview have just about the same parameters.
What am I missing? It seems that a lock has been placed on the Population object so that its inner fields cannot be edited...
Please advise. Thanks.
First verify that these grid properties are set:
ReadOnly = false;
AllowUserToAddRow = true;
EditMode = ;
If that doesn't work then you may be getting stuck in edit mode... It sounds like you have some custom behavior on your grid ("When I double click, it opens up a dialog form showing the object details.")...
For this try calling DataGridView.CancelEdit() after your dialog closes to end the edit session on the clicked row. This will restore the "new row" row to the grid. It disappears when you begin editing another row, which, depending on the EditMode setting may begin when you click on (enter) another row.

Windows Forms Modal Dialog that returns an Object rather than DialogResult

I'm kinda stuck with this one so I hoped someone could help me.
I am doing a Winforms application and I need to show a Modal Dialog (form.ShowDialog) that returns a value (prompts the User some values and wraps them in a Object).
I just can't see how to do this rather than give a reference into the object or depending on some form of public Property to read the data afterwards.
I'd just like to have ShowDialog return something different, but that doesn't work.
Is thare some "good" way to do this?
I'm sure the problem isn't new, but since almost nobody seems to do Winforms any more I can't find any guidance on the web.
Add a static method to your form, like this:
public class MyDialog : Form
{
// todo: think of a better method name :)
public static MyObject ShowAndReturnObject()
{
var dlg = new MyDialog();
if (new dlg.ShowDialog() == DialogResult.OK)
{
var obj = // construct an instance of MyObject from dlg
return obj;
}
else
{
return null;
}
}
}
Now you can call this from your program thusly:
var myObject = MyDialog.ShowAndReturnObject();
... and if they cancel the dialog, myObject will be null.
Now, having said all that, I do believe that adding a property to your form's class which you then read from after calling ShowDialog() is the better approach.
You can create a public property inside the Dialog that represents the returning value:
/* Caller Code */
var dlg = new MyDialog();
if(dlg.ShowDialog() == DialogResult.OK)
MessageBox.Show(dlg.MyResult);
/* Dialog Code */
public string MyResult { get { return textBox1.Text; } }
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
Or you could create a new ShowDialog method inside your form class that does basically what Matt Hamilton's does. Maybe even an extension method if it's something you do to lots of forms in your problem.
The public property in the dialog form makes sense. However, do not close the dialog in the Ok button click event handler. When you assign the DialogResult property the dialog form will be hidden. Then in the calling form you can determine if Ok or Cancel was clicked by examining the DialogResult. Then you can access the public property if the Ok button was clicked and then dispose the dialog form. This should be done using a try-catch-finally block in the calling form or through a using statement. You must dispose of the modal dialog in order to prevent a memory leak.

Resources