Populate from a child form to an another childform - c#

In my project, I have a mdi container mdiMother whith two childform (mdiChild1 and mdiChild2). The mdiChild1 have a listview (mode view detail). The mdiChild2 have a button who goal to populate the listview in mdiChild1.
I have done some peace of code. I copy this code into the mdiChild1, create a button, try it and it s works (mean: I see the listview populate).
My problem:
When I press button from mdiChild2, my listview isn't populate with same code in mdiChild1 the code do the job.

You must control child's controls from mdiMother.
Create static instance from each mdiChild, after that when you want to call method from each of them you must use from these static instances.
Like this:
public class mdiMother{
public static mdiChild1 ch1;
public static mdiChild2 ch2;
public mdiMother(){
ch1 = new mdiChild1();
ch2 = new mdiChild2();
}
}
public class mdiChild1{
public void Do(){
// fill list
}
}
public class mdiChild2{
public void CallMdiChild1(){
mdiMother.ch1.Do();
}
}

Thank to all. I solve my problem and I see it was so stupid. Tha way it's didn't populate: dont refere to the good form. For doing the job, i make a ref to the active mdi child ... and it's work :)
Thanx all for your help... and many thanx to Ahmad who's giving to me the good way to find the solve :p

Related

C# access Textbox of parent form from Listview control class

I created custom Listview and for usage I need to access Textbox on same Form where Listview is. Currently what I do everywhere in my Listview code is this:
Form frm = FindForm();
var text_ctl = frm.Controls.Find("Textbox1", true).FirstOrDefault() as Control;
TextBox Txt = (TextBox)text_ctl;
...
Txt.Text="Test";
But instead of repeating same code over and over I want to do It only once, like in OnCreateControl() and pass that reference to everywhere I need It in my Listview class. What is the easiest or most elegant solution for this ? Thanks for help in advance !
I complicated too much, here is what I did:
private TextBox _Txt;
public void GetTxt(TextBox ref_txt)
{
_Txt = ref_txt;
}
I run GetTxt() after Form loads, then listview class has reference to It.
alternative using property:
private TextBox _Txt;
public TextBox GetTxt
{
get { return _Txt; }
set { _Txt = value; }
}
Thanks for help, specially Sandeep. And sorry for any inconvenience, I got confused a bit...

Windows Forms - ErrorProvider overlapping icons

I'm using a custom icon in my ErrorProvider with
ErrorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink
I've got a problem with overlapping icons using code that is similar to this one:
public partial class TestForm : Form
{
private ErrorProvider _errorProvider1;
private ErrorProvider _errorProvider2;
private CheckBox _control1;
private CheckBox _control2;
//...
private void ValidateAll()
{
_errorProvider1.Clear();
_errorProvider2.Clear();
_errorProvider1.SetError(_control1, string.Empty);
_errorProvider2.SetError(_control2, string.Empty);
if(Validate(_control1.Checked))
{
_errorProvider1.SetError(_control1, "Error1");
}
if(Validate(_control2.Checked))
{
_errorProvider2.SetError(_control2, "Error2");
}
}
//...
}
I'm interacting with _control1 while _control2 has some error (is Checked), thus _errorProvider2 has some error set. Everytime method ValidateAll is called it will correctly set _errorProvider1 for _control1 but _control2 _errorProvider2 will keep drawing extra icons without erasing old ones.
Starting view
After 'clicking' _control1 multiple times
When interacting with _control2, _errorProvider2 will go back to normal, but _control1's _errorProvider1 will do the same until it is 'clicked'.
After 'clicking' _control2
Please note that underlying Control doesn't affect it; it doesn't have to be CheckBox.
What I tried:
Using only one ErrorProvider per Form/Control,
Focusing each Control before setting ErrorProvider,
Not clearing ErrorProviders
Any help is much appreciated, thanks!

How to add a ListBox programmatically in a Form

I started out C# very recently and sorry if this question sounds dumb.
How do I add a Listbox in a Form that pops out from a Button click?
Note: The Form isn't the one that's added from the Solution Explorer whereby I can just drag a Listbox from the Toolbox to my Form.
So what I want is to create a ListBox in my file drawer1Form where I can add additional items. Thanks for the help in advance!:)
private void drawer1button_Click(object sender, EventArgs e) // Drawer 1 Button
{
drawer1Form df1 = new drawer1Form();
df1.StartPosition = FormStartPosition.CenterScreen;
df1.Show();
}
public partial class drawer1Form : Form // Creates drawer1Form
{
public drawer1Form()
{
Text = "Drawer 1 ";
}
}
Pretty much the same way as you'd do with any other object.
In the class of your form add a
private ListBox myAwesomeListBox;
Then in the button event handler add something like this:
myAwesomeListBox = new ListBox();
myAwesomeListBox.SuspendLayout();
// set all the properties that you want
myAwesomeListBox.Name = "myAwesomeListBox";
myAwesomeListBox.Location = new Point(...); // place it somewhere
myAwesomeListBox.Size = new Size(...); // give it a size
// etc...
df1.Controls.Add(myAwesomeListBox);
myAwesomeListBox.ResumeLayout();
This should be it.
I highly advise you to do it through the designer first though, and then take a look at the generated code in the form's .Designer.cs file, you'll have a very good understanding after reading through that.

