-1

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:-(

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Mnur
  • 13
  • 2
  • 1
    *"I tried to put the accessibility modifier public"* - Where? I don't see any code for `MainWindow` or its `listView1` property. – David Jan 12 '23 at 19:00
  • 1
    Does this answer your question? [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread) – Thomas Weller Jan 12 '23 at 19:07
  • Please provide a [mre]. Use a simple UI with one button and one text box. – Thomas Weller Jan 12 '23 at 19:08
  • 1
    What does this (not quite a) sentence mean: _"error shows listview1 is not accessible due to security level"_? Is that an exception or something else? What's the error text? Are these two forms in the same process? What is the relationship between the windows? Why is this do you show both WinForms and WPF in the title – Flydog57 Jan 12 '23 at 19:39
  • I just put it in the wrong place – Mnur Jan 12 '23 at 20:04
  • For data transmission between forms, it is easier to use to create a public property or method, and then call it. – Jiale Xue - MSFT Jan 16 '23 at 07:21

2 Answers2

1

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);
David
  • 208,112
  • 36
  • 198
  • 279
  • sorry to bother you, but if the reference to the object is not available to the object instance – Mnur Jan 12 '23 at 20:02
  • @Mnur: It's not clear what you mean by that. Please elaborate. – David Jan 12 '23 at 20:04
  • well, the specified data will not be added to the table, but will disappear – Mnur Jan 12 '23 at 20:07
  • @Mnur take a step back, it sounds like you have a _smart UI_ running the show; consider looking into the Model-View-Presenter pattern, so that your UI manipulates model properties. Then you share the model instance between the forms, and nobody needs to know anything about anyone's private controls. – Mathieu Guindon Jan 12 '23 at 20:08
  • @Mnur: What "table"? When you step through the code in a debugger, what specifically fails in this exact operation? If you have *some other code* which is doing something else, such as over-writing your ListView's data, then that's something else you'd need to investigate. This answer is specifically focused on the problem demonstrated in the question. – David Jan 12 '23 at 20:10
1

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.

add-game

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);
            }
        }
    }
}
IVSoftware
  • 5,732
  • 2
  • 12
  • 23