Having a form send back a DialogResult value

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
Having a form send back a DialogResult value.

Is that possible? If so, how do you do it?

I'll tell you what I am doing so that you understand better. I have a tab control that is password protected by another password form that pops up. If the password is correct I want to send back "access" if it is not correct I want to send back "noaccess".
 
Last edited:
Provided you've shown the form with the ShowDialog method, you can just set DialogResult from within the popup form. I suggest you use Ok and Cancel for your access and noaccess values, respectively.
 
Ok, I have it set up. But no matter what I get the result "Cancel" returned because I need to close the form with this.Close().

Even when I use Dispose I get the same thing.
 
Last edited:
C#:
// 
			// PassForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(194, 80);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.TB,
																		  this.label1});
			this.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "PassForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Security";
			this.TopMost = true;
			this.ResumeLayout(false);
			this.DialogResult=DialogResult.No;
		}
		#endregion

		private void TB_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{ 
			if((int)e.KeyChar==(int)Keys.Enter)
			{
				string pass="";
				pass=CommunicationLog.aaa.GetValue("aaa").ToString().ToLower();

				if(pass==this.TB.Text.ToLower())
				{
	//CommunicationLog.Access=true;
					r=DialogResult.Yes;
					this.Dispose();
				}
				else {//MessageBox.Show("Access Denied");
					r=DialogResult.No; this.Dispose();}
			}

--------------------------------------------------------------------------------


PassForm P=new PassForm();
				DialogResult r= P.ShowDialog();
				if(r==DialogResult.Yes) MessageBox.Show(r+"");
				else MessageBox.Show(r+"");
 
Last edited by a moderator:
Ok, now I remember. DialogResult is a property, not a variable. It works now. Thank you very much.
 
Back
Top