OpenFileDialog doesn't show - c#

I have this simple code:
private void buttonOpen_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
When I run program form doesn't show and exit of debugging mode.
In output view writes:The program '[4244] openfiledialog.vshost.exe: Managed (v4.0.30319)' has exited with code 1073741855 (0x4000001f).
I have Visual Studio 2010 Professional.
Edit:form1.designer.cs
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.buttonOpen = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// buttonOpen
//
this.buttonOpen.Location = new System.Drawing.Point(13, 48);
this.buttonOpen.Name = "buttonOpen";
this.buttonOpen.Size = new System.Drawing.Size(75, 23);
this.buttonOpen.TabIndex = 0;
this.buttonOpen.Text = "open";
this.buttonOpen.UseVisualStyleBackColor = true;
this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(113, 50);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(279, 20);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(13, 98);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(385, 20);
this.textBox2.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(445, 216);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonOpen);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

As a general rule I initialize and use my OpenFileDialog's within the event that is calling it. I can't think of a circumstance in which I would want it to be a property of my window. The first thing I would do is delete it as a property and initialize it in your event.
private void buttonOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
}
You don't need to set the FileName property to anything because the dialog will do it for you.
The only thing I found on your error code was this (Program and debugger quit without indication of problem). Which in your current code I cannot find anything that would cause this. If you are accessing unmanaged code you may need to enable unmanaged code debugging.

Related

Error when trying to Initialize WebBrowser

Can anyone have a look at my code, I don't quite follow what is the error about
private void InitializeComponent()
{
this.WebBrowserHost = new System.Windows.Forms.Integration.ElementHost();
this.SuspendLayout();
//
// WebBrowserHost
//
this.WebBrowserHost.Dock = System.Windows.Forms.DockStyle.Fill;
this.WebBrowserHost.Location = new System.Drawing.Point(0, 0);
this.WebBrowserHost.MinimumSize = new System.Drawing.Size(20, 20);
this.WebBrowserHost.Name = "WebBrowserHost";
this.WebBrowserHost.Size = new System.Drawing.Size(284, 262);
this.WebBrowserHost.TabIndex = 0;
//
// AuthorizationWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.WebBrowserHost);
this.Name = "AuthorizationWindow";
this.Text = "AuthorizationWindow";
this.ResumeLayout(false);
}
In my form I just have a WebBrowser control and I am having the error:
Cannot implicitly convert type
'System.Windows.Forms.Integration.ElementHost' to
'System.Windows.Forms.WebBrowser'
After Initialize I need ElementHost.Child like this
public AuthorizationWindow()
{
// This call is required by the designer.
InitializeComponent();
WebBrowser = new System.Windows.Controls.WebBrowser();
WebBrowserHost.Child = WebBrowser;
WebBrowser.Navigating += WebBrowser_Navigating;
WebBrowser.LoadCompleted += WebBrowser_LoadCompleted;
_authorization = new AuthorizationState();
}
Did you try changing the line
this.WebBrowserHost = new System.Windows.Forms.Integration.ElementHost();
to
this.WebBrowserHost = new System.Windows.Forms.WebBrowser();
?

Bind List<class> to DataGridView

I need your help on how to bind List to the DataGridView. I tried to use the BindingList<T> but still it does not display the records in my gridview. I tried using the List<T>, but it still does not work.
Below is the code which I used:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ToolClientController ctrl = new ToolClientController();
IpAddressTextbox.Text = ctrl.GetIPv4Config();
PortNumberTextbox.Text = ctrl.GetPortNumber();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
ToolMsgFileTextbox.Text = openFileDialog1.FileName;
}
}
private void UploadButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ToolMsgFileTextbox.Text))
{
FileTypeError.SetError(UploadButton, "Please enter filename.");
return;
}
if (!openFileDialog1.FileName.Contains(".txt"))
{
FileTypeError.SetError(UploadButton, "File should be in .txt");
return;
}
ToolClientController ctrl = new ToolClientController();
List<ToolMessages> test = new List<ToolMessages>();
ToolMessages tool = new ToolMessages();
tool.IsPass = true;
tool.ToolMessageReply = string.Empty;
tool.ToolMessageRequest = "x";
test.Add(tool);
MessageGridViews.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "ToolMessageRequest";
col1.HeaderText = "Request";
MessageGridViews.Columns.Add(col1);
BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test);
MessageGridViews.VirtualMode = true;
MessageGridViews.DataSource = bind;
}
}
Based on your code I prepared simple WinForms app:
public Form1()
{
InitializeComponent();
List<ToolMessages> test = new List<ToolMessages>();
ToolMessages tool = new ToolMessages();
tool.IsPass = true;
tool.ToolMessageReply = string.Empty;
tool.ToolMessageRequest = "x";
test.Add(tool);
dataGridView1.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "ToolMessageRequest";
col1.HeaderText = "Request";
dataGridView1.Columns.Add(col1);
BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test);
dataGridView1.DataSource = bind;
dataGridView1.Show();
}
I added dataGridView1 into form directly via WinForms designer, so InitializeComponent method looks like:
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(560, 57);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(967, 399);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
And everything seems to work just fine... Data are bound to grid and ouput looks like:
which is exactly what I would expect to see, so I don't see any real problem with you solution, unless you post bigger piece of your code on which I can reproduce the issue you are experiencing.