Should code be inside user control or the form using it

I have a small design question which I couldn't find relevant google hits for some reason.
I have a user control which I use in my application.
The main form opens a second form as a dialog. T
his second form is using the user control which includes a list box.
Naturally I want to preserve the list box items when the forms dispose so I am keeping a private list in the main form.
List<string> _listofFirstCoordinates = new List<string>();
Now the question is, should the dialog form be the one responsible for relaying the list to the main form or should the code be in the user control?
Should the one populating the list be the user control
lst_Coordinates.Items.AddRange(ListOfCoordinates.Cast<object>().ToArray());
or should the form using it populate it (The subform)
uc_EditCoordinates.ListOfCoordinates = ListOfCoordinates;
Also is it feasible to just have the user control be a public variable for the form holding it so it may be changed directly or would that be bad design?
Edit:
By the way, the data is saved for now in variables going back and forth between the forms as the user has to finish all subforms before submitting and finally saving it to the database. So it is a
var _listofFirstCoordinates = new List<string>();
going back and forth.
The "correct" solution is to abstract-away the View-level concern (in this case, anything to do with Form, UserControl, and UI controls) away from the Controller and Model-level concerns (in this case, your application's data).
Without completely rearchitecturing your system, you can still apply this separation-of-concerns within your example.
You can conceptually argue the "code-behind" of your MainForm class acts as a kind of Controller (purists would disagree). It will have to know about creating the child form, but it does not need to know about the user-control hosted within the child form - that would be the concern of the child form's.
I suggest defining a class that represents a ViewModel - albeit as we're using WinForms we will use it as a kind of crude "one-way" ViewModel, like so:
class MainForm : Form {
private void ShowChildFormModal() {
ChildViewModel vm = new ChildViewModel();
vm.CoordinatesList = ...
vm.OtherData = ...
ChildForm child = new ChildForm();
child.LoadFromViewModel( vm );
child.ShowDialog();
child.SaveToViewModel( vm );
SaveToDatabase( vm );
}
}
class ChildViewModel { // this is a POCO
public List<String> CoordinatesList;
public Int32 OtherData;
}
class ChildForm : Form {
public void LoadFromViewModel(ChildViewModel vm) {
// save time and trouble by using the List as a datasource directly, or you can manually populate the combobox as well
this.childUserControl.LoadFromViewModel( vm );
this.someOtherControl.Value = vm.OtherData;
}
public void SaveToViewModel(ChildViewModel vm) {
// completing this is an exercise for the reader
// but basically copy values from the controls on the form into the `vm` instance
}
}
class ChildUserControl : UserControl {
public void LoadFromViewModel(ChildViewModel vm) {
this.comboBox.DataSource = vm.CoordinatesList;
}
}

Adding Items to a ListBox

I have a WPF main window, which contains a toolbar with buttons and a tabcontrol that is displaying a page with a listbox. The page is hosted on a frame, and the frame is set on the tab I selected.
When I click on a button on my toolbar, a new window pops up with a textbox and a submit button. When I press the submit button, I want to insert the textbox contents into the listbox that's on the main window. However, nothing displays in the listbox. I tried listbox.Items.Add() but it still won't display.
public partial class AddNewCamper : Window
{
public AddNewCamper()
{
InitializeComponent();
}
private void btnNewSubmit_Click(object sender, RoutedEventArgs e)
{
CampersPage c;
// Converting string to int b/c thats what camper() takes in.
int NewAge = Convert.ToInt16(txtNewAge.Text);
int NewGrade = Convert.ToInt16(txtNewGrade.Text);
// Create a new person
Camper person = new Camper(NewAge, NewGrade, txtNewFirstName.Text);
txtNewFirstName.Text = person.getName();
// Trying to add the first name of the person to display on the listbox of another window.
c.testListBox.Items.Add(txtNewFirstName.Text);
}
You can follow any of the following approaches. But based on your comments I guess solution 3 suits you.
1) Try initializing c first. You can't use an object without allocating memory for it.
2) If you want to use the same object, use the reference of the object created in the MainWindow
in the required class.
something like this should work:
CampersPage c = [reference to CampersPage object in MainWindow]
then add items to your listbox
3) If you want to use the listbox object, make your CampersPage Class static.
Making it static would not require you to initialize your class explicitly.
public static CampersPage {
// do something here
}
Make sure that you declare your listbox in CampersPage as public.
Then in the class requiring your listbox defined in CampersPage, do the following
CampersPage.testListBox.Items.Add(txtNewFirstName.Text);
4) If the classes are in the same namespace, you can declare listbox as a global public property and access it from rest of the classes in the same namespace.

Categories

Resources