C# How to dynamically creating a form and passing some parameters - c#

I'm dealing with an C# windows form application.
I know how to dynamically create and call a windows form.
I Know how to pass some parameters to a windows forms before calling them by Frm_Sample.show();
but I cant do these two together.
Why?
because when we have a normal windows form , we have access to Its form class and we are able to edit it but when we call it dynamically , we are not able.
As I know , If I want to pass some parameters to a windows form , before calling , I have to write some code like below inside my form class definition.
public string ReturnValue1 { get; set; }
public string ReturnValue2 { get; set; }
but if I call my form dynamically , how di I do that . Imagine this is my form creating code ...
Form aForm = new Form();
aForm.show();
How can I define some parameters inside aForm and pass my values before calling.
Thank you .
I have tried to add some attribute or method to my form class dynamically but I was not successful.
Thank you .

Related

How can I pass informations between the form and the user controls?

I have a problem with my project. Since I do not like it that every Windows Form is a new window I did try to use an different way.
This way I did create an FormGUI which contains the menu and calls the different UserControls. Since we did only learn how to use Windows Forms I do know how to work with them. There I would have changed the constructor of the new Windows Form to pass Data.
But this time I use mainly one Windows Form and different UserControls.
e.g. I have one to add new data, an other one to show data in a datagrid and one to show the data inside of an chart. At least this is the result.
But I do not finde a way to transport informations from the Windows Form into one User Control and how to get them back. In order to use this knowlegde somewhere else.
Does someone knew a tutorial where I can see how it could work? Or could explain it to me.
Since the UI is part of the end project that will be marked I would prefer my new way. But if I can't find a work around I will need to change it so that I would work with different windows form. >_> But really... I do not want the application to open new windows for every task. I would prefer to only show the information in one page.
In my search I did found some tutorials about UI Design with Windows Form but only the design (where a chart would be placed it is only a picture) an not how this will work with real informations.
I hope you could understand my problem...
Let's assume that the constructor of the form is something like this
private List<MyData> _data;
public MyForm(List<MyData> data)
(
...
_data = data;
)
Declare an interface
public IDataAware
{
List<MyData> Data { get; set; }
}
and let the user controls implement it. E.g.
public MyDataGridUserControl : UserControl
{
...
public List<MyData> Data {
get { return (List<MyData>)dataGridView1.DataSource; }
set { dataGridView1.DataSource = value }
}
}
If you are working with a BindingSource, access the BindingSource instead of the grid control. Now, you can access all your user controls through the same interface. In the form you can create a field
private IDataAware _dataUserControl;
In a menu item click routine, you could do some thing like this
_dataUserControl = new MyDataGridUserControl();
in another one
_dataUserControl = new MyChartUserControl();
But all the user controls implementing IDataAware you can do
Controls.Add(_dataUserControl);
_dataUserControl.Data = _data;
Note: Forms, Controls and UserControls are just classes as any other class, and you can do all the object-oriented things with them as with any c# class. (There are some restrictions with generics, however.)

Want to take data from one form to use them as variable in an other

