-1

I created a class

public static class Prompt
{
   public static Form showCategoryDialog()
   {
       Form addCategoryDialog = new Form()
       {
           Width = 500,
           Height = 150,
           FormBorderStyle = FormBorderStyle.FixedDialog,
           Text = "caption",
           StartPosition = FormStartPosition.CenterScreen
       };
       Label textLabel = new Label() { Left = 50, Top = 20, Text = "asd" };
       TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
       Button confirmation = new Button() { Text = "Ok", Left = 350, Width =          100, Top = 70, DialogResult = DialogResult.OK };
       confirmation.Click += (sender, e) => { addCategoryDialog.Close(); };
       addCategoryDialog.Controls.Add(textBox);
       addCategoryDialog.Controls.Add(confirmation);
       addCategoryDialog.Controls.Add(textLabel);
       addCategoryDialog.AcceptButton = confirmation;

       return addCategoryDialog;

   }
}

now in the form's button click event

private void addCateogoryClick(object sender, EventArgs e)
    {
        Form a = new Form();
        a = Prompt.showCategoryDialog();
        a.ShowDialog();
    }

There, a textBox, button and label is created in a form and that form is assigned to the form 'a'. Since Form 'a' also has textBox, button and label, i want to call them like

if(a.textbox.text == string.empty)
{
//do this
}

AND access it's button_click event also, like

a.button_click
{
  if(button.text == "ok")
  //do this
}

Summing it up: I dont know how to access a form's control(textbox, button etc) and also i dont know how to use events in this situation.

  • I don't really understand the purpose of this `showCategoryDialog`, what do you want to do with it? – Gal Yedidovich Nov 13 '16 at 16:26
  • You can't they're private. Also you're creating new form instance twice. Form a = new Form(); than a= showCategoryDialog=> addCategoryDialog = new Form(). What are you trying to do? – Miguel Nov 13 '16 at 16:34
  • just making an add category form in which name is required and the button click event would show if the name's field is not empty. – ShoaibSivany Nov 13 '16 at 16:36
  • See marked duplicate for discussion on how to interact with one form from another (or from any other object, for that matter). You can simply make fields public or, better, implement events and properties that hide the underlying controls of the form. There are examples of both in the marked duplicate. – Peter Duniho Nov 13 '16 at 20:37

1 Answers1

0

Derive new class from Form. In constructor add controls the same way you do in showCategoryDialog(), and then for each control add property that will let you access this control. E.g. this sample shows how to do this just for Button:

public class CategoryDialog : Form
{
    public CategoryDialog()
    {
        button1 = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
        Controls.Add(button1);
    }

    Button button1;

    // this property will let you access button
    public Button Button1 { get { return button1; } }
}

When you create instance of this form, you can now access button like this:

CategoryDialog a = new CategoryDialog();
a.Button1.Text = ...

Events (like Button click) are accessible automatically without doing anything. E.g.

a.Button1.Click += (sender, e) =>
{
    //sender parameter contains instance of Button that was clicked
    var button = sender as Button;
    if(button.PropertyToBeChecked == ...)//check whatever you want to
    ...
};

If you are using Visual Studio, it's even simpler. Add new form to your project (it will create derived class automatically), drag controls (Button, Label, etc.) to this form and then click on each control and change it's property "Modifiers" to Public.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • Sir, thanks for your comment, i would like to ask if you tell me how is a.Button1.Click = ... is used? if i want to check something, say a text? – ShoaibSivany Nov 14 '16 at 14:20
  • @ShoaibSivany: I updated my answer to show, how to register event handler to the Click event and perform any check you want. – Ňuf Nov 15 '16 at 02:24