Showdialog accessing controls

rahul kumar 605 Reputation points
2023-11-10T04:26:50.3233333+00:00

below is the snippet that i am using to open a dialog form from a datagridview on the key press of alt+c but i am having some major problems

Itemedit form has a two controls : label1 and a textbox1which i cannot access

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {   
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            int rowIndex = this.CurrentCell.RowIndex;
            int columnIndex = this.CurrentCell.ColumnIndex;
            // add new item mode 
            if (columnIndex == 0 && (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN))
            {
                if (keyData == (Keys.Alt | Keys.C))
                {
                    ItemEdit edit = new ItemEdit();
                    Point PositionX = this.Parent.PointToScreen(Point.Empty);
                    int LeftScreenX = PositionX.X;
                    Point PositionY = this.Parent.PointToScreen(Point.Empty);
                    int TopScreenY = PositionY.Y;
                    edit.StartPosition = FormStartPosition.Manual;
                    edit.Location = new Point(LeftScreenX, TopScreenY);
                    edit.ShowInTaskbar = false;
  error -->                  edit.label1.Text += "Creation";  
                    if (edit.ShowDialog() == DialogResult.Yes)
                    {                        
  error-->                      this.Rows[rowIndex].Cells[columnIndex].Value = edit.textBox1;                        
                    }
                    return true; // return true means the shortcut key has been processed
                }               
            return base.ProcessCmdKey(ref msg, keyData); // If it is not the desired shortcut key, call the method of the base class for default processing
        }


Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-11-10T06:33:23.5366667+00:00

    Hi @Rahul Kumar , Welcome to Microsoft Q&A,

    You need to modify the access modifiers of label and textbox in itemedit.designer.cs to public;

    public System.Windows.Forms.Label label1;
    public System.Windows.Forms.TextBox textBox1;
    

    enter image description here

    In addition, there seems to be something wrong with this code:

    if (edit.ShowDialog() == DialogResult.Yes)
                         {
       error--> this.Rows[rowIndex].Cells[columnIndex].Value = edit.textBox1;
                         }
    

    What effect do you want to achieve?

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Viorel 125.7K Reputation points
    2023-11-10T06:26:33.7+00:00

    Try to open the ItemEdit form, select the label, change the Modifiers property to Public. Repeat this for textbox.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.