I need help with the Check button. After a user adds all the 42 numbers in the textbox and enter a number from 0-9 in the "Enter number" area and clicks on the start button, next what he should do is run through the array of labels with the red label or lblCovece and he should collect the same values like the number enters before. And after he clicks on the Check button, the programme should first validate if the value that is selected with the red label is the same as the number entered. If is valid the label should turn green and than the result should appear in the label lblResultE(the result for example should be like this: if the number entered is 2, the result it is 2+2+2...)and if is not valid in the lblResultE we take out 10 points. That's what i did by now with some help.:) thank you.
namespace Seminarska
{
public partial class Form1 : Form
{
private Label l,l2,lblCovece,l4,lblResultE;
private Button bUp, bRight, bLeft, bDown, bCheck,bStart, bReset;
private TextBox txtVnes, txtGoal;
private Label[] pole;
public Form1()
{
InitializeComponent();
l2 = new Label();
l2.Text = " Enter one number";
l2.Location = new Point(230, 200);
l2.AutoSize = true;
l4 = new Label();
l4.Text = "Score";
l4.Location = new Point(240, 260);
l4.AutoSize = true;
lblResultE = new Label();
lblResultE.Location = new Point(350, 260);
lblResultE.AutoSize = true;
bLeft = new Button();
bLeft.Location = new Point(0, 250);
bLeft.Width=75;
bLeft.Height = 25;
bLeft.Text = "LEFT";
bCheck = new Button();
bCheck.Location = new Point(75, 250);
bCheck.Width = 75;
bCheck.Height = 25;
bCheck.Text = "Check";
bRight = new Button();
bRight.Location = new Point(150, 250);
bRight.Width = 75;
bRight.Height = 25;
bRight.Text = "RIGHT";
bUp = new Button();
bUp.Location = new Point(75, 220);
bUp.Width = 75;
bUp.Height = 25;
bUp.Text = "UP";
bDown = new Button();
bDown.Location = new Point(75, 280);
bDown.Width = 75;
bDown.Height = 25;
bDown.Text = "DOWN";
bStart = new Button();
bStart.Location = new Point(240, 165);
bStart.Width = 75;
bStart.Height = 25;
bStart.Text = "START";
bReset = new Button();
bReset.Location = new Point(320, 165);
bReset.Width = 75;
bReset.Height = 25;
bReset.Text = "RESET";
txtVnes = new TextBox();
txtVnes.Location = new Point(240, 10);
txtVnes.Width = 160;
txtVnes.Height = 130;
txtVnes.Multiline = true;
txtGoal = new TextBox();
txtGoal.Width = 75;
txtGoal.Height = 25;
txtGoal.Location = new Point(330, 200);
lblCovece = new Label();
lblCovece.Location = new Point(160,165);
lblCovece.Width = 20;
lblCovece.Height = 20;
lblCovece.TextAlign = ContentAlignment.MiddleCenter;
lblCovece.Text = "O";
lblCovece.BackColor = Color.FromArgb(255, 0, 0);
int a = 0;
pole = new Label[42];
this.Controls.Add(lblCovece);
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 7; j++)
{
l = new Label();
l.Name = "label" + i.ToString() + j.ToString();
l.Text = "Z";
l.Width = 20;
l.Height = 20;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Parent = this;
l.BackColor = Color.FromArgb(100, 149, 237);
l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
pole[a] = l;
this.Controls.Add(l);
a++;
}
}
this.Width = 460;
this.Height = 380;
this.Controls.Add(l2);
this.Controls.Add(l4);
this.Controls.Add(lblResultE);
this.Controls.Add(lblTimeE);
this.Controls.Add(bStart);
this.Controls.Add(bReset);
this.Controls.Add(txtGoal);
this.Controls.Add(txtVnes);
this.Controls.Add(bUp);
this.Controls.Add(bLeft);
this.Controls.Add(bRight);
this.Controls.Add(bDown);
this.Controls.Add(bCheck);
bStart.Click+=new EventHandler(bStart_Click);
bUp.Click+=new EventHandler(bUp_Click);
bDown.Click+=new EventHandler(bDown_Click);
bLeft.Click+=new EventHandler(bLeft_Click);
bRight.Click+=new EventHandler(bRight_Click);
bCheck.Click+=new EventHandler(bZemaj_Click);
bReset.Click+=new EventHandler(bReset_Click);
}
private void bStart_Click(object sender, EventArgs e)
{
string Str = txtGoal.Text.Trim();
int Num;
bool isNum = int.TryParse(Str, out Num);
if (isNum && Str.Length == 1)
{
string[] ts = txtVnes.Text.Split(
new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
if (LineIsValid(ts[i]))
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = ts[i][2 * col].ToString();
}
row++;
}
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
}
}
}
else
{
MessageBox.Show("Invalid Input");
}
}
private static Regex regex = new Regex(#"^(\s)*(\d ){6}\d(\s)*$");
private static bool LineIsValid(string line)
{
return regex.IsMatch(line);
}
private void bReset_Click(object sender, EventArgs e)
{
txtVnes.Clear();
string[] ts = txtVnes.Text.Split(new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor=Color.FromArgb(100, 149, 237);
}
row++;
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor = Color.FromArgb(100, 149, 237);
}
}
txtGoal.Clear();
lblCovece.Location=new Point(160,165);
}
private void bUp_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y -
25);
}
private void bDown_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y +
25);
}
private void bLeft_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X - 25,
lblCovece.Location.Y);
}
private void bRight_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X + 25,
lblCovece.Location.Y);
}
private void bCheck_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
What makes your programm complicated and difficult to understand, is that you mix game logic with display logic.
I suggest you to redesign your game. It could look something like this:
public enum State
{
Empty, // Displays "Z"
Neutral, // Blue
Good, // Green
Bad // Red
}
public class Square
{
public int Number { get; set; }
public State State { get; set; }
}
public class Game
{
public const int Width = 7, Height = 6;
public Game()
{
Board = new Square[Width, Height];
}
public event EventHandler GameChanged;
public Square[,] Board { get; private set; }
public int CurrentX { get; private set; }
public int CurrentY { get; private set; }
public void Reset()
{
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
Board[x, y].State = State.Empty; // Always displayed in blue as "Z"
}
}
OnGameChanged();
}
public void MoveLeft()
{
if (CurrentX > 0) {
CurrentX--;
OnGameChanged();
}
}
public void MoveRight()
{
if (CurrentX < Width - 1) {
CurrentX++;
OnGameChanged();
}
}
// and so on
private void OnGameChanged()
{
EventHandler eh = GameChanged;
if (eh != null) {
eh(this, EventArgs.Empty);
}
}
}
In the form I would define pole to be a matrix as well (like the board). I show only a few relevant parts of the form code, to give you an idea of what I mean:
public class Form1 : Form
{
private Game game;
private Label[,] pole;
public Form1()
{
game = new Game();
game.GameChanged += new EventHandler(Game_GameChanged);
pole = new Label[Game.Width, Game.Height];
// Intialize pole.
// ...
}
void Game_GameChanged(object sender, EventArgs e)
{
for (int x = 0; x < Game.Width; x++) {
for (int y = 0; y < Game.Height; y++) {
Square square = game.Board[x, y];
Label label = pole[x,y];
switch (square.State) {
case State.Empty:
label.BackColor = Color.Blue;
label.Text = "Z";
break;
case State.Neutral:
label.BackColor = Color.Blue;
label.Text = square.Number.ToString();
break;
case State.Good:
label.BackColor = Color.Green;
label.Text = square.Number.ToString();
break;
case State.Bad:
label.BackColor = Color.Red;
label.Text = square.Number.ToString();
break;
default:
break;
}
}
}
// Place lblCovece according to game.CurrentX and game.CurrentY
// ...
}
private void bReset_Click(object sender, EventArgs e)
{
game.Reset();
}
private void bLeft_Click(object sender, EventArgs e)
{
game.MoveLeft();
}
private void bRight_Click(object sender, EventArgs e)
{
game.MoveRight();
}
}
Note how the Game class tells the form when changes happen through the event GameChanged. The form then updates the game display. In the game class, you can now concentrate on the game logic and do not have to deal with buttons and labels. You can also work with logical coordinates in the range [0..6] and [0..5] of the game board. This is easier than working with pixels. You delegate all the pixel calculations to the form.
My example is not complete, but when you try to implement it, you will see that it will be much easier to think about how the logic of the game should work.
Add an int score;
private void bCheck_Click(object sender, EventArgs e)
{
bool found = false;
foreach (Label l in pole)
{
if (l.Location == lblCovece.Location && l.Text == txtGoal.Text)
{
l.BackColor = Color.Green;
score += int.Parse(l.Text);
lblResultE.Text = score.ToString();
found = true;
}
}
if (!found)
{
score -= 10;
lblResultE.Text = score.ToString();
}
}
Related
I am working on a project, but I face a problem. I cannot find the solution, maybe you can help me.
Below you can see the details of my code:
private void InitializeComponent()
{
this.BKezdes = new System.Windows.Forms.Button();
this.PPalya = new System.Windows.Forms.Panel();
this.LPalyaMerete = new System.Windows.Forms.Label();
this.CBPalyaMerete = new System.Windows.Forms.ComboBox();
this.PKezelo = new System.Windows.Forms.Panel();
this.PKezelo.SuspendLayout();
this.SuspendLayout();
//
// BKezdes
//
this.BKezdes.Location = new System.Drawing.Point(1133, 247);
this.BKezdes.Name = "BKezdes";
this.BKezdes.Size = new System.Drawing.Size(208, 64);
this.BKezdes.TabIndex = 0;
this.BKezdes.Text = "Kezdés";
this.BKezdes.UseVisualStyleBackColor = true;
this.BKezdes.Click += new System.EventHandler(this.BKezdes_Click);
//
// PPalya
//
this.PPalya.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.PPalya.Location = new System.Drawing.Point(12, 12);
this.PPalya.Name = "PPalya";
this.PPalya.Size = new System.Drawing.Size(773, 651);
this.PPalya.TabIndex = 1;
//
// PKezelo
//
this.PKezelo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.PKezelo.Controls.Add(this.CBPalyaMerete);
this.PKezelo.Controls.Add(this.LPalyaMerete);
this.PKezelo.Location = new System.Drawing.Point(784, 12);
this.PKezelo.Name = "PKezelo";
this.PKezelo.Size = new System.Drawing.Size(218, 651);
this.PKezelo.TabIndex = 2;
//
// Fkepernyo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1461, 757);
this.Controls.Add(this.BKezdes);
this.Controls.Add(this.PKezelo);
this.Controls.Add(this.PPalya);
this.Name = "Fkepernyo";
this.Text = "Amolba";
this.PKezelo.ResumeLayout(false);
this.PKezelo.PerformLayout();
this.ResumeLayout(false);
}
public partial class Fkepernyo : Form
{
Mezo m;
List<Mezo> mezok;
public Fkepernyo()
{
InitializeComponent();
}
private void BKezdes_Click(object sender, EventArgs e)
{
mezok = new List<Mezo>();
int xKezdes = 1;
int yKezdes;
int width = 20;
int height = 20;
//MessageBox.Show(PPalya.Width + " " + PPalya.Height);
for (int i = 0; i <2; i++)
{
yKezdes = 1;
for (int j = 0; j < 2; j++)
{
m = new Mezo(xKezdes, yKezdes, width, height, 1);
PPalya.Controls.Add(m);
mezok.Add(m);
//MessageBox.Show(""+m.Location);
//MessageBox.Show(i + " " + j);
yKezdes += width;
}
xKezdes += height;
}
/*foreach (var item in mezok)
{
PPalya.Controls.Add(item);
}*/
}
void PBPalyaElemek_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
MessageBox.Show("Left button clicked");
}
private void CBPalyaMerete_SelectedIndexChanged(object sender, EventArgs e)
{
}0
}
public Mezo(int x, int y, int width, int height,int kep)
{
PictureBox PBPalyaElemek;
PBPalyaElemek = new PictureBox();
PBPalyaElemek.Width = width;
PBPalyaElemek.Height = height;
var redX = new Bitmap(rings_and_x_s.Properties.Resources.red_x);
var greenCircle = new Bitmap(rings_and_x_s.Properties.Resources.green_circle);
if (kep == 1)
{
PBPalyaElemek.Image = greenCircle;
}
else
{
PBPalyaElemek.Image = redX;
}
PBPalyaElemek.Location = new Point(x, y);
PBPalyaElemek.SizeMode = PictureBoxSizeMode.Zoom;
PBPalyaElemek.MouseClick += PBPalyaElemek_MouseClick;
PBPalyaElemek.BorderStyle = BorderStyle.Fixed3D;
Controls.Add(PBPalyaElemek);
}
There are two problems:
When I try to display the images on the screen, it only appears in 100 * 55 rectangles.
When I give the instruction to generate 4 picture boxes, the first one appears and the others do not.
I would appreciate any idea which help me to find a solution.
public Mezo(int x, int y, int width, int height, int kep)
{
X = x;
Y = y;
OWidth = width;
OHeight = height;
Kep = kep;
}
public int X { get; set; }
public int Y { get; set; }
public int OWidth { get; set; }
public int OHeight { get; set; }
public int Kep { get; set; }
public PictureBox Keszit()
{
PictureBox PBPalyaElemek;
PBPalyaElemek = new PictureBox();
PBPalyaElemek.Width = this.OWidth;
PBPalyaElemek.Height = this.OHeight;
var redX = new Bitmap(rings_and_x_s.Properties.Resources.red_x);
var greenCircle = new Bitmap(rings_and_x_s.Properties.Resources.green_circle);
if (this.Kep == 1)
{
PBPalyaElemek.Image = greenCircle;
}
else
{
PBPalyaElemek.Image = redX;
}
PBPalyaElemek.Location = new Point(this.X, this.Y);
PBPalyaElemek.SizeMode = PictureBoxSizeMode.Zoom;
PBPalyaElemek.MouseClick += PBPalyaElemek_MouseClick;
PBPalyaElemek.BorderStyle = BorderStyle.Fixed3D;
Controls.Add(PBPalyaElemek);
return PBPalyaElemek;
}
public MouseEventHandler PBPalyaElemek_MouseClick { get; }
Mezo m;
List<PictureBox> pbk;
public Fkepernyo()
{
InitializeComponent();
}
private void BKezdes_Click(object sender, EventArgs e)
{
pbk = new List<PictureBox>();
int meret = CBPalyaMerete.SelectedIndex+3;
int xKezdes = 6;
int yKezdes;
int width = 20;
int height = 20;
//MessageBox.Show(PPalya.Width + " " + PPalya.Height);
for (int i = 0; i <20; i++)
{
yKezdes = 6;
for (int j = 0; j < 20; j++)
{
m = new Mezo(xKezdes, yKezdes, width, height, 1);
PPalya.Controls.Add(m.Keszit());
pbk.Add(m.Keszit());
//MessageBox.Show(""+m.Location);
//MessageBox.Show(i + " " + j);
yKezdes += width;
}
xKezdes += height;
}
foreach (var item in pbk)
{
MessageBox.Show($"{item.Location.X} {item.Location.Y}");
//PPalya.Controls.Add(item);
}
BKezdes.Visible = false;
}
thx for dani he is foud the soltion.
I have a form like this. You can see my dynamic groupbox (30 groupbox) in Flowlayout on the right, and a picturebox to show questions on the left.
I'm worndering that How to use Click event for each groupbox to show the question equivalent in Picturebox?
Here's my code of groupbox
public GroupBox gbx(String name, int s, string i, string msch)
{
this.Name = int.Parse(i);
this.sda = s;
this.MsCauHoi = msch;
// gb
//
if (s == 2)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
}
if (s == 3)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
}
if (s == 4)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
gb.Controls.Add(cb4);
}
gb.Location = new System.Drawing.Point(219, 44);
gb.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Name = name;
gb.Padding = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Size = new System.Drawing.Size(120, 45);
gb.TabIndex = 7;
gb.TabStop = false;
gb.Text = i;
gb.BackColor = Color.Silver;
//
// cb1
//
cb1.AutoSize = true;
cb1.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb1.Location = new System.Drawing.Point(15, 13);
cb1.Margin = new System.Windows.Forms.Padding(0);
cb1.Name = "cb1";
cb1.Size = new System.Drawing.Size(17, 31);
cb1.TabIndex = 0;
cb1.Text = "1";
cb1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb1.UseVisualStyleBackColor = true;
cb1.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb2
//
cb2.AutoSize = true;
cb2.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb2.Location = new System.Drawing.Point(35, 13);
cb2.Name = "cb2";
cb2.Size = new System.Drawing.Size(17, 31);
cb2.TabIndex = 1;
cb2.Text = "2";
cb2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb2.UseVisualStyleBackColor = true;
cb2.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb3
//
cb3.AutoSize = true;
cb3.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb3.Location = new System.Drawing.Point(58, 13);
cb3.Name = "cb3";
cb3.Size = new System.Drawing.Size(17, 31);
cb3.TabIndex = 2;
cb3.Text = "3";
cb3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb3.UseVisualStyleBackColor = true;
cb3.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb4
//
cb4.AutoSize = true;
cb4.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb4.Location = new System.Drawing.Point(81, 13);
cb4.Name = "cb4";
cb4.Size = new System.Drawing.Size(17, 31);
cb4.TabIndex = 3;
cb4.Text = "4";
cb4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb4.UseVisualStyleBackColor = true;
cb4.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
return gb;
}
And here's the code of FormLoad
private void FrmThi_Load(object sender, EventArgs e)
{
droptable();
this.CauDaLam = 0;
if (dethi == null) setdethi();
Screen scr = Screen.PrimaryScreen; //đi lấy màn hình chính
this.Left = (scr.WorkingArea.Width - this.Width) / 2;
this.Top = (scr.WorkingArea.Height - this.Height) / 2;
int i = 0;
foreach (DataRow row in this.dethi.Rows)
{
String myValue = row["SoDA"].ToString();
String msch = row["MaCH"].ToString();
ptl = new FrmPhieuTraLoi();
pn_DeThi.Controls.Add(ptl.gbx("cau" + (i + 1).ToString(), int.Parse(myValue), (i + 1).ToString(), msch));
listptl.Add(ptl);
i++;
}
loadcauhoi(this.CauDangLam);
listptl[CauDangLam].setBackColorCDL();
Random r = new Random();
lbmade1.Text = r.Next(1, 4).ToString();
txt = lbSatHachBangLai.Text;
len = txt.Length;
lbSatHachBangLai.Text = "";
timer1.Start();
this.timer2.Start();
}
This is an example code:
private void Form1_Load(object sender, EventArgs e)
{
for(int idx = 0; idx < 5; idx++)
{
var gBox = new GroupBox();
gBox.Height = 50;
gBox.Width = 50;
gBox.Text = "Box: " + idx;
gBox.Tag = idx;
gBox.MouseClick += GBox_MouseClick;
this.Controls.Add(gBox);
}
}
private void GBox_MouseClick(object sender, MouseEventArgs e)
{
//var ctrl = sender as Control; -- Not required
int questionIdx = (int)(sender as Control).Tag;
}
I would extend the GroupBox control and add a property for the Question ID (Add new Item > Custom Control) and change the class to look like this:
public partial class QuestionGroupBox : GroupBox
{
public string QuestionID;
public QuestionGroupBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
Then populate your flowlayout and set the QuestionID and Click EventHandler, similar to this:
var gb = new QuestionGroupBox{ QuestionID = "44"};
gb.Click += new System.EventHandler(this.questionGroupBox_Click);
//add add to your flow layout
Finally create your event handler to get and display the question
private void questionGroupBox_Click(object sender, EventArgs e)
{
var questionID = ((QuestionGroupBox)sender).QuestionID;
//display your question
}
If I inputted 5, it will generate a square matrix by 5 (5x5)
How do I multiply 1, 7, 13, 19, 25?
Is there any applicable algorithm for my code so I could multiply the diagonals or do i need to rewrite a new one?
public partial class Form1 : Form {
public int e = 0;
int Row = 0;
int Column = 0;
int YAxisPosition = 0;
int XAxisPosition = 0;
int Counter = 0;
int PositionalValue = 0;
TextBox[] MyTextBoxDimA = new TextBox[999999];
TextBox tbRow = new TextBox();
Button MyButton = new Button();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AutoScroll = true;
WindowState = System.Windows.Forms.FormWindowState.Maximized;
//GENERATING THE SIZE BUTTON
tbRow = new TextBox();
tbRow.Text = "5";
tbRow.Size = new Size(100, 10);
tbRow.Location = new Point(0, 0);
Controls.Add(tbRow);
//GENERATE MATRIX BUTTON
MyButton = new Button();
MyButton.Text = "GENERATE MATRIX";
MyButton.Size = new Size(200, 25);
MyButton.Click += new EventHandler(MyButton_Click);
MyButton.Location = new Point(0, 30);
Controls.Add(MyButton);
}
public void MyButton_Click(object sender, EventArgs ee) {
//CODE FOR GENERATING MATRIX A
e = 1;
PositionalValue = 1;
Counter = 1;
//POSITION
YAxisPosition = 60;
XAxisPosition = 0;
Row = Convert.ToInt32(tbRow.Text);
Column = Convert.ToInt32(tbRow.Text);
while (Row >= e) {
while (Column >= Counter) {
MyTextBoxDimA[PositionalValue] = new TextBox();
MyTextBoxDimA[PositionalValue].Location =
new Point(XAxisPosition, YAxisPosition); //coordinates (start)
MyTextBoxDimA[PositionalValue].Size = new Size(70, 10);
MyTextBoxDimA[PositionalValue].Text = Convert.ToString(PositionalValue);
Controls.Add(MyTextBoxDimA[PositionalValue]);
XAxisPosition = XAxisPosition + 100;
PositionalValue++;
Counter++;
}
YAxisPosition = YAxisPosition + 50;
Counter = 1;
e++;
XAxisPosition = 0;
}
}
}
If you are looking for an algorithm:
static void Main(string[] args)
{
int n = 5;
int ans = 1;
int current = 1;
for (int i = 1; i <= n; i++)
{
ans = ans * current;
current += n + 1;
}
Console.WriteLine(ans);
}
Just loop from zero to the size of the matrix. The index will be used to get each diagonal value (this code assumes that each textbox contains an integer and is not blank):
int matrixSize = 5;
int product = 1;
for (int i = 0; i < matrixSize; i++)
{
product *= int.Parse(MyTextBoxDimA[i,i].Text);
}
I have a panel in Visual Studio/windows form app.But I coulnd't add pictureBox on it with code.It is working if I work without panel however I need it.MyCodes:
PictureBox[] pipe = new PictureBox[3];
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);}
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
this.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = 30;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}
You can use panelName.Controls.Add(temp), and i advise you to change a top of PictureBox in for loop to view all PictureBox, like this :
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
panelName.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = temp.Height * panelName.Controls.Count;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}
It's my problem.
I have a few RadioButtons. If i click on first Radiobutton on form create TextBox, if click on second - create second TextBox and if i click again on first RadioButton, again one TextBox,is it possible?
Give me idea, please.
And without property visible.
There isn't much point in dynamically creating and destroying controls here. It is just a headache, ensuring the position, size and tab order is correct. Just make the text box visible if you like the choice:
private void radioButton2_CheckedChanged(object sender, EventArgs e) {
textBox1.Visible = radioButton2.Checked;
}
Set the textbox' Visible property to False in the designer.
Try something like this (this is only one of them):
TextBox t;
private void radio_CheckedChanged(object sender, System.EventArgs e)
{
if (radio.Checked) {
t = new TextBox();
t.Top = radio.Top;
t.Left = radio.Left + radio.Width;
this.Controls.Add(t);
t.Show();
} else {
if (t!=null)t.Dispose();
}
}
See Only foreach and void TextBoxes.
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
private const int
HeightTextBox = 40, WidthTextBox = 25, //размер textboxes
DistanceBetweenTexBoxHeight = 25, DistanceBetweenTexboxWigth = 25; //растояние между ними
private int DimentionalTextBox = 3;
private const int
RadioButtonNumbers = 3, // количество радио кнопок
DistanceBetweenRadiobutton = 50,
RadioButtonFirstGroupStartPositionX = 5,
RadioButtonSecondGroupStartPositionX = 0,
RadioButtonFirstGroupStartPositionY = 0,
RadioButtonSecondGroupStartPositionY = 0,
RadioButtonSize = 25;
public MyForm()
{
//Size of window
ClientSize = new System.Drawing.Size(7 * HeightTextBox + 8 * DistanceBetweenTexBoxHeight,
7 * WidthTextBox + 8 * DistanceBetweenTexboxWigth);
//Create RaioButton
int x = RadioButtonFirstGroupStartPositionX;
int y;
RadioButton[] DimRadioButtons = new RadioButton[RadioButtonNumbers];
for (int i = 0; i < RadioButtonNumbers; i++)
{
DimRadioButtons[i] = new RadioButton();
DimRadioButtons[i].Name = "RadioButton" + (i + 2);
DimRadioButtons[i].Text = Convert.ToString(i + 2);
DimRadioButtons[i].SetBounds(x, RadioButtonFirstGroupStartPositionY, RadioButtonSize, RadioButtonSize);
x += DistanceBetweenRadiobutton;
Controls.Add(DimRadioButtons[i]);
}
//Watch dimention
// And catch even click on RadioButton
foreach (var a in this.Controls)
{
if (a is RadioButton)
{
if (((RadioButton)a).Checked)
{
DimentionalTextBox = Convert.ToInt16(((RadioButton)a).Text);
((RadioButton)a).Click += new EventHandler(this.TextBoxes);
}
}
}
}
// Create-Delete TextBoxes
private void TextBoxes(object sender, EventArgs e)
{
RadioButton rb_click = (RadioButton)sender;
int x = RadioButtonFirstGroupStartPositionX;
int y = 30;
int dim = Convert.ToInt16(rb_click.Text);
TextBox[,] MatrixTextBoxes = new TextBox[dim, dim];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
MatrixTextBoxes[i, j] = new TextBox();
MatrixTextBoxes[i, j].Top = rb_click.Top;
MatrixTextBoxes[i, j].Name = "MatrixTextBox" + i + j;
MatrixTextBoxes[i, j].Text = i + " " + j;
MatrixTextBoxes[i, j].SetBounds(x, y, WidthTextBox, HeightTextBox);
x += DistanceBetweenTexboxWigth;
this.Controls.Add(MatrixTextBoxes[i, j]);
MatrixTextBoxes[i, j].Show();
}
y += DistanceBetweenTexBoxHeight;
x = RadioButtonFirstGroupStartPositionX;
}
}
}
class MyClassMain : MyForm
{
public static void Main()
{
Application.Run(new MyClassMain());
}
}