c#


Passing Parameter from MDIParent to MDIChild Form


In C# Windows application, I have a Login Form, MdiParent Form and several Mdichild forms. When a user logs in successfully the Mdi Parent form is launched and then the user can launch the Child forms by clicking buttons on the MDI Parent.In the MDI Parent form I have a method:
public void ShoworActivate<T>(string loggedUser)where T: Form
{
var k = MdiChildren.Where(c==>c.GetType() == typeof(T)).FirstorDefault();
if(k= null)
{
k = (Form)Activator.CreateInstance(typeof(T));
......More code
}
}
I need to pass the parameter string loggedUser to child forms but when I put the parameter in the constructor of the child form, I get an error :
{"No parameterless constructor defined for this object."}
How can I solve this?
I don't want to use Child Form = new ChildForm(string loggedUser).This works.
You are doing over engineering by using reflection to create form instance. You could have used factory for creating forms.
Anyways for your current problem you need to use another version of CreateInstance method which also takes parameters to be passed in the constructor of the target type.
k = (Form)Activator.CreateInstance(typeof(T), System.Reflection.BindingFlags.CreateInstance, null, loggedUser);
You can refer various overrides of CreateInstance method at https://msdn.microsoft.com/en-us/library/system.activator.createinstance(v=vs.110).aspx
CreateInstance(typeof(T),loggedUser);

Related Links

How to get Scroll to work with a Vertically oriented Hub Control Windows Phone 8.1 App
How to write query to generate XML file from SQL Server table
Load winform from class library from string
how to display binary image to image box? [duplicate]
Entity Framework serialization in WebApi not eager-loading
WPF: Update/refresh/rebind ItemSource?
Is there a way using SimpleMembership Provider to login using the webpages_Membership table encrypted password field
How to get formatted word document's content using OpenXML?
Issues connecting to a SQL Server database in C#
Object reference not set to an instance of an object with linq query EF6.1 [duplicate]
MarshalByRefObject Lifetime
When to use Custom HTML Helpers Asp.net MVC
Regex to get all “cells” form csv file row [duplicate]
LINQ Cannot implicitly convert type Generic.ICollection to Generic.List
WPF Window - When to load data so form appears instantly
Sharepoint SPDocumentLibrary - Location of SPWeb.Lists in Database

Categories

HOME
reactjs
joomla
macros
service
jenkins-pipeline
ldap
bigtable
squeak
symfony-sonata
collision-detection
meson-build
google-api-oauth
restheart
soap-client
digital
vichuploaderbundle
point-of-sale
resharper
captcha
react-select
responsive
pylint
vivado-hls
wallet
draw2d
udeploy
bootstrap-datetimepicker
lotusscript
mattermost
aws-iot
bootstrap-accordion
android-wear-2.0
ocl
key-bindings
login-required
ibm-bpm
urhosharp
dropdownbox
cache-control
maven-2
queryover
mediastream
julia-jump
collation
directadmin
web-technologies
pexpect
nshttpurlresponse
servicebus
inner-join
spring-webflow-2
nao-robot
openxava
dotnet-cli
boost-regex
dmalloc
try-catch-finally
multi-select
lightspeed
syslog4j
hfp
patching
php-java-bridge
clique
calc
wizard
dcast
instabug
dynamics-nav-2013
android-contentresolver
random-sample
dnvm
musicbrainz
android-filterable
p4python
veeam
pymol
backtrace
frisby.js
grunt-contrib-connect
novell
api-eveonline
go-gin
nsnotificationcenter
azure-worker-roles
disparity-mapping
amzi-prolog
isl
django-tests
opencobol
azure-caching
iwork
dayofweek
acrofields
codio
vbaccelerator
netbeans-plugins
usersettings
kademlia
panelgrid
wordbreaker
biztalk-deployment
phone-state-listener
openafs
databus
winrt-async
eeprom
nsundomanager
cherokee
log-shipping
fxcopcmd
silverlight-2.0
hp-trim
disk-based
j++

Resources

Encrypt Message