0

I have a problem in passing a string value between two forms.

First I created public string sanacode which I assign the value passed in form 2

Form 1 code

AnalysisEdit ae = new AnalysisEdit();
int Row = dataGridView1.CurrentRow.Index;
ae.sanacode = dataGridView1[0, Row].Value.ToString();
ae.Show();

Form 2 constructor code

public AnalysisEdit()
{
    InitializeComponent();
    MessageBox.Show(sanacode,);
}

it shows me nothing

2
  • 2
    Your code does not compile. Commented Jun 20, 2013 at 12:08
  • Can you make sure ae.sanacode = dataGridView1[0, Row].Value.ToString(); is pulling back any value? Also post more code of your AnalysisEdit form or you are going to end up with everyone posting to make a property or variable. You may have already done this. Commented Jun 20, 2013 at 12:11

5 Answers 5

2

Change you constructor from

public AnalysisEdit()
{
   InitializeComponent();
   MessageBox.Show(sanacode);
}

to

public AnalysisEdit(string sanacode)
{
    InitializeComponent();
    MessageBox.Show(sanacode);
}

form call

int Row = dataGridView1.CurrentRow.Index;
AnalysisEdit ae = new AnalysisEdit(dataGridView1[0, Row].Value.ToString());    
ae.Show();
Sign up to request clarification or add additional context in comments.

1 Comment

it tells me that i must have a constructor with empty parameter
1

The issue is that you're not calling things in the correct order. The form 2 constructor code will be called on line 1 of the form code, or AnalysisEdit ae = new AnalysisEdit(); However, this is before the assignment that takes place on line 3: ae.sanacode = dataGridView1[0, Row].Value.ToString(); So when you show the messagebox in the form 2 constructor, sanacode has not yet been assigned to.

There are two ways to fix this. Firstly, you can pass the value in via the constructor as per @kostas ch.'s answer, or you can override the form's OnShown event in form 2:

protected override void OnShown(EventArgs e)
{
    MessageBox.Show(sanacode);
}

Comments

0

I wouldn't put your code

MessageBox.Show(sanacode,);

in the constructor. I would use the "Load"-Event. If you use the "Load"-Event your MessageBox will Show, when you use

ae.Show();

Like this

private void AnalysisEdit_Load(object sender, EventArgs e)
    {
        MessageBox.Show(sanacode);
    }

2 Comments

the idea to use messagebox is to make sure that the string is passed correctly what i want exactly is to pass string from datagrid in form 1 to be used in form 2
Even if I also think that the other solutions are better, my code does exactly what you just described. It will take a string that you apply to sanacode in Form1 and shows it after you Load Form2
0

Perhaps you'd like some ideas for the various ways to pass data between forms. I have written two blog posts on the subject:

http://geek-goddess-bonnie.blogspot.com/2011/01/passing-data-between-forms.html

http://geek-goddess-bonnie.blogspot.com/2012/12/passing-data-between-forms-redux_31.html

Comments

-1

You could temporarily add MessageBox.Show() to your setter:

// In Form2
public sanacode
{
  set
  {
    _sanacode = value;
    MessageBox.Show(_sanacode);
  }

  get
  {
    return _sanacode;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.