'Windows form to collect data
I have a dialog box for the user to fill in information for a post (post name, post description, etc), then, when the user press "ok" it will start the user control constructor to build a post with the data given. It's a code a found in an old program of mine and edit it for the current requirements. I get an error when trying to run the UserControl1 constructor. An object reference is required for the nonstatic field, method, or property"
public partial class Form1 : Form
{
private UserControl1 Post_Storage;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = "";
ShowInputDialog(ref input);
}
/// <summary>
/// type = DialogResult
/// </summary>
private static void ShowInputDialog(ref string input)
{
System.Drawing.Size size = new System.Drawing.Size(325, 350);
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = "Add a new post"; ///window name
////////
Label PostName = new Label();
PostName.Text = "Post Name:";
PostName.Size = new System.Drawing.Size(size.Width - 10, 23);
PostName.Location = new System.Drawing.Point(10, 10);
inputBox.Controls.Add(PostName);
System.Windows.Forms.TextBox NametextBox = new TextBox();
NametextBox.Size = new System.Drawing.Size(size.Width - 10, 23);
NametextBox.Location = new System.Drawing.Point(10, 35);
inputBox.Controls.Add(NametextBox);
//////////
////////
Label PostDis = new Label();
PostDis.Text = "Post Description:";
PostDis.Size = new System.Drawing.Size(size.Width - 10, 23);
PostDis.Location = new System.Drawing.Point(10, 60);
inputBox.Controls.Add(PostDis);
System.Windows.Forms.TextBox DistextBox = new TextBox();
DistextBox.Size = new System.Drawing.Size(size.Width - 10, 23);
DistextBox.Location = new System.Drawing.Point(10, 85);
inputBox.Controls.Add(DistextBox);
//////////
////////
Label PostURL = new Label();
PostURL.Text = "Video URL:";
PostURL.Size = new System.Drawing.Size(size.Width - 10, 23);
PostURL.Location = new System.Drawing.Point(10, 110);
inputBox.Controls.Add(PostURL);
System.Windows.Forms.TextBox URLtextBox = new TextBox();
URLtextBox.Size = new System.Drawing.Size(size.Width - 10, 23);
URLtextBox.Location = new System.Drawing.Point(10, 135);
inputBox.Controls.Add(URLtextBox);
//////////
////////
Label PostTag = new Label();
PostTag.Text = "Tags (Please add related tags decribing the video \n and separate them using #. exp: #Tag #Tag2):";
////*max of 5 tags per video.
///when user tries to add more than 5 tags, a message will pop, asking to remove.
PostTag.Size = new System.Drawing.Size(size.Width - 10, 35);
PostTag.Location = new System.Drawing.Point(10, 160);
inputBox.Controls.Add(PostTag);
System.Windows.Forms.TextBox TagtextBox = new TextBox();
TagtextBox.Size = new System.Drawing.Size(size.Width - 10, 23);
TagtextBox.Location = new System.Drawing.Point(10, 195);
inputBox.Controls.Add(TagtextBox);
//////////
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&ADD";
okButton.Location = new System.Drawing.Point(size.Width - 240 - 80, 300);
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new System.Drawing.Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new System.Drawing.Point(size.Width - 240, 300);
inputBox.Controls.Add(cancelButton);
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
String Post_Name = NametextBox.Text;
String Post_Dis = DistextBox.Text;
String Post_URL = URLtextBox.Text;
String Post_Tags = TagtextBox.Text;
///MessageBox.Show(Post_Name);
DialogResult Name = inputBox.ShowDialog();
input = NametextBox.Text;
///DialogResult dis = inputBox.ShowDialog();
///input = DistextBox.Text;
///DialogResult URL = inputBox.ShowDialog();
///input = URLtextBox.Text;
///DialogResult Tags = inputBox.ShowDialog();
///input = TagtextBox.Text;
if (inputBox.DialogResult == DialogResult.OK)
{
// Display a message box indicating that the OK button was clicked.
MessageBox.Show("Added the post.");
// Optional: Call the Dispose method when you are finished with the dialog box.
// in here we are going to run the constructor builder for the new post, from the UC constructor.
Post_Storage = new UserControl1(Post_Name, Post_Dis, Post_URL, Post_Tags);
Post_Storage.Location = new Point(50, 50);
this.Controls.Add(Post_Storage);
inputBox.Dispose();
}
else
{
// Display a message box indicating that the Cancel button was clicked.
MessageBox.Show("Upload canceled.");
// Optional: Call the Dispose method when you are finished with the dialog box.
inputBox.Dispose();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