I'm almost done doing a Connect4 game with VS2012 using WinForms. Everything is working well but I wanted to bring the options for the user on a dedicated Start Menu window. On that menu I have two comboBoxes I need to take the text from to use them as a value for two variables in my other form (the game window). I also have one New Game button that should call a method from my other form if that's possible (basically, I made an "Initialization()" method in my game form and I'd like it to be launched when I click the "New Game" button on the other form).
I only found tutorials that show how to do very basic things from one form to an other (such as labeling texts) but I I didn't find an answer to my specific problem.
I used this in my main form to instantiate the menu form
public FormMenu myMenu;
myMenu = new FormMenu();
What I want to do is that I could do something like this in the other form :
amountOfRows = Int32.Parse(myMenu.comboBoxRows.Text);
amountOfColumns = Int32.Parse(myMenu.comboBoxColumns.Text);
Any idea how I could do that?
I would love to see some example code so I can help see where your confusion lies. WinForms requires the other form to be instantiated.
OtherForm form = new OtherForm();
Once the form is instantiated you should be able to run code from it.
EDIT:
Based on your implementation I would suggest making public methods within FormMenu that return these int values.
public int ReturnRows()
{
return Int32.Parse(myMenu.comboBoxRows.Text);
}
public int ReturnColumns()
{
return Int32.Parse(myMenu.comboBoxColumns.Text);
}
Then from the other form in which myMenu is instantiated you can call myMenu.ReturnRows() and myMenu.ReturnColumns()
The easiest way would be to store a reference to your form in the menue as a variable. (you already named it myMenu)
Then you should create the property/properties you need in the form an add a setter for the values. (see example here)
Last you update the form fields with
myMenu.property = newvalue;
That`s all about it

How to call a method of a custom datagridview control?

I have a custom control that I'm also using as a DataGridView control. This custom control has a method called SetHttpClientand I pass a HttpClient object as parameter.
How can I call this method from the parent form? (The parent form only knows about the instance of the class deriving from DataGridViewTextBoxCell)
You should build a nice structure for this. For example if you have two Forms, and in Form 1 you have the Grid, which you want to refresh From Form 2. If you want to open Form 2, you should pass the instance of the Grid from on creating the instance.
Or a second way, Form 2 provide an event which will be subscribed in Form 1. And the event is called if the Grid should be refreshed. You can than pass the HttpClient over the Args-Object to Form 1.
Please tell me, if i misunderstand you.
In my opinion you should use a ViewModels to communicate with View instead of one View execute action on second View.
So, for example your View like
CustomersView contains CustomersViewModel in DataContext, if you execute a operation GetData you make it in CustomersViewModel and the result put to DataGridView.
This solution moves responsibility of providing data from class DataGridView to CustomersViewModel. The ViewModel can use some Service or other mechanism to provide you data using HttpClient.
I know that's not straight answer for your question, but is good solution for your problem.
In this example https://msdn.microsoft.com/en-us/library/ff798384.aspx you have method GetParts, you can imagine that the body of this method contains HttpClient and get the data from a website.
May be you're talking about access modifiers like private, public, internal.
If so, then in child form make access modifier like public or internal.
And in parent form just make needed call.
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
internal String HellowWorld_mt(string SuffixValue)
{
return "hello world and "+ SuffixValue;
}
}
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm cf = new ChildForm();
cf.Show();
string resp = cf.HellowWorld_mt(" extra!");
this.Text = resp;
}
}
More about that: Access Modifiers (C# Reference)

C# using winform controls in another class

I have a WinForm application. On the main form there are a number of controls, such as labels, textboxes, etc.
If I have another class, within the same assembly, how can I access those controls?
For example from my new class, I want to update a label in Form1?
In the property of the label (or any control) set the "Modifiers" option to "Public"
Now you can access the label from the object of the Form
Form1 f = new Form1()
f.lblMyLabel.Text = "My Text"
One way would be to create public properties in your Form1 class that expose the controls you are trying to modify
For example, if your Label is called label1 in the designer then you could do something like this:
public Label MyForm1Label { get { return label1; } }
select the control which you wants to access from another class/form. go to its property and set its modifiers value to "internal" (if you want to use it only in same assembly) .
now where ever in same assembly you wants to use it just create an object of that form like
myForm objform = new myForm();
objform.txtName.Text="any text";
then you can show that form using objform.show(); or objform.showdialog();
but i think this will not solve you issue because what i feel is that your form is already showing on screen and from another form/class you wants to change its label/textbox's value
so for this you will have to take that current object of form otherwise it will not show any changes on currently showing form.
so i think singleton pattern will give you perfect solution.
just create class and in that class create a static object of that form and than create a static function and check if object is already initialized that do not initialize it and use existing otherwise initialize it.
there are lots of other solutions also exists like by creating public property but you will have to use reference of same object of currently showing form to see changes reflect to currently showing form
Apart from the solutions which were already mentioned you can create some public method to your Form that will provide desired functionality (Might be good if some change has to be displayed in several controls - your other classes don't have to remember which one to change)
public void SetSomething(int value)
{
Control1.value = value;
Control2.value = value;
...
}
The easyiest way is to use:
Form1 f = new Form1()
f.lblMyLabel.Text = "My Text"
Therefore, you have to set the Form1 Label "lblMyLabel" just to public. I have done it with a richTextBox.
enter image description here

windows application

Am doing one project in C# windows application with MVC pattern. In that i need to access the controls from client Form to ClientStatus Form any ideas
Your forms fall under the V (view) part of MVC; they only need to present data to the user and provide means for interaction. They don't need to know about what other forms are doing because that is handled in the controller...
Logic to react to user interaction should be contained in the C (controller) part. So in your example the controller will respond to user input on one form to update the state of the other form.
In basic terms your controller should instantiate the forms, react to events on the interactive form, and call methods or modify properties on the other form to update it. The method you use to achieve this depends on the technology you are using; if you are using WinForms then use events and delegates. If you are using WPF then you should look into data binding.
If you are actually using MVC your Forms shouldn't need each other controls. Your forms shouldn't even know each other.
Please explain your situation better.
Since you post no code explaining your implementation of MVC in WinForms, I'm not sure how you're actually instantiating and showing the Forms. I'll assume that you're still calling constructors somewhere in your own code though.
Pass a reference to the Client Form when the ClientStatus form is created (and create a constructor on the ClientStatus form to accept the Client Form as a parameter):
public class ClientForm : Form
{
public ClientForm() { }
}
public class ClientStatusForm : Form
{
ClientForm _parent;
public ClientStatusForm(ClientForm parent)
{
_parent = parent;
}
}

Categories

Resources