-5
public partial class MainForm : Form
{
    public MainForm(string Username)
    {
        InitializeComponent();
    }

Hey I tried to pass data from form1 to Mainform and all the time that show me this error

There is no argument given that corresponds to the required formal parameter 'Username' of 'MainForm.MainForm(string)'

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
MayKayTie
  • 5
  • 1
  • Where are you getting this error? – David Arno Jan 04 '16 at 14:46
  • It's saying that you're not passing a string in when you call the `MainForm` constructor. If the method signature says it takes a string, then you have to provide one when you call it. – adv12 Jan 04 '16 at 14:47
  • How are you calling the MainForm? – Huntt Jan 04 '16 at 14:47
  • 3
    You say you've tried everything. What have you tried? All you have shown us is the constructor of `MainForm`. How is this actually called, what have you tried when calling the constructor? – Stephen Ross Jan 04 '16 at 14:49
  • 2
    Duplicate of - [https://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form](https://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form) – haddow64 Jan 04 '16 at 14:51

1 Answers1

3

You have done at least one step, but you probably missed some.

You passed in the variable, but you didn't assign it yet:

public MainForm(string username)
{
    InitializeComponent();

    this.Username = username;
}

public string Username { get; set; }

Here I have create a property named Username to assign the incoming variable to. (Note that I renamed your argument to make it lower-case, since that is how the naming convention is)

And you should provide a value when you call the MainForm constructor:

MainForm m = new MainForm("my name");
m.Show();
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • ` public partial class MainForm : Form { public MainForm(string Username) { InitializeComponent(); this.username = Username; } public string username { get; set; } ` like that? – MayKayTie Jan 04 '16 at 16:17
  • Yes, but your naming convention for properties and fields is the other way around, see my answer. – Patrick Hofman Jan 04 '16 at 16:19
  • wasnt work. and the argument is Username in Form1. There is no argument given that corresponds to the required formal parameter 'Username' of 'MainForm.MainForm(string)' – MayKayTie Jan 04 '16 at 16:58