Witajcie.
Mam kilka pytań na początek, jestem nowy, proszę o wyrozumiałość.
- Co robią instrukcje:
this.SuspendLayout();
this.ResumeLayout(false);
this.PerformLayout();
- Dlaczego w poniższym kodzie na textbox1 nie zmienia się wyświetlana liczba - przecież inkrementuję ją w zdarzeniu kliknięcia przycisku oraz odświeżam kontrolkę textbox:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Form1 : Form
{
public int wsk;
public Form1()
{
InitializeComponent();
}
private Button button1;
private Button button2;
private TextBox textBox1;
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(98, 50);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(98, 153);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Paint += new System.Windows.Forms.PaintEventHandler(this.button2_Paint);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(89, 206);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 2;
this.textBox1.Text = wsk.ToString();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
wsk++;
textBox1.Refresh();
}
private void button2_Paint(object sender, PaintEventArgs e)
{
}
}
}
- Dlaczego w obsłudze zdarzenia metoda Invalidate() odświeża całą formę a nie tylko przycisk.
Jak zrobić żeby odświeżeniu uległ jedynie przycisk?
private void button1_Click(object sender, EventArgs e)
{
// odświeżenie kontrolki Form a nie button'a
Invalidate();
}
A i jeszcze jedno drobne pytanie :
W metodzie obsługi zdarzenia formatki .Paint mam napisane :
private void Form1_Paint(object sender, PaintEventArgs e)
{
//Invalidate();
wsk += 10;
Graphics g = e.Graphics;
g.DrawRectangle(Pens.Red, 5, 5, 7, wsk);
}
W chwili gdy za "DrawRectangle" dopisuję instrukcję :
g.Dispose();
to generowany jest błąd po uruchomieniu projektu - VS wskazuje na Application.Run i wypisuje że "parametr jest nieprawidłowy". Skąd ten błąd się bierze ? Przecież w literaturze czytałem że utworzone obiekty typu Graphics można a nawet należy usunąć po "narysowaniu".
dodanie znacznika <code class="csharp"> - furious programming