c#


A way to access class/method from parent form using usercontrol


I would like to have some advice or possible solution to access a class or method from the parentform to user control..
i wanted to reuse the loadquizes() which was just to reload the datagrid from gradingsheet form using the usercontrol on the same form.
my structure of my project was
Mainform(Form)
|_Gradingsheet(MDIchild/Also a Form)
|_mypanel(inside the Gradingsheet)
|_updatequizes(usercontrol)
Gradingsheet code to call the usercontrol
private void updateQuizToolStripMenuItem_Click(object sender, EventArgs e)
{
quiz = new UpdateQuizes();
quiz.lrnLbl.Text = MelrnLbl.Text;
quiz.nameLbl.Text = MEnameLbl.Text;
quiz.quartervar.Text = quartervar.Text;
quiz.GradeLbl.Text = GradeLbl.Text;
quiz.subjLbl.Text = subjLbl.Text;
quiz.yearLbl.Text = yearLbl.Text;
UserControlPanel.Controls.Clear();
UserControlPanel.Controls.Add(quiz);
}
Inside the UserControl
void insertQuiz()
{
using (MySqlConnection conn = new MySqlConnection(myConnection))
{
string query = "UPDATE GimenezSystem.quizes SET Q"+(quizcombo.SelectedIndex + 1) + " =?newval WHERE Year=?year and GradeLevel=?glevel and Subject=?subj and Quarter =?qtr and LRN =?LRN";
conn.Open();
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
try
{
cmd.Parameters.AddWithValue("?LRN", lrnLbl.Text);
//cmd.Parameters.AddWithValue("?quiz", quiz);
cmd.Parameters.AddWithValue("?newval", NewVal.Text);
cmd.Parameters.AddWithValue("?year", yearLbl.Text);
cmd.Parameters.AddWithValue("?glevel", GradeLbl.Text);
cmd.Parameters.AddWithValue("?subj", subjLbl.Text);
cmd.Parameters.AddWithValue("?qtr", quartervar.Text);
cmd.ExecuteNonQuery();
GradingSheet gr = GradingSheet.ActiveForm as GradingSheet;
gr.loadquizes();//here it gives me an error of object is not set for reference of instance or something like that T.T
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
}
conn.Close();
}
}
If you really want to re-use the method multiple places then consider implementing the method in a separate class and use that in whichever from or user control you want instead of having the method in a Form or UserControl. below code lines is the issue.
GradingSheet gr = GradingSheet.ActiveForm as GradingSheet;
gr.loadquizes();//he
Instread have it in a separate class like
public class Helper
{
public void LoadQuizes()
{
//code here
}
}

Related Links

Getting Error An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter
Using async/await with pre-defined delegate signature
Why does MessageQueue.Send(Object) NOT work when sending to remote private queue using FormatName?
Creating binding source vs just assigning a List<C> for combobox?
EF Fluent API + two column composite uniqueness + autoincrement primary key
Object flies off to infinity after rotation
Binding autofac with webapi using generic repository
what is using SR=MS.Internal.PresentationCore.SR?
Design pattern for similar processing steps, but with slight variations
How can I declare a pointer to a struct for P/Invoke?
Check if FieldInfo type is generic class without type
Desktop application not able to connect to web services
datagridview all cell values pass to label
sql query for getting data between 2 dates which are explicitly given
Scale out PhantomJs istances on Azure in .Net
How to set help information for pscmdlet class

Categories

HOME
actionscript-3
azure-active-directory
ftp
cors
postgresql-9.3
xamarin.forms
x86-64
django-middleware
thermal-printer
meson-build
browserstack
cracking
ios-universal-links
wowza
facebook-sharer
elastalert
windows-phone
android-actionbar
orange
bootstrap-datetimepicker
sonarlint
xna
cherrypy
angular2-pipe
pumping-lemma
ckeditor4.x
adobe-captivate
boosting
typeorm
bytebuffer
custom-fields
spock
pushpad
semantic-ui-react
storefront
scene7
n1ql
odp.net
wunderground
ponylang
perf
gstreamer-1.0
oracle-bpm-suite
instances
armadillo
linkurious
file-conversion
socketcluster
uitextview
logan-square
ocpjp
line-endings
monaca
dotnet-cli
visualizer
dynamic-linking
bioperl
aws-rds
codesniffer
huawei
directinput
racket-student-languages
mongodb-3.3
class-design
gdi
db2-zos
factorization
methodology
play-json
dpkt
tokudb
log-analysis
system.diagnostics
autopep8
optimistic-locking
dojox.grid
parsefloat
disabled-control
scjp
twitter-finagle
connections
db2-connect
nop
violation
xrandr
nsregularexpression
libx264
teamsystem
netbiscuits
webpage-screenshot
unordered-set
copyfile
wdm
soapexception
gmagick
windows-xp-embedded
youtube.net-api
functional-specifications
llblgen
konsole
time-management
authkit
file-icons
jgrowl
asp.net-1.1

Resources

Encrypt Message