Windows Form Is Shown Blank When Opened - c#

this is how the form shows when opened
http://imgur.com/JbE5I6m
what the form is supposed to look like
http://imgur.com/eTTiWUw
note: when in visual studio i did not start by making a win form app, this is for a private server for a game, its a .sln with 9 projects in it that all compile and work together, i am experimenting and seeing if its possible to do windows form in it, so far ive gotten good results, except for when i do the command (/wedit) to open it, the form shows blank, meanwhile in VS i have indeed added stuff to the form.
FrmWorldEdit.Designer.cs below
namespace WorldEdit
{
partial class FrmWorldEdit
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listTiles = new System.Windows.Forms.ListView();
this.lblSearch = new System.Windows.Forms.Label();
this.tbxSearch = new System.Windows.Forms.TextBox();
this.btnToggle = new System.Windows.Forms.Button();
this.lblSelected = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// listTiles
//
this.listTiles.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listTiles.Location = new System.Drawing.Point(24, 69);
this.listTiles.Margin = new System.Windows.Forms.Padding(6);
this.listTiles.MultiSelect = false;
this.listTiles.Name = "listTiles";
this.listTiles.Size = new System.Drawing.Size(512, 492);
this.listTiles.TabIndex = 0;
this.listTiles.UseCompatibleStateImageBehavior = false;
this.listTiles.View = System.Windows.Forms.View.List;
this.listTiles.SelectedIndexChanged += new System.EventHandler(this.listTiles_SelectedIndexChanged);
//
// lblSearch
//
this.lblSearch.AutoSize = true;
this.lblSearch.Location = new System.Drawing.Point(19, 28);
this.lblSearch.Name = "lblSearch";
this.lblSearch.Size = new System.Drawing.Size(138, 25);
this.lblSearch.TabIndex = 1;
this.lblSearch.Text = "Search Tiles:";
//
// tbxSearch
//
this.tbxSearch.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbxSearch.Location = new System.Drawing.Point(163, 26);
this.tbxSearch.Name = "tbxSearch";
this.tbxSearch.Size = new System.Drawing.Size(373, 31);
this.tbxSearch.TabIndex = 2;
this.tbxSearch.TextChanged += new System.EventHandler(this.tbxSearch_TextChanged);
//
// btnToggle
//
this.btnToggle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnToggle.Location = new System.Drawing.Point(24, 617);
this.btnToggle.Name = "btnToggle";
this.btnToggle.Size = new System.Drawing.Size(512, 45);
this.btnToggle.TabIndex = 3;
this.btnToggle.Text = "Start Painting";
this.btnToggle.UseVisualStyleBackColor = true;
this.btnToggle.Click += new System.EventHandler(this.btnToggle_Click);
//
// lblSelected
//
this.lblSelected.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lblSelected.AutoSize = true;
this.lblSelected.Location = new System.Drawing.Point(20, 577);
this.lblSelected.Name = "lblSelected";
this.lblSelected.Size = new System.Drawing.Size(197, 25);
this.lblSelected.TabIndex = 4;
this.lblSelected.Text = "Selected Tile: none";
//
// FrmWorldEdit
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(564, 685);
this.Controls.Add(this.lblSelected);
this.Controls.Add(this.btnToggle);
this.Controls.Add(this.tbxSearch);
this.Controls.Add(this.lblSearch);
this.Controls.Add(this.listTiles);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Margin = new System.Windows.Forms.Padding(6);
this.Name = "FrmWorldEdit";
this.Text = "World Editor Tool";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmWorldEdit_FormClosing);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListView listTiles;
private System.Windows.Forms.Label lblSearch;
private System.Windows.Forms.TextBox tbxSearch;
private System.Windows.Forms.Button btnToggle;
private System.Windows.Forms.Label lblSelected;
public FrmWorldEdit()
{
}
}
}

You have provided the content of the 'FrmWorldEdit.Designer.cs' file
Go to your 'FrmWorldEdit' file and make sure you have 'InitializeComponent' method call in the constructor, like so:
public partial class FrmWorldEdit : Form
{
public FrmWorldEdit()
{
InitializeComponent();
}
}
Dont forget to remove your constructor from the 'Designer' file if will decide to implement as I have suggested. Otherwise leave all as it is, but dont forget to add 'InitializeComponent()' method to your constructor in the 'Designer.cs' file

