Accessing string from another Form
Hello I have datagridview in form1 and through form1 I open form2 and through form2 I open form3 and string named vyber_ID_K
placed in Form1 needs to be accessed in Form3 (I need to get its value in Form3)
this is placed on button click in form1
form2 a = new form2 ("Novy");
string vyber_IDK = (string)dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
a.vyber_ID_K = vyber_IDK;
a.Show();
a.Closed += klient_Closed;
I would like to access vyber_ID_K in form 3
, how it can be done? I tried to set public string vyber_ID_K in form2 and pass it similary to form3 but I get null
. Am I doing it right? Is there any other better solution please?
Thanks in advance.
My step-by-step according to Servy:
button click in Form 1
Func vyberIDKGetter = () => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
try
{
form2 = new form2 ("Novy");
a.vyberIDKGetter = () => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
a.Show();
}
button click in form2
public Func vyberIDKGetter; private void button1_Click(object sender, EventArgs e) { nova_platba b = new nova_platba("novy"); b.vyberIDKGetter(); b.Show(); b.Closed += klient_Closed; }
In form3
Func<string> vyberIDKGetter = veberIDK;
string vyberIDK = vyberIDKGetter();
SqlCommand sc = new SqlCommand(@"
INSERT INTO kliplat (datum,text,castka,akce,subkey,priznak,rocnik)
VALUES (@datum,@text,@castka,@akce,@subkey,@priznak,@rocnik);
SELECT scope_identity();
", spojeni);
sc.Parameters.AddWithValue("@subkey", vyberIDK);
So the issue here is that the value that you want doesn't exist yet when you're constructing Form2
, or even Form3
for that matter. It needs to have some means of accessing the data at some point in the future. We can get this behavior by leveraging delegates.
Rather than passing a string
to Form2
, when that form is constructed (since we don't know what the string will be yet) pass a Func<string>
. That object will be a method that, when invoked, will provide a string that represents the needed value. Form1
can define it like this:
Func<string> vyberIDKGetter =
() => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
Then in Form3
when it's holding onto the function that was passed it can get the string out by simply invoking that delegate:
Func<string> vyberIDKGetter = [...];
string vyberIDK = vyberIDKGetter();
This approach to solving the problem is particularly adventageous in that Form3
doesn't need to know anything about Form1
or Form2
. If there is some other caller that wants to use it they can provide their own delegate instead. If there is a developer handling the coding of each form they don't need to communicate all of the internal details of each form to each other, they can just handle the passing of this delegate and then be able to treat the caller/callee as a black box.
You have to make a public getter/setter around the string:
public string Vyber_ID_K
get
{
return vyber_ID_K;
}
set
{
vyber_ID_K = value
}
That you need a reference from Form 1 in Form 2, and from Form 2 in Form 3. So you can access each Form.
You can't use a string as Referenced Parameter, becuase it is an immutable class. String C#
That is a really odd that you pass a parameter via the constructor
form2 a = new form2 ("Novy");
and in the same time you pass another parameter via the property
a.vyber_ID_K = vyber_IDK;
Why don't you instead pass all parameters via the constructor?
string vyber_IDK = (string)dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
form2 a = new form2 ("Novy", vyber_IDK);
and in Form2
public class form2
{
private string Name { get; set; }
private int vyber_IDK { get; set; }
public form2( string Name, int vyber )
{
this.Name = Name;
this.vyber_IDK = vyber_IDK;
}
Then, passing anything to form3
from form2
works in the same way
form3 f = new form3( this.vyber_IDK );
链接地址: http://www.djcxy.com/p/71162.html
下一篇: 从另一个窗体访问字符串