OpenFileDialog的问题.

lsfyfan 2002-08-16 10:58:00
各位高手:
我在一个form里加了一个toobar, 然后在toolbar上有一个button, btnOpen, 我用它来打开一个文件. 代码如下:
private void btnOpen_Click( object sender, System.EventArgs e )
{
if (this.openFileDialogReadPayment.ShowDialog()
== System.Windows.Forms.DialogResult.OK) /*到这里就没有相应了, 而且没有弹出对话框. 不知是何原因?:(
/*.....*/
}
请不吝赐教. 多谢.
...全文
181 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsfyfan 2002-08-21
  • 打赏
  • 举报
回复
感谢楼上的回答, 其实你所说的我这边都有, 只是后来我设了一个InitalizeDirectory, 问题就解决了. 另外, 我以前是没有设InitalizeDirectory的, 但是可以执行的(在某些机器上不能). 我在想是不是.Net FrameWork出了问题, 还是?
freesoul_ms 2002-08-19
  • 打赏
  • 举报
回复
感谢您使用微软产品。

我测试了您的代码,在我这边运行没有任何问题。下面是我的完整示例代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace OpenFile
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialogReadPayment;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.Button btnOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.openFileDialogReadPayment = new System.Windows.Forms.OpenFileDialog();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.btnOpen = new System.Windows.Forms.Button();
this.toolBar1.SuspendLayout();
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnOpen});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(360, 39);
this.toolBar1.TabIndex = 1;
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(32, 8);
this.btnOpen.Name = "btnOpen";
this.btnOpen.TabIndex = 2;
this.btnOpen.Text = "Open";
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(360, 310);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.toolBar1});
this.Name = "Form1";
this.Text = "Form1";
this.toolBar1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnOpen_Click(object sender, System.EventArgs e)
{
if (this.openFileDialogReadPayment.ShowDialog()
== System.Windows.Forms.DialogResult.OK)
{
}
}

}
}

请设置断点检查一下您的btnOpen_Click这个方法是否执行,是否将btnOpen_Click添加到btnOpen.click的事件响应函数中。
希望能对您有所帮助!



======================
- 微软全球技术中心

本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
freesoul_ms 2002-08-19
  • 打赏
  • 举报
回复
如果您指的是toolbar的toolbarbutton的click,那么首先您要给toolbar添加ButtonClick事件的处理函数:
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
然后,在该处理函数中判断点击的是哪一个toolbarbutton,并进行相应的操作:

private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (this.toolBar1.Buttons.IndexOf(e.Button))
{
case 0:
if (this.openFileDialogReadPayment.ShowDialog()
== System.Windows.Forms.DialogResult.OK)
{
}
break;
case 1:
MessageBox.Show("second button", "The Event Information");
break;
}
}
希望能对您有所帮助!



======================
- 微软全球技术中心

本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
lsfyfan 2002-08-16
  • 打赏
  • 举报
回复
没人回答吗?

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