If the startup Form is blank and there is no compiler error.
Check if your Program.cs file is running the right Form, else it will run a form without context and will appear blank.
In this case the Program.cs file should look something like this:
using System;
using System.Windows.Forms;
namespace WorldEdit
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmWorldEdit());
}
}
}

Related

How can I get the HTML from a Microsoft.Toolkit WebView?

I am using Microsoft.Toolkit.Forms.UI.Controls.WebView, available from https://www.nuget.org/packages/Microsoft.Toolkit.Forms.UI.Controls.WebView
I have added the WebView into my WinForms application - Designer code:
namespace Testing
{
partial class TestForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pnlWebPage = new System.Windows.Forms.Panel();
this.webView = new Microsoft.Toolkit.Forms.UI.Controls.WebView();
this.pnlWebPage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.webView)).BeginInit();
this.SuspendLayout();
//
// pnlWebPage
//
this.pnlWebPage.Controls.Add(this.webView);
this.pnlWebPage.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlWebPage.Location = new System.Drawing.Point(0, 101);
this.pnlWebPage.Margin = new System.Windows.Forms.Padding(4);
this.pnlWebPage.Name = "pnlWebPage";
this.pnlWebPage.Size = new System.Drawing.Size(1205, 624);
this.pnlWebPage.TabIndex = 3;
//
// webView
//
this.webView.Dock = System.Windows.Forms.DockStyle.Fill;
this.webView.Location = new System.Drawing.Point(0, 0);
this.webView.Margin = new System.Windows.Forms.Padding(4);
this.webView.MinimumSize = new System.Drawing.Size(27, 25);
this.webView.Name = "webView";
this.webView.Size = new System.Drawing.Size(1205, 624);
this.webView.Source = new System.Uri("https://www.bbc.com", System.UriKind.Absolute);
this.webView.TabIndex = 0;
this.webView.NavigationCompleted += new System.EventHandler<Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs>(this.webView_NavigationCompleted);
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1505, 725);
this.Controls.Add(this.pnlWebPage);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "TestForm";
this.Text = "TestForm";
this.pnlWebPage.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.webView)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel pnlWebPage;
private Microsoft.Toolkit.Forms.UI.Controls.WebView webView;
}
}
Once a website has loaded into the WebView, I want to be able to extract the page's HTML and store it as a string or similar.
I've added the following method, which is called when the WebView's NavigationCompleted event happens.
private async void wvLI_NavigationCompleted(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs e)
{
\\ HTML extraction to go here
}
I can't see a way to access the HTML however - there doesn't seem to be a property or similar for it. When researching how to do this I can only find questions and answers relating to Android or Xamarin - and none of the solutions for those work for the Microsoft Toolkit WebView. Does anyone know how I can achieve this?
You can invoke the JavaScript eval() function by calling InvokeScriptAsync (or InvokeScript) to get the outerHTML (or innerHTML or whatever you need) as follows:
private async void wvLI_NavigationCompleted(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs e)
{
string html = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
}
It is important that this goes in the NavigationCompleted event, rather than, say, DOMContentLoaded, as the page is only fully loaded at the point of NavigationCompleted firing

the type or namespace Xtralayout does not exist in the namespace Devexpress

