c# - WinForms passing data between Forms -


i have table named questions field name qcategory. in wfa have toolstripmenu have category named simulation , sub-category named b. so, want create mysql select select rows values equal sub-category value. (columns qcategory table has value b). string:

static string dataa = "select distinct * questions order rand() limit 1"; 

the problem have 2 forms. 1 menu , 1 want make select.

you should try split ui-code , database code. called layering (mvvm, mvc, mvp,...) don't have to!

there several ways:

1) make class both forms can reference , execute database logic there. (that cleanest way now)

2) create event in form menu , react on in other form

3) menu form holds reference other form , executes method on passing selected subitem.

in code

1

public static class sqlclass {     public static void executequery(string menuitem)     {         //execute query     } }   form 1 //menu changed... sqlclass.executequery(menuitem) 

2

form1: public event eventhandler<string> menuitemchanged;  //menu changed... if(this.menuitemchanged != null)     this.menuitemchanged(this, menuitem)   form2: public form2(form1 otherform) {     initializecomponent();     _otherform.menuitemchange += //... handle sql code } 

3

private readonly form2 _otherform;  public form1(form2 otherform) {     initializecomponent();     _otherform = otherform; }  //menu changed... otherform.executequery(menuitem); 

for examples, form 2 form want execute query because there method/event-handler defined interact database.

to understand solution, need more high level perspective - information in code behind of form (a class) , want consume information somewhere else.
in general need reference form holds information interested in , tells information when changed (event) or
information source tells every interested destination (calls method). information source hold references consumers.
both concepts same direction of communication (and reference) changes.

the alternative (option 1) move information destination somewhere else (e.g. in static class) , consume there. mechanism of passing information pretty same (via parameterized method call) encapsulates ui-colde (form) database code (sql query execution)


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -