简体   繁体   中英

how to pass input data from one form to another form C#, WinForms, wpf,

help, it's not possible in the AddGame form to transfer the entered data from the Textboxes to the listview1 table which is in the MainWindow form. error shows listview1 is not accessible due to security level

add form your text

 public partial class AddGame : Form
    {
        public MainWindow mainWindow;
        TextBox[] textBoxes; 
        public AddGame()
        {
            InitializeComponent();

        }
            public void setForm1(MainWindow form)
        {
            mainWindow = form;
        }
        private void AddGame_Load(object sender, EventArgs e)
        {
        }
        private void CloseAddFormButton_Click(object sender, EventArgs e)
        {
            Close();
        }
        public void Add0Batchbutton_Click(object sender, EventArgs e)
        {
            try
            {
                // Check to see if there are any blank texts, we can't be having those.
                if (Play0NametextBox.Text == string.Empty)
                    throw new System.ArgumentException("");
                if (PlayersTextBox.Text == string.Empty)
                throw new System.ArgumentException("");
                if (Sequence1TextBox.Text == string.Empty)
                 throw new System.ArgumentException("");
                if (Sequance2TextBox.Text == string.Empty)
                throw new System.ArgumentException("");
                if(CommentsTextBox.Text == String.Empty)
                throw new System.ArgumentException("");
            if(WinnerTextBox.Text ==  String.Empty)
                throw new System.ArgumentException("");
            if(GameDateTextBox.Text == string.Empty)
                throw new System.ArgumentException("");

            // Use the info given via textbox's and add them to items/subitems
            ListViewItem lvi = new ListViewItem(Play0NametextBox.Text);
            lvi.SubItems.Add(PlayersTextBox.Text);
            lvi.SubItems.Add(Sequence1TextBox.Text);
            lvi.SubItems.Add(Sequance2TextBox.Text);
            lvi.SubItems.Add(CommentsTextBox.Text);
            lvi.SubItems.Add(WinnerTextBox.Text);
            lvi.SubItems.Add(GameDateTextBox.Text);
            // Add the items to the list view.

            mainWindow.listView1.Items.Add(lvi); // here the error is that listview1 is not      accessible due to its security level

            // If no error, success.
            MessageBox.Show("");
            Close();
        }
        catch (Exception ex)
        {
            //If error show the error
            MessageBox.Show(ex.Message, "Error");
        }
    }
}

main form

  public partial class MainWindow : Form
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void AddGamesButton_Click(object sender, EventArgs e)
    {
        var addGames = new AddGame();
        addGames.Show();
    }
    private void Closebutton_Click(object sender, EventArgs e)
    {
        Close();
    }
    public void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (ListViewItem item in listView1.Items)
            item.ForeColor = Color.Gray;
    }
    private void DeleteButton_Click(object sender, EventArgs e)
    {
        int selectNumber = -1;
        foreach (ListViewItem item in listView1.Items)
            if (item.Selected)
            {
                selectNumber = int.Parse(item.Text);
            }
    }
}

I do not know what to do I'm lost what functions and methods I have some idea how to link this in SQl but thinking it won't work:-(

error shows listview1 is not accessible due to security level

Form controls are not public by default when added by the forms designer. You could make it public , but it's not generally advisable to edit designer-controlled code. (As it could be over-written by further designer changes.)

Create a public method on MainWindow to perform this operation. For example:

public void AddListViewItem(ListViewItem lvi)
{
    this.listView1.Items.Add(lvi);
}

Then you can call that public method from your other form's code:

mainWindow.AddListViewItem(lvi);

When you're ready to take your game to the next level, your code could be improved by keeping each form's UI controls private and using properties and methods to access the values between forms. The thing is, we really don't want the AddGame form to have access to the main form's list view. Here's one way to streamline the design in three easy steps.


Make a class to represent a Game (that will become a ListViewItem)

public class Game
{
    public string? Play0Name { get; set; }
    public string? Players { get; set; }
    public string? Sequence1 { get; set; }
    public string? Sequence2 { get; set; }
    public string? Winner { get; set; }
    public string? Comments { get; set; }
    public DateTime GameDate { get; set; }
}

Add Game

When you show the Add Game dialog its purpose is to populate the Game class. Once the user supplies enough information that the new game is valid then enable the [OK] button.

添加游戏

public partial class AddGameForm : Form
{
    public AddGameForm()
    {
        InitializeComponent();
        StartPosition = FormStartPosition.Manual;
        buttonOK.Enabled = false; // Will be enabled when all the texboxes have values
        buttonOK.Click += (sender, e) =>DialogResult = DialogResult.OK;
        // Make an array of the textboxes.
        _textboxes = 
            Controls
            .Cast<Control>()
            .Where(_ => _ is TextBox)
            .Cast<TextBox>()
            .ToArray();
        foreach (var textbox in _textboxes) textbox.TextChanged += onAnyTextChanged;
    }
    // Determine if the game is valid by looking through all the textboxes.
    private void onAnyTextChanged(object? sender, EventArgs e)
    {
        buttonOK.Enabled = !_textboxes.Any(_=>string.IsNullOrWhiteSpace(_.Text));
    }
    private TextBox[] _textboxes;
    // If this dialog returns [OK] then the main form 
    // can retrieve the valid game using this method.
    public Game GetGame()
    {
        Game game = new Game
        {
            Play0Name = textBoxPlay0Name.Text,
            Players = textBoxPlayers.Text,
            Sequence1 = textBoxSequence1.Text,
            Sequence2 = textBoxSequence2.Text, 
            Winner = textBoxWinner.Text, 
            Comments = textBoxComments.Text, 
        };
        return game;
    }
    // Position the form relative to the main form when shown.
    protected override void OnVisibleChanged(EventArgs e)
    {
        base.OnVisibleChanged(e);
        if(Visible && Owner != null) 
        {
            Location = new Point(
                x: Owner.Location.X + Owner.Width + 10,
                y: Owner.Location.Y);
        }
    }
}

Retrieve valid Game

If the AddGameForm returns DialogResult.OK it means it holds a valid game form that you can retrieve and assign however you want to the ListView.

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonAddGame.Click += onClickAddGame;
    }        
    private void onClickAddGame(object? sender, EventArgs e)
    {
        using (AddGameForm addGame = new AddGameForm())
        {
            if (DialogResult.OK.Equals(addGame.ShowDialog(this)))
            {
                Game game = addGame.GetGame();
                // Use the info given via textboxes and add them to items/subitems
                ListViewItem lvi = new ListViewItem(game.Play0Name);
                lvi.SubItems.Add(game.Players);
                lvi.SubItems.Add(game.Sequence1);
                lvi.SubItems.Add(game.Sequence2);
                lvi.SubItems.Add(game.Comments);
                lvi.SubItems.Add(game.Winner);
                lvi.SubItems.Add(game.GameDate.ToShortDateString());

                // Add the items to the list view.
                listView1.Items.Add(lvi);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM