我的第一个c#程序,大家看看毛病在哪里?(10分)

蔓草

Unregistered / Unconfirmed
GUEST, unregistred user!
编译可以通过,但是菜单却没有,请大家指点一下!
// project created on 2002-4-21 at 17:35
using System;
using System.Windows.Forms;
using System.Drawing;
class MainForm : Form
{
public void CreateMenu()////建立菜单的部分
{
MainMenu myMM = new MainMenu();
MenuItem MyMI=new MenuItem("文件");
MenuItem newMI=new MenuItem("新建");
MenuItem editMI=new MenuItem("编辑");
MenuItem exitMI=new MenuItem(" 退出", new EventHandler(this.endProgram),Shortcut.CtrlN);
MyMI.MenuItems.Add(newMI);
MyMI.MenuItems.Add(editMI);
MyMI.MenuItems.Add(exitMI);
Menu=myMM;
} //建立菜单的部分

public void CreateForm()////建立窗体的部分
{
this.Text = "信息管理系统";
this.StartPosition=FormStartPosition.Manual;
Point P = new Point(0,0);
this.Location=P;
Size s=new Size(800,600);
this.Size=s;
this.FormBorderStyle=FormBorderStyle.FixedSingle;
this.ControlBox=true;
this.MaximizeBox=false;
this.TopMost=true;
} ///建立窗体的部分

public static void Main(string[] args)
{
Application.Run(new MainForm());
}
public MainForm():base()
{
CreateForm();
CreateMenu();
}
protected void endProgram(object sender,System.EventArgs e)
{
Application.Exit();
}
}
 
没有GG会C#帮助MM么?:~| 我帮忙提前一下好了!:〉
 
还在观望 没想到MM先学了
 
呵呵 灌水先 我还没开始学C#呢:)
 
webform 都不很熟悉
winform 没做过
帮你提前,
 
我还没有学呀
 
至少少了一句
myMM.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {MyMI,newMI,editMI,exitMI});
没做测试,不知道还差什么?
 
timerri:
呵呵,果然有了,但是我看书上是这么写的,没有用到你说的那句话
using System;
using System.Windows.Forms;
using System.Drawing.Printing;
class myMenu : Form
{
public myMenu() : base()
{
Text = "Menu Sample";
MainMenu myMM = new MainMenu();
MenuItem fileMI = new MenuItem("文件");
MenuItem editMI = new MenuItem("编辑");
MenuItem helpMI = new MenuItem("帮助");
MenuItem newMI = new MenuItem("新建",new EventHandler(this.endProgram),Shortcut.CtrlN);
MenuItem openMI = new MenuItem("打开",new EventHandler(this.endProgram),Shortcut.CtrlO);
MenuItem saveMI = new MenuItem("保存",new EventHandler(this.endProgram),Shortcut.CtrlS);
MenuItem saveasMI = new MenuItem("另存为",new EventHandler(this.endProgram),Shortcut.CtrlShiftS);
MenuItem pagesetupMI = new MenuItem("页面设置",new EventHandler(this.endProgram),Shortcut.CtrlShiftU);
MenuItem printMI = new MenuItem("打印",new EventHandler(this.endProgram),Shortcut.CtrlP);
MenuItem quitMI = new MenuItem("退出",new EventHandler(this.endProgram),Shortcut.AltF4);
MenuItem cutMI = new MenuItem("剪切",new EventHandler(this.endProgram),Shortcut.CtrlX);
MenuItem copyMI = new MenuItem("复制",new EventHandler(this.endProgram),Shortcut.CtrlC);
MenuItem pasteMI = new MenuItem("粘贴",new EventHandler(this.endProgram),Shortcut.CtrlV);
MenuItem findMI = new MenuItem("查找",new EventHandler(this.endProgram),Shortcut.CtrlF);
MenuItem replaceMI = new MenuItem("替换",new EventHandler(this.endProgram),Shortcut.CtrlH);
MenuItem descMI = new MenuItem("帮助主题",new EventHandler(this.endProgram),Shortcut.F1);
MenuItem verMI = new MenuItem("关于…",new EventHandler(this.endProgram),Shortcut.CtrlF1);
fileMI.MenuItems.Add(newMI);
fileMI.MenuItems.Add(openMI);
fileMI.MenuItems.Add(saveMI);
fileMI.MenuItems.Add(saveasMI);
fileMI.MenuItems.Add(pagesetupMI);
fileMI.MenuItems.Add(printMI);
fileMI.MenuItems.Add(quitMI);
editMI.MenuItems.Add(cutMI);
editMI.MenuItems.Add(copyMI);
editMI.MenuItems.Add(pasteMI);
editMI.MenuItems.Add(findMI);
editMI.MenuItems.Add(replaceMI);
helpMI.MenuItems.Add(descMI);
helpMI.MenuItems.Add(verMI);
myMM.MenuItems.Add(fileMI);
myMM.MenuItems.Add(editMI);
myMM.MenuItems.Add(helpMI);
descMI.Checked = true;
Menu = myMM;
}
public static void Main()
{
Application.Run(new myMenu());
}
protected void endProgram(object sender, System.EventArgs e)
{
Application.Exit();
}
}
欢迎对c#有兴趣的人来交流交流啊!
 
因为它有这个!
Application.Run(new myMenu());
 
timerri:
哎呀,不对啊,我想的是菜单一竖排排下来啊,怎么是横的呢?
 
惭愧,落伍了,我还没用过C#呢,别说用,见都没见过呢
 
算了,别看书了,我这里visual studio.net可视化建立的.......
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4});
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "编辑";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "属性";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "退出";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}
 
timerri:
我想多了解一下c#的结构,所以才看书一点一点写出来的,我用的开发工具是
sharpdevelop,没有用visualstudio里的东西,你看我的代码比你的代码是不是
简单很多!
pipi:
如果我delphi有你一半的水平,我也不用看c#了,拿了就能用!呵呵
 
小妹妹啊,你要怎样一个菜单,是不是这样的菜单:
文件
新建
编辑
退出
你的CreateMenu()明显有错误,myMM根本没有绑定菜单
public void CreateMenu()////建立菜单的部分
{
MainMenu myMM = new MainMenu();
MenuItem MyMI=new MenuItem("文件");
MenuItem newMI=new MenuItem("新建");
MenuItem editMI=new MenuItem("编辑");
MenuItem exitMI=new MenuItem(" 退出", new EventHandler(this.endProgram),Shortcut.CtrlN);
MyMI.MenuItems.Add(newMI);
MyMI.MenuItems.Add(editMI);
MyMI.MenuItems.Add(exitMI);
myMM.MenuItems.Add(MyMI);
//加上这句
Menu=myMM;
}

 
我刚开始学呢,多指教!
QQ:18283251
验证:programmer
 
还挺认真的....
那我就认真说吧。
首先肯定seaboy所说的,你的要求确实应使用他的方法来实现。
我没仔细看你的代码,所以给你了个AddRange,AddRange是用来增加一组同父MenuItem的,我给的东西父节点都是MainMenu,当然会放到同一排去。
Add方法和AddRange功能差不多,区别在于Add只能一次加入一个菜单项。
像你写的
MyMI.MenuItems.Add(newMI);
MyMI.MenuItems.Add(editMI);
MyMI.MenuItems.Add(exitMI);
这部分,其实可以简单的写成
MyMI.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{newMI,editMI,exitMI});
这里new System.Windows.Forms.MenuItem[]{newMI,editMI,exitMI}是新建一个对象数组,因为AddRange只能接受菜单项数组作为参数。
由于你的MainMenu只有一个项"文件",那么我们只需要用Add就能解决。
myMM.MenuItems.Add(MyMi);
菜单是建立完成了,但我们还需要把它加入到Form里去,Form有个属性叫MainMenu,专门用来关联Form与菜单,所以就有了那个Menu=myMM;
菜单就像一棵树,根节点是MainMenu,Add用来增加一个子节点,AddRange用来增加一堆子节点。
关于事件处理,MS尽量让我们原先的事件观念不丢失(用JAVA我就一点也没了原来的感觉),但是编程方法却像极了DELPHI,只不过直接函数引用变成了经过EventHandler的委托。
实际上C#的事件操作跟java的ActionListener(等等)原理上差不多),但是用起来可亲切多了。
事件处理没有了VC里的begin
_MESSAGE_MAP(虽然他是针对消息的)那样的令人讨厌的宏,确是MS的一大改进
这样,我们就有了
MenuItem exitMI=new MenuItem(" 退出", new EventHandler(this.endProgram),Shortcut.CtrlN);

exitMI.Click+=new EventHandle(this.endProgram);//跟我们DELPHI的exitMI.click:=endProgram;多像
这两种方法添加事件。
不过跟DELPHI区别的是,事件处理函数可以添加多个,而且都能响应事件!!代码重用绝对可以提高很多。
还有public MainForm():base()
这个地方默认的就是base(),不写他也罢。
这里也有点意思,Java的实例构造函数写在方法体内部,而C#写在方法的声明部分(我感觉好),只不过用法却是一样的
知识点差不多了吧?
不过我建议应该先了解微软提供的编程框架再去深入研究别的处理方法。毕竟微软的编程框架最体现C#的构造思想。
你不用说自动生成的代码长,其实去掉注释跟你那个的长度差不多。
还有就是多看看MSDN吧!人家都翻译成中文了还不看?MSDN绝对比你那些所谓的书强多了。(这也是我用vs.net的一个理由)
 
timerri:
请问哪里有中文的msdn下载,我买的是英文的,而且还装不上去!
 
根本就没有中文的msdn,即使说是中文的,也只是几篇文章中文,有用的都是英文的
 
没见下...
VS.net正式中文版(7张)里自带中文MSDN
大部分还是英文,不过C#的语言参考与.net的类库参考全是中文的(虽然我已习惯看英文,看中文文档还真有点不习惯)
to Pipi.
这么经典的东西你都没有?好的开发资料还是要跟着微软走
 
顶部