如何将输入数据从一个窗体传递到另一个窗体C#、WinForms、wpf

7kqas0il  于 2023-01-14  发布在  C#
关注(0)|答案(2)|浏览(230)

帮助中,无法在AddGame窗体中将输入的数据从文本框传输到MainWindow窗体中的listview 1表。错误显示由于安全级别原因,listview 1不可访问
添加表单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");
        }
    }
}

主形式

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);
            }
    }
}

我不知道该怎么做我失去了什么功能和方法我有一些想法如何链接这在SQl,但认为它不会工作😢:-(

ou6hu8tu

ou6hu8tu1#

错误显示listview1由于安全级别而无法访问
窗体控件在由窗体设计器添加时默认不是public。您 * 可以 * 将其设置为public,但通常不建议编辑设计器控制的代码。(因为它可能会被设计器的进一步更改覆盖。)
MainWindow上创建public * 方法 * 以执行此操作。例如:

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

然后,您可以从其他窗体的代码中调用该公共方法:

mainWindow.AddListViewItem(lvi);
ut6juiuv

ut6juiuv2#

当你准备好将游戏提升到一个新的水平时,你的代码可以通过保持每个窗体的UI控件私有化并使用属性和方法来访问窗体之间的值来改进。问题是,我们真的不想让AddGame窗体访问主窗体的列表视图。这里有一个简化设计的方法,只需三个简单的步骤。

创建一个类来表示游戏(它将成为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; }
}

添加游戏

当你显示“添加游戏”对话框时,它的目的是填充游戏类。一旦用户提供了足够的信息,新游戏是有效的,然后启用[确定]按钮。

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);
        }
    }
}

检索有效游戏

如果AddGameForm返回DialogResult.OK,则意味着它保存了一个有效的游戏表单,您可以检索该表单并将其分配给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);
            }
        }
    }
}

相关问题