How to compile and add the form designed by user to a project

I am going to let users create Forms during RunTime and add them to a project. I have done the designing and the UI of the form with the help of an open source Form Designer.
Here is the image of the Form Designer:
Lets assume I have the Form1.cs and Form1.cs[Designer] files which are enough for a WinForm. But how do I compile it to a DLL or an EXE and add it to the project? Any ideas? Any clues?
Thanks!!
EDIT
It creates this code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 49);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseCompatibleTextRendering = true;
this.button1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 94);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(38, 184);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(104, 24);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseCompatibleTextRendering = true;
this.checkBox1.UseVisualStyleBackColor = true;
//
// form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
I will do some extra coding to the rest of the code myself. but I dont know how to compile it and add it to the main exe?
The clue is System.CodeDom.Compiler
i think Form1.cs will contain no code (other then InitializeComponents call in constructor). so if you merge it with the designer code with simple string operations your job will be easier.

My C# Windows Form Application is blank?

I'm Making A windows form application , And everytime I Excute it , All i see is a blank form , My project is named frmMain.cs and this is the header that i use :
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private TextBox txtYear;
private Button btnCheck;
private Button btnClose;
private Label label1;
#region windows code
private void IntitializeComonent()
{
}
#endregion
public frmMain()
{
IntitializeComonent();
}
[STAThread]
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtYear = new System.Windows.Forms.TextBox();
this.btnCheck = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(25, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(98, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Year To Test: ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtYear
//
this.txtYear.Location = new System.Drawing.Point(129, 24);
this.txtYear.Name = "txtYear";
this.txtYear.Size = new System.Drawing.Size(100, 20);
this.txtYear.TabIndex = 1;
//
// btnCheck
//
this.btnCheck.Location = new System.Drawing.Point(25, 93);
this.btnCheck.Name = "btnCheck";
this.btnCheck.Size = new System.Drawing.Size(75, 23);
this.btnCheck.TabIndex = 2;
this.btnCheck.Text = "Leap Year?";
this.btnCheck.UseVisualStyleBackColor = true;
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(154, 93);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 3;
this.btnClose.Text = "&Close";
this.btnClose.UseVisualStyleBackColor = true;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnCheck);
this.Controls.Add(this.txtYear);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Determain a Leap Year";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
I also have a habit that i delete program.cs and frmMain.Designer.cs because i dont need them in my program , Does that have anything to do with my problem ? Help Please!
In frmMain() you're calling InitializeComonent() (which is empty) instead of calling InitializeComponent(). Probably just a typo.
Your code is fine. Just a small typo
public frmMain()
{
// In your constructor change 'IntitializeComonent' to 'InitializeComponent'
IntitializeComonent();
}

Preventing users from resizing columns widths in ListView?

I have a ListView In My Winform that has 4columns, Name, Money, ID and Level.
The problem is when I run my app, I still have the ability to mess with the columns widths
and change them.
I searched And found that I should do something like this:
private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
But the problem is that when I debugged and Ccanged the columns widths, this event didn't even fire!
Why didn't it fire?
And how can I make the column widths fixed?
I made a new winform app just in case if there was something wrong in my old one,
it fired, but only for the first time running the app .. here's the code:
namespace CsharpWinformTestingStuff
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
}
void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
}
}
here is the initialize component just in case you might wanna know:
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(284, 275);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Name";
this.columnHeader1.Width = 97;
//
// columnHeader2
//
this.columnHeader2.Text = "Age";
this.columnHeader2.Width = 52;
//
// columnHeader3
//
this.columnHeader3.Text = "Email";
this.columnHeader3.Width = 157;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(308, 299);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
You need to register the ColumnWidthChanging event with your form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// This line registers the event, soc that the form can "hear" it and call the indicated handling code:
this.listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging);
}
void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
Console.Write("Column Resizing");
e.NewWidth = this.listView1.Columns[e.ColumnIndex].Width;
e.Cancel = true;
}
}
Just click on the Properties>>Events>>ColumnWidthChanging.
Then add this code:
private void lstItems_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = lstItems.Columns[e.ColumnIndex].Width;
}
Happy coding! ^_^
You may check Better ListView Express. We have implemented AllowResize property on the columns, which does exactly what you need.

Categories

Resources