I am creating an USER INTERFACE in my programing class, and I am using Devexpress but it can not run because visual studio 2019 says:
the type or namespace Xtralayout does not exist in the namespace Devexpress
I am using C# and a plataform .NET
namespace Inventario_UI.Catalogos
{
partial class FrmListarProveedorcs
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl();
this.Root = new DevExpress.XtraLayout.LayoutControlGroup();
this.formAssistant1 = new DevExpress.XtraBars.FormAssistant();
this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem();
this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem();
((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit();
this.layoutControl1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.Root)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit();
this.SuspendLayout();
//
// layoutControl1
//
this.layoutControl1.Controls.Add(this.labelControl1);
this.layoutControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.layoutControl1.Location = new System.Drawing.Point(0, 0);
this.layoutControl1.Name = "layoutControl1";
this.layoutControl1.Root = this.Root;
this.layoutControl1.Size = new System.Drawing.Size(800, 450);
this.layoutControl1.TabIndex = 0;
this.layoutControl1.Text = "layoutControl1";
//
// Root
//
this.Root.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
this.Root.GroupBordersVisible = false;
this.Root.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
this.layoutControlItem1,
this.emptySpaceItem1});
this.Root.Name = "Root";
this.Root.Size = new System.Drawing.Size(800, 450);
this.Root.TextVisible = false;
//
// labelControl1
//
this.labelControl1.Location = new System.Drawing.Point(12, 12);
this.labelControl1.Name = "labelControl1";
this.labelControl1.Size = new System.Drawing.Size(63, 13);
this.labelControl1.StyleController = this.layoutControl1;
this.labelControl1.TabIndex = 4;
this.labelControl1.Text = "labelControl1";
//
// layoutControlItem1
//
this.layoutControlItem1.Control = this.labelControl1;
this.layoutControlItem1.Location = new System.Drawing.Point(0, 0);
this.layoutControlItem1.Name = "layoutControlItem1";
this.layoutControlItem1.Size = new System.Drawing.Size(780, 17);
this.layoutControlItem1.TextSize = new System.Drawing.Size(0, 0);
this.layoutControlItem1.TextVisible = false;
//
// emptySpaceItem1
//
this.emptySpaceItem1.AllowHotTrack = false;
this.emptySpaceItem1.Location = new System.Drawing.Point(0, 17);
this.emptySpaceItem1.Name = "emptySpaceItem1";
this.emptySpaceItem1.Size = new System.Drawing.Size(780, 413);
this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0);
//
// FrmListarProveedorcs
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.layoutControl1);
this.Name = "FrmListarProveedorcs";
this.Text = "FrmListarProveedorcs";
((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit();
this.layoutControl1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.Root)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private DevExpress.XtraLayout.LayoutControl layoutControl1;
private DevExpress.XtraLayout.LayoutControlGroup Root;
private DevExpress.XtraBars.FormAssistant formAssistant1;
private DevExpress.XtraEditors.LabelControl labelControl1;
private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1;
}
}
```
Devexpress was not updated. That was why VS did not get the reference.
In visual Studio
go to extensiones-devexpress-project convert--upgrade all

How I create custom button in a messagebox in .net form application?

I'm trying to implement a custom messagebox (Ok,Cancel) using .NET Compact Framework 3.5 on Form Application. How I implement it?
If you are after a messagebox with ok and cancel buttons you can use
MessageBox.Show(this, "Message", "caption", MessageBoxButtons.OKCancel);
If you want a custom look/feel and any buttons that you don't normally see on messageboxes, then you have to make your own form to display
MessageBoxButton options
A co-worker and I came up with the following class to act as a sort of dynamic message box.
Here's the designer code:
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblMessage = new System.Windows.Forms.Label();
this.btnRight = new System.Windows.Forms.Button();
this.btnLeft = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblMessage
//
this.lblMessage.AutoSize = true;
this.lblMessage.Location = new System.Drawing.Point(12, 39);
this.lblMessage.Name = "lblMessage";
this.lblMessage.Size = new System.Drawing.Size(35, 13);
this.lblMessage.TabIndex = 0;
this.lblMessage.Text = "label1";
//
// btnRight
//
this.btnRight.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnRight.Location = new System.Drawing.Point(89, 73);
this.btnRight.Name = "btnRight";
this.btnRight.Size = new System.Drawing.Size(75, 23);
this.btnRight.TabIndex = 1;
this.btnRight.UseVisualStyleBackColor = true;
//
// btnLeft
//
this.btnLeft.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnLeft.Location = new System.Drawing.Point(8, 73);
this.btnLeft.Name = "btnLeft";
this.btnLeft.Size = new System.Drawing.Size(75, 23);
this.btnLeft.TabIndex = 0;
this.btnLeft.UseVisualStyleBackColor = true;
//
// CustomMessageBox
//
this.AcceptButton = this.btnLeft;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(170, 114);
this.ControlBox = false;
this.Controls.Add(this.btnLeft);
this.Controls.Add(this.btnRight);
this.Controls.Add(this.lblMessage);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.KeyPreview = true;
this.MinimumSize = new System.Drawing.Size(176, 120);
this.Name = "CustomMessageBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "CustomMessageBox";
this.Load += new System.EventHandler(this.frmCustomMessageBoxLoad);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblMessage;
private System.Windows.Forms.Button btnRight;
private System.Windows.Forms.Button btnLeft;
And here's the code behind the form:
internal partial class CustomMessageBox : Form
{
#region Fields
public readonly MessageBoxButtons _buttons;
#endregion
//need to seal properties to override from derived class
#region Constructors
/// <summary>
/// This constructor is required for designer support.
/// </summary>
public CustomMessageBox()
{
InitializeComponent();
}
public CustomMessageBox(string message, string title, MessageBoxButtons buttons)
{
InitializeComponent();
Text = title;
lblMessage.Text = message;
_buttons = buttons;
}
#endregion
#region Properties
public override sealed string Text
{
get { return base.Text; }
set { base.Text = value; }
}
#endregion
#region private
private void frmCustomMessageBoxLoad(object sender, EventArgs e)
{
lblMessage.Left = (ClientSize.Width - lblMessage.Width) / 2;
switch(_buttons)
{
case MessageBoxButtons.OKCancel:
{
btnLeft.Text = #"OK";
btnLeft.DialogResult = DialogResult.OK;
btnRight.Text = #"Cancel";
btnRight.DialogResult = DialogResult.Cancel;
AcceptButton = btnLeft;
break;
}
case MessageBoxButtons.OK:
{
btnLeft.Text = #"OK";
btnLeft.DialogResult = DialogResult.OK;
btnRight.Hide();
btnLeft.Left = (ClientSize.Width - btnLeft.Width) / 2;
AcceptButton = btnLeft;
break;
}
case MessageBoxButtons.YesNo:
{
btnLeft.Text = #"Yes";
btnLeft.DialogResult = DialogResult.Yes;
btnRight.Text = #"No";
btnRight.DialogResult = DialogResult.No;
AcceptButton = btnLeft;
break;
}
default :
{
btnLeft.Hide();
btnRight.Hide();
break;
}
}
AcceptButton = btnLeft;
}
#endregion
}
You'll need to implement your own custom Form and access it with
myForm.ShowDialog();
Here's a guide to DialogBoxes and you can follow this guide this guide to create your own dialog box.
But if you're only using OK/Cancel buttons, what's wrong with MessageBox?

How can I show a message box with details in WinForms?

Just now I noticed that Visual Studio shows a message box with details when a property is set to an invalid value. For example:
Is it possible to make this type of message box in WinForms?
I have tried the following code:
MessageBox.Show("Error in Division Fill.\n" + ex.Message,
"Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxOptions.RightAlign);
But this produced the following error:
Error 24 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string, string, System.Windows.Forms.MessageBoxButtons, System.Windows.Forms.MessageBoxIcon, System.Windows.Forms.MessageBoxDefaultButton)' has some invalid arguments
G:\Jagadeeswaran\Nov 17\MCS-SPS School\MCS-SPS School\Certificate\Transfer.cs 164 21 MCS-SPS School
How can I fix this error and get a message box that shows additional details?
As others have pointed out, you should write a custom dialog with the desired features. For help on this, you can look at the actual implementation used by the PropertyGrid for this dialog (perhaps with a decompiler) , which is, as of .NET 4.0, the System.Windows.Forms.PropertyGridInternal.GridErrorDlg type, internal to the System.Windows.Forms assembly.
I really wouldn't recommend it (could break in a future release), but if you're feeling really lazy, you can directly use this internal type using reflection.
// Get reference to the dialog type.
var dialogTypeName = "System.Windows.Forms.PropertyGridInternal.GridErrorDlg";
var dialogType = typeof(Form).Assembly.GetType(dialogTypeName);
// Create dialog instance.
var dialog = (Form)Activator.CreateInstance(dialogType, new PropertyGrid());
// Populate relevant properties on the dialog instance.
dialog.Text = "Sample Title";
dialogType.GetProperty("Details").SetValue(dialog, "Sample Details", null);
dialogType.GetProperty("Message").SetValue(dialog, "Sample Message", null);
// Display dialog.
var result = dialog.ShowDialog();
Result:
You need to set following properties of Form to create a custom Dialog/Message window.
AcceptButton
CancelButton
FormBorderStyle=FixedDialog
MaximizeBox=False
MinimizeBox=False
ShowIcon=False
ShowInTaskBar=False
StartPosition=CenterParent
Now, use ShowDialog() method to show custom dialog.
MyDialog dialog=new MyDialog();
DialogResult result=dialog.ShowDialog();
if(result == DialogResult.OK)
{
//
}
For more information on Dialog read MSDN article - Dialog Boxes (Visual C#)
just write your own dialog, there is no overload like you want to show method.
Based on the answer above (which I upvoted) I wrote the following. Paste this into a form you generated using a VS template and it should work, maybe with few tweaks.
My code:
using System;
using System.Windows.Forms;
namespace MessageBoxWithDetails
{
/// <summary>
/// A dialog-style form with optional colapsable details section
/// </summary>
public partial class MessageBoxWithDetails : Form
{
private const string DetailsFormat = "Details {0}";
public MessageBoxWithDetails(string message, string title, string details = null)
{
InitializeComponent();
lblMessage.Text = message;
this.Text = title;
if (details != null)
{
btnDetails.Enabled = true;
btnDetails.Text = DownArrow;
tbDetails.Text = details;
}
}
private string UpArrow
{
get
{
return string.Format(DetailsFormat, char.ConvertFromUtf32(0x25B4));
}
}
private string DownArrow
{
get
{
return string.Format(DetailsFormat, char.ConvertFromUtf32(0x25BE));
}
}
/// <summary>
/// Meant to give the look and feel of a regular MessageBox
/// </summary>
public static void Show(string message, string title, string details = null)
{
new MessageBoxWithDetails(message, title, details).ShowDialog();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Change these properties now so the label is rendered so we get its real height
var height = lblMessage.Height;
SetMessageBoxHeight(height);
}
private void SetMessageBoxHeight(int heightChange)
{
this.Height = this.Height + heightChange;
if (this.Height < 150)
{
this.Height = 150;
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDetails_Click(object sender, EventArgs e)
{
// Re-anchoring the controls so they stay in their place while the form is resized
btnCopy.Anchor = AnchorStyles.Top;
btnClose.Anchor = AnchorStyles.Top;
btnDetails.Anchor = AnchorStyles.Top;
tbDetails.Anchor = AnchorStyles.Top;
tbDetails.Visible = !tbDetails.Visible;
btnCopy.Visible = !btnCopy.Visible;
btnDetails.Text = tbDetails.Visible ? UpArrow : DownArrow;
SetMessageBoxHeight(tbDetails.Visible ? tbDetails.Height + 10 : -tbDetails.Height - 10);
}
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(tbDetails.Text);
}
}
Designer auto generated code (it should give you an idea regarding what to put in the form if you don't want to paste it):
namespace MessageBoxWithDetails
{
partial class MessageBoxWithDetails
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnDetails = new System.Windows.Forms.Button();
this.btnCopy = new System.Windows.Forms.Button();
this.lblMessage = new System.Windows.Forms.Label();
this.tbDetails = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.Location = new System.Drawing.Point(267, 37);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 0;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnDetails
//
this.btnDetails.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnDetails.Enabled = false;
this.btnDetails.Location = new System.Drawing.Point(12, 37);
this.btnDetails.Name = "btnDetails";
this.btnDetails.Size = new System.Drawing.Size(75, 23);
this.btnDetails.TabIndex = 1;
this.btnDetails.Text = "Details";
this.btnDetails.UseVisualStyleBackColor = true;
this.btnDetails.Click += new System.EventHandler(this.btnDetails_Click);
//
// btnCopy
//
this.btnCopy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnCopy.Location = new System.Drawing.Point(93, 37);
this.btnCopy.Name = "btnCopy";
this.btnCopy.Size = new System.Drawing.Size(102, 23);
this.btnCopy.TabIndex = 4;
this.btnCopy.Text = "Copy To Clipboard";
this.btnCopy.UseVisualStyleBackColor = true;
this.btnCopy.Visible = false;
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
//
// lblMessage
//
this.lblMessage.AutoSize = true;
this.lblMessage.Location = new System.Drawing.Point(12, 9);
this.lblMessage.MaximumSize = new System.Drawing.Size(310, 0);
this.lblMessage.Name = "lblMessage";
this.lblMessage.Size = new System.Drawing.Size(35, 13);
this.lblMessage.TabIndex = 5;
this.lblMessage.Text = "label1";
//
// tbDetails
//
this.tbDetails.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.tbDetails.Location = new System.Drawing.Point(12, 68);
this.tbDetails.MaximumSize = new System.Drawing.Size(328, 100);
this.tbDetails.Multiline = true;
this.tbDetails.Name = "tbDetails";
this.tbDetails.ReadOnly = true;
this.tbDetails.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbDetails.Size = new System.Drawing.Size(328, 100);
this.tbDetails.TabIndex = 6;
this.tbDetails.Visible = false;
//
// MessageBoxWithDetails
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(354, 72);
this.Controls.Add(this.tbDetails);
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.btnCopy);
this.Controls.Add(this.btnDetails);
this.Controls.Add(this.btnClose);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MessageBoxWithDetails";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnDetails;
private System.Windows.Forms.Button btnCopy;
private System.Windows.Forms.Label lblMessage;
private System.Windows.Forms.TextBox tbDetails;
}
}
I also encounter the same problem to you. I suggest you use windowsAPIcode pack.
This site will be a great help. Just follow the instructions properly: http://www.developerfusion.com/article/71793/windows-7-task-dialogs/
You can simply do this:
catch (Exception error)
{
throw new Exception("Error with details button! \n"+error);
}
The text in "" is "Adicionar Fotografia".
Note: this code works fine when running the app(.exe) while running in editor it doesn't work and makes sense.
This is an example of the code above:

C# issue: How do I manipulate a Textbox on one form from another form?

I'm trying to create my own error window for the project I am working on. When I show my error window I have no way to pass the error message, and user message to the Error window because the "ErrorMessage.Text" cannot be seen in the classes I make.
I went into the form designer generated code and tried to make the TextBox static, but that just breaks things. Can I make a TextBox public / static so I can change it from another form?
How do I make a TextBox Static Public so I can manipulate it across other forms, or is there another method for doing this?
EDIT:
Okay, for more information...
I have my own Form created. It is called "formErrorWindow." I need to display the form that i've pre-designed with the message set from another form. The only way I can do this is if I create a Function in the windows designer area for the form, and I set the variables with "this.errorMsg.text = error." The only way I can see that function is if I set it to static. If I set the function to Static, when I try and put "this.errorMsg.Text = error" I get this error: An object reference is required for the non-static field, method, or property.
Here is what I've attempted:
namespace LCR_ShepherdStaffupdater_1._0
{
partial class formErrorWindow
{
/// <summary>
/// Required designer variable.
/// </summary>
public System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
///
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
// ********* HERE IS THE FUNCTION THAT IVE ADDED BELOW. THIS WOULD WORK BUT.... *********
public static void showError(string errorTitle, string usrMsg, string errorMsg)
{
formErrorWindow errorWindow = new formErrorWindow();
errorMsgItem.Text = errorMsg;
errorTitleItem.Text = "Error! : " + errorTitle;
usrMsgItem.Text = usrMsg;
errorWindow.ShowDialog();
}
// ********* HERE IS THE FUNCTION THAT IVE ADDED ABOVE. THIS WOULD WORK BUT.... *********
// ********* I get an error: "An object reference is required for the non-static field, method, or property." *********
public void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formErrorWindow));
this.usrMsgItem = new System.Windows.Forms.TextBox();
this.errorTitleItem = new System.Windows.Forms.Label();
this.errorMsgItem = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// usrMsgItem
//
this.usrMsgItem.Enabled = false;
this.usrMsgItem.Location = new System.Drawing.Point(13, 37);
this.usrMsgItem.Multiline = true;
this.usrMsgItem.Name = "usrMsgItem";
this.usrMsgItem.Size = new System.Drawing.Size(334, 81);
this.usrMsgItem.TabIndex = 0;
this.usrMsgItem.Text = "Undefined";
//
// errorTitleItem
//
this.errorTitleItem.AutoSize = true;
this.errorTitleItem.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.errorTitleItem.ForeColor = System.Drawing.Color.Red;
this.errorTitleItem.Location = new System.Drawing.Point(12, 9);
this.errorTitleItem.Name = "errorTitleItem";
this.errorTitleItem.Size = new System.Drawing.Size(152, 20);
this.errorTitleItem.TabIndex = 1;
this.errorTitleItem.Text = "Error! : Undefined";
//
// errorMsgItem
//
this.errorMsgItem.Enabled = false;
this.errorMsgItem.Location = new System.Drawing.Point(0, 21);
this.errorMsgItem.Multiline = true;
this.errorMsgItem.Name = "errorMsgItem";
this.errorMsgItem.Size = new System.Drawing.Size(329, 101);
this.errorMsgItem.TabIndex = 2;
this.errorMsgItem.Text = "Undefined";
//
// button1
//
this.button1.Location = new System.Drawing.Point(272, 256);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Continue";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.errorMsgItem);
this.panel1.Location = new System.Drawing.Point(12, 124);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(335, 126);
this.panel1.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(68, 1);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(189, 17);
this.label2.TabIndex = 3;
this.label2.Text = "Technical Error Message";
//
// formErrorWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.ClientSize = new System.Drawing.Size(359, 290);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Controls.Add(this.errorTitleItem);
this.Controls.Add(this.usrMsgItem);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "formErrorWindow";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Error!";
this.VisibleChanged += new System.EventHandler(this.formErrorWindow_VisibleChanged);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label2;
public System.Windows.Forms.TextBox usrMsgItem;
public System.Windows.Forms.Label errorTitleItem;
public System.Windows.Forms.TextBox errorMsgItem;
}
}
Look for the function I've added above. How do I get that to compile and do what I want it to do WITHOUT that error I keep getting: An object reference is required for the non-static field, method, or property.
I would just pass the message as a constructor parameter.
MyMessageBox messageBox = new MyMessageBox("My error message");
messageBox.Show();
Yes! have a function made public that can receive this text:
pseudo: public void updateTextBox(string new_text)
and have the function update the textbox from there.
don't mix UI with logic.
public partial class Form1 : Form
{
private static TextBox box;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// box
box = new TextBox();
box.Location = new System.Drawing.Point(87, 230);
box.Name = "box";
box.Size = new System.Drawing.Size(100, 20);
box.TabIndex = 1;
this.Controls.Add(box);
}
}
You can make the TextBox public first of.
Second make a static form(name of your form) variable and put the form there each time its loaded...
This is not a good solution in my eyes but without knowing more about your design its the best i can come up with...
Allowing public access to an object contained in your class may be a design. How about writing a property in the error window class called ErrorText and inside that property set the ErrorMessage.Text to the passed string? Or am I missing something?
If you really want to go with accessing the controls directly:
public Form2(Form1 form1)
{
InitializeComponent();
Owner = form1;
_form1 = form1;
}
now you can access the controls on form1 this way:
_form1.theForm1ControlName
You could create a method in the Error window's class which could be something like:
public void SetErrorText(string errorText)
{
this.ErrorMessage.Text = errorText; // assuming the TextBox' ID is ErrorMessage.
}
Sounds like you're on the right track by adding the public function you did, but you shouldn't have to make it static. As a general rule, if you have to go into the Designer Generated Code, something isn't right, so let's get out of there - it doesn't mean you can't add code to the form, obviously, but don't do it in that section :)
The error you're getting I believe originates from having a non-instantiated object (i.e. creating the error form like:
formErrorWindow myErrorWindow;
as opposed to instantiating it / creating an instance of it like so:
formErrorWindow myErrorWindow = new formErrorWindow();
You've made the function static, so you can see it from anywhere, however that function can't actually do anything because the form that it runs on doesn't exist yet until you use " = new formErrorWindow();".
So if I'm in a form called "MainForm" and I want to show an error, using the name of the function you provided it would be done like this:
formErrorWindow myErrorWindow = new formErrorWindow();
myErrorWindow.showError("Title", "User Message Here", "Error Message Here");
myErrorWindow.ShowDialog();
The key is to make sure nothing is static and then make sure you're initializing the formErrorWindow with = new formErrorWindow();
Pass the TextBox to the function/method like so
public static void myFunc(TextBox textBox1)
{
textBox1.Text = "do stuff";
{

Categories