nixp.ru v3.0

25 апреля 2024,
четверг,
15:12:07 MSK

anonymous написал 1 июля 2005 года в 13:43 (8803 просмотра) Ведет себя неопределенно; открыл 1814 темы в форуме, оставил 5575 комментариев на сайте.

Здраствуйте, я новичек в С#.. у меня не получается создать кнопку на форме.. Из MSDN я вставил текст:

private void InitializeMyButton()

{

// Create and initialize a Button.

Button button1 = new Button();

// Set the button to return a value of OK when clicked.

button1.DialogResult = DialogResult.OK;

// Add the button to the form.

Controls.Add(button1);

}

а дальше что делать? помогите, кто может, please..

decvar

this.Show();

дабы показать форму с батоном :)

ну можно еще текст поменять или OnClick эвент чем-нить обработать

anonymous

Все равно не получается :( вот что у меня написано и куда мне дописать?

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace map

{

///

/// Summary description for Form1.

///

public class Form1 : System.Windows.Forms.Form

{

///

/// Required designer variable.

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

private void InitializeMyButton()

{

// Create and initialize a Button.

Button button1 = new Button();

// Set the button to return a value of OK when clicked.

button1.DialogResult = DialogResult.OK;

// Add the button to the form.

Controls.Add(button1);

}

///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// Required method for Designer support — do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.Size = new System.Drawing.Size(300,300);

this.Text = «Form1»;

}

#endregion

///

/// The main entry point for the application.

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

}

}

decvar

ну а вызов этой своей InitializeMyButton() хоть где-нить сделать надо!

Марш учить basic c азов программирования.

anonymous

спасибо, но все равно не получается…

:) Все это я делал.. но это все равно не получается..Вот, например, в Visual C++, Basic и т.д. мы находим компонент Button тыкаем его на форму ну и так далее.. А тут как? Ну написала я эту ф-цию, ведь эта ф-ция-метод у обекта Form1 (раз я его туда написал), его, конечно, нужно вызвать, но Form1.InitializeMyButton() неверно!

Еще ведь неопределено, где должна располагаться эта кнопка.. так не правильно.. Please, помогите мне.. очень надо!!!!!!

decvar

Поставь http://www.icsharpcode.net/OpenSource/SD/Default.aspx

кинь на форму кнопку. и посмотри что там на делано.

и еще раз, ИДИ УЧИТЬСЯ АЗАМ ПРОГРАММИРОВАНИЯ.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace sample
{
      public class MyForm : System.Windows.Forms.Form
      {
//--- MyButton ---
            private System.Windows.Forms.Button myButton;
// ---
            private System.ComponentModel.Container components = null;
            public MyForm()
            {
                  InitializeComponent();
            }
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)  
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.Size = new System.Drawing.Size(300,300);
                  this.Text = "SampleForm";
// --- new instanse of Button
             myButton = new System.Windows.Forms.Button();
             myButton.Text = "Sample"; // add text
             myButton.Width = 100; // size x
             myButton.Height = 30; // size y
             myButton.Location = new System.Drawing.Point(20, 20); //position
             Controls.Add(myButton); //add to form
                          
            }
            [STAThread]
            static void Main()  
            {
// now run current app inside current app domain with MyForm as main instanse
// MyForm already contains MyButton so there is no need to call .Show() for it.
// Application.Run() will show MyForm with MyButton on it.
                  Application.Run(new MyForm());
                  
            }
      }
}
decvar

ну и до кучи, с большими пояснениями:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace sample
{
      public class MyForm : System.Windows.Forms.Form
      {
         private System.Windows.Forms.Button myButton;
            private System.ComponentModel.Container components = null;
      // constructor of MyForm
         public MyForm()
            {
            // init components
                  InitializeComponent();
            }
      // override dispose of object class for MyForm
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
             //call for parent's .Dispose()
                  base.Dispose( disposing );
            }
      // this method will init controls of MyForm
            private void InitializeComponent()
            {
                  components = new System.ComponentModel.Container();
                  Size = new System.Drawing.Size(300,300);
                  Text = "SampleForm";
            // new button
             myButton = new System.Windows.Forms.Button();
              // set text
              myButton.Text = "Sample";
              // size x
             myButton.Width = 100;
             // size y
             myButton.Height = 30;
             // set new handler for Click event
              myButton.Click += new System.EventHandler(myButton_Click);
             // location is a point, so define a point
             myButton.Location = new System.Drawing.Point(20, 20);
             // add to controls collection
            Controls.Add(myButton);
            }
            [STAThread] // this is gonna be single thread
            static void Main()
            {
            // run app with MyForm
                  Application.Run(new MyForm());
            }
      // event handler for Click event of myButton
      private void myButton_Click(object sender,EventArgs e)
      {
            // show message with text
            MessageBox.Show("Clicked!");
            // print text to stdout
            Console.WriteLine("myButton clicked!");
      }
      }
}

PS

Шуруп, это у меня копирование с табами глючит или в YABB что-то нет так?

фигня какая-то копируется, приходится руками исправлять переносы и табы…

Последние комментарии

ecobeingecobeing.ru
Экология и вегетарианство на благо всем живым существам Планеты.