Dev express Grid View – Column Visible Index property giving un expected output - c#

I have dev express grid view, and trying to set column visibility run time, Some columns are not appearing in correct order i.e. RebateAmount i want to set to appear at 4th position but its appearing always on second position, any idea what's wrong in below code? i want all columns should be appear at position which i have set in visibility index.
if (currentColum.FieldName.Equals("TaxName"))
{
currentColum.Caption = #"abc";
}
else if (intGridType == 1 || intGridType == 0)//Both
{
if (currentColum.FieldName.Equals("PastCurrentCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 15;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionVyajTotal"))
{
currentColum.Caption = #"Äyij";
currentColum.VisibleIndex = 16;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionNoticeFeeTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 17;
}
else if (currentColum.FieldName.Equals("RebateAmount"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 18;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionTotalTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 19;
}
else
currentColum.Visible = false;
}
else if (intGridType == 2)//Only Past
{
if (currentColum.FieldName.Equals("PastCollection"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 6;
}
else if (currentColum.FieldName.Equals("PastCollectionVyaj"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 7;
}
else if (currentColum.FieldName.Equals("PastCollectionNoticeFee"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 8;
}
else if (currentColum.FieldName.Equals("PastCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 9;
}
else
currentColum.Visible = false;
System.Diagnostics.Debug.Print(currentColum.VisibleIndex.ToString() + currentColum.Name);
}
else if (intGridType == 3) //Only Current
{
if (currentColum.FieldName.Equals("CurrentCollection"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 10;
}
else if (currentColum.FieldName.Equals("CurrentCollectionVyaj"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 11;
}
else if (currentColum.FieldName.Equals("CurrentCollectionNoticeFee"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 12;
}
else if (currentColum.FieldName.Equals("CurrentCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 13;
}
else if (currentColum.FieldName.Equals("RebateAmount"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 14;
}
else
currentColum.Visible = false;
}

Hide all columns and active ( Visible = true) one at a time in order you want

Related

DataGridView - AllowUserToDeleteRows

Hey people I'm having trouble to deny row deletion if the value of a cell equals 'Y'.
I tried with:
for (int i = 0; i < dGridView.Rows.Count; i++)
{
dGridView.Rows[i].Cells["A"].ReadOnly = true;
dGridView.Columns["A"].DefaultCellStyle.BackColor = Color.Gray;
dGridView.Rows[i].Cells["B"].ReadOnly = true;
dGridView.Columns["B"].DefaultCellStyle.BackColor = Color.Gray;
if (dGridView.Rows[i].Cells["A"].Value != null && dGridView.Rows[i].Cells["A"].Value.Equals("Y"))
{
dGridView.Rows[i].Cells["F"].ReadOnly = true;
dGridView.Rows[i].Cells["F"].Style.BackColor = Color.Gray;
dGridView.Rows[i].Cells["C"].ReadOnly = true;
dGridView.Rows[i].Cells["C"].Style.BackColor = Color.Gray;
dGridView.Rows[i].Cells["U"].ReadOnly = true;
dGridView.Rows[i].Cells["U"].Style.BackColor = Color.Gray;
**** dGridView.AllowUserToDeleteRows = false;
**** }
Could you kindly support me?
Thanks!
I know that:
dGridView.AllowUserToDeleteRows = false;
will deny deletion of all rows, but I can't find the solution.

Multiple buttons different properties loop

I'm setting up a windowsform that pings all my IP's at work and changes the button.BackColor accordingly.
Ping pingClassxx = new Ping();
PingReply pingxx = pingClassxx.Send("192.168.xx.xx");
if (pingxx.Status == IPStatus.Success)
{
button1.BackColor = Color.Green;
button1.Text = "SQL Server | Online";
}
else
{
button1.BackColor = Color.Red;
button1.Text = "SQL Server | Offline";
}
Now I'm trying to do this to multiple machines so I went like this:
private void timer0_Tick(object sender, EventArgs e)
{
timer1.Stop();
int iCount0 = 0;
string[] arr = new string[8];
arr[0] = "192.168.x.xx"; //button0
arr[1] = "192.168.x.yy"; //button1
arr[2] = "192.168.x.zz"; //button2
arr[3] = "192.168.x.xy"; //button3
arr[4] = "192.168.x.xz"; //button4
arr[5] = "192.168.x.yx"; //button5
arr[6] = "192.168.yy.yz"; //button6
arr[7] = "192.168.x.ww"; //button7
for (int I = 0; I < arr.Length ; I++)
{
// Ping
string s = arr[I];
Console.WriteLine(s);
Ping pingClassx = new Ping();
PingReply pingx = pingClassx.Send(arr[I]);
if (pingx.Status == IPStatus.Success)
{
[What do I do here?].BackColor = Color.Green;
}
else
{
[What do I do here?]BackColor = Color.Red;
}
// Loading Bar
iCount2 += 1;
progressBar0.Value = iCount2 * 100 / 8;
label0.Text = iCount2 + "/8";
}
timer1.Start();
}
How can I loop this?
I could just repeat the code but I am sure there is a fancier way to do it.
If you want to Change the Color only Of Your Form Buttons then try This :
foreach(Button c in Form.Controls){
if (pingx.Status == IPStatus.Success)
{
c.BackColor = Color.Green;
}
else
{
c.BackColor = Color.Red;
}
}

Click event restart

I'm using c# windows form in Visual studio 2017 version
I've made a high low game for practice and it works perfect but what I'm trying to accomplish is once someone hits 10 guesses it starts the application over again.
This was my last resort coming here and asking but i have no one else to turn to. I've tried all kinds of things to make it work when hitting 10 guesses to restart the app but nothing works and i get errors.
here's the start button and the guess button
variables I'm using
static int intRandomNumber;
static int intNumGuesses;
static int intBestLowScore;
static int intGuessedNum;
static int difference = 0;
Start Button
{
//Random Numbers//
Random rnRandomNumber = new Random();
intRandomNumber = rnRandomNumber.Next(0, 1000);
lblRandomNumber.Text = intRandomNumber.ToString();
txtGuess.Enabled = true;
btnGuess.Enabled = true;
btnStart.Enabled = false;
lblAnswer.Text = "".ToString();
intBestLowScore = intNumGuesses;
intNumGuesses = 0;
lblNumGuesses.Text = "0";
lblBestScore.Text = intBestLowScore.ToString();
lblAnswer.BackColor = Color.White;
txtGuess.Focus();
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Stop();
}
Guess Button
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Play();
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
You're missing an if statement
Guess Button:
private void btnGuess_Click(object sender, EventArgs e)
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
//This is what you're looking for-v
if(intNumGuesses==10)
{
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
intNumGuesses=0;
}
//This is what you're looking for-^
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
In your 'guess-button' handler do the following:
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
bool guessed = false;
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
...
}
else if (intRandomNumber - intGuessedNum > difference)
{
...
}
else
{
...
guessed = true;
}
if ((intNumGuesses == 10}&&(!guessed))
{
// Show Message "max. nr. of guesses reached'
// call method to clear values from textBoxes & enable Start-button
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
What about this?
{
intNumGuesses++;
if(intNumGuesses >= 10)
{
StartButton(); // pseudo call, replace with whatever the name and parameters of your method really are
return;
}
lblNumGuesses.Text = intNumGuesses.ToString();
// ...
}
I would recommend to write an Init()method, that you call either from your StartButton() method or from your GuessButton() method when necessary

How to add sprites in array C#?

I want to access game objects (sprites) in my code in an Array but I have no idea how to do that, I am new to C#.
This is my testing code, but I am not getting any values:
void Start()
{
Sprite[] countries = Resources.LoadAll<Sprite>("MAPS");
sr = GetComponent<SpriteRenderer>();
names = new string[countries.Length];
for (int i = 0; i < names.Length; i++)
{
names[i] = countries[i].name;
//0 = red
//1 = green
if (UnityEngine.Random.Range(0, 2) == 0)
{
this.GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
this.GetComponent<SpriteRenderer>().color = Color.green;
}
}
}
Instead of using this keyword:
if (UnityEngine.Random.Range(0, 2) == 0)
{
this.GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
this.GetComponent<SpriteRenderer>().color = Color.green;
}
try to use countries[i]:
if (UnityEngine.Random.Range(0, 2) == 0)
{
countries[i].GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
countries[i].GetComponent<SpriteRenderer>().color = Color.green;
}

How to assign a variable/integer to a image? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm super new to c# and i have got a problem i have been trying to figure out for the past couple of days anyways i am trying to make a blackjack game but i cant figure out for the life of me how to associate the images of the random cards that show up in the picture box's to a variable/integer so i can make a function to add the values of the cards, if y'all could give me some ideas that would be great!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace pokerapp
{
public partial class pokergame : Form
{
//Card Varibles
int card1 = 10;
int card2 = 10;
int card3 = 10;
int card4 = 10;
int card5 = 10;
int card6 = 10;
int card7 = 10;
int card8 = 10;
int card9 = 10;
int card10 = 10;
int card11 = 10;
int card12 = 10;
int card13 = 10;
int card14 = 10;
int card15 = 10;
int card16 = 10;
int card17 = 10;
int card18 = 10;
int card19 = 10;
int card20 = 10;
int card21 = 9;
int card22 = 9;
int card23 = 9;
int card24 = 9;
int card25 = 8;
int card26 = 8;
int card27 = 8;
int card28 = 8;
int card29 = 7;
int card30 = 7;
int card31 = 7;
int card32 = 7;
int card33 = 6;
int card34 = 6;
int card35 = 6;
int card36 = 6;
int card37 = 5;
int card38 = 5;
int card39 = 5;
int card40 = 5;
int card41 = 4;
int card42 = 4;
int card43 = 4;
int card44 = 4;
int card45 = 3;
int card46 = 3;
int card47 = 3;
int card48 = 3;
int card49 = 2;
int card50 = 2;
int card51 = 2;
int card52 = 2;
public pokergame()
{
InitializeComponent();
}
Random random = new Random();
List<Image> images = new List<Image>();
public void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = images[random.Next(0, images.Count - 1)];
pictureBox2.Image = images[random.Next(0, images.Count - 1)];
pictureBox14.Image = images[random.Next(0, images.Count - 1)];
pictureBox13.Image = images[random.Next(0, images.Count - 1)];
button2.Enabled = true;
button1.Enabled = false;
}
public void pokergame_Load(object sender, EventArgs e)
{
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
pictureBox8.Visible = false;
pictureBox9.Visible = false;
pictureBox10.Visible = false;
pictureBox11.Visible = false;
pictureBox12.Visible = false;
//label1.Text = Form1.textBox1.text;
button1.Enabled = true;
button2.Enabled = false;
images.Add(pokerapp.Properties.Resources._1);
images.Add(pokerapp.Properties.Resources._2);
images.Add(pokerapp.Properties.Resources._3);
images.Add(pokerapp.Properties.Resources._4);
images.Add(pokerapp.Properties.Resources._5);
images.Add(pokerapp.Properties.Resources._6);
images.Add(pokerapp.Properties.Resources._7);
images.Add(pokerapp.Properties.Resources._8);
images.Add(pokerapp.Properties.Resources._9);
images.Add(pokerapp.Properties.Resources._10);
images.Add(pokerapp.Properties.Resources._11);
images.Add(pokerapp.Properties.Resources._12);
images.Add(pokerapp.Properties.Resources._13);
images.Add(pokerapp.Properties.Resources._14);
images.Add(pokerapp.Properties.Resources._15);
images.Add(pokerapp.Properties.Resources._16);
images.Add(pokerapp.Properties.Resources._17);
images.Add(pokerapp.Properties.Resources._18);
images.Add(pokerapp.Properties.Resources._19);
images.Add(pokerapp.Properties.Resources._20);
images.Add(pokerapp.Properties.Resources._21);
images.Add(pokerapp.Properties.Resources._22);
images.Add(pokerapp.Properties.Resources._23);
images.Add(pokerapp.Properties.Resources._24);
images.Add(pokerapp.Properties.Resources._25);
images.Add(pokerapp.Properties.Resources._26);
images.Add(pokerapp.Properties.Resources._27);
images.Add(pokerapp.Properties.Resources._28);
images.Add(pokerapp.Properties.Resources._29);
images.Add(pokerapp.Properties.Resources._30);
images.Add(pokerapp.Properties.Resources._31);
images.Add(pokerapp.Properties.Resources._32);
images.Add(pokerapp.Properties.Resources._33);
images.Add(pokerapp.Properties.Resources._34);
images.Add(pokerapp.Properties.Resources._35);
images.Add(pokerapp.Properties.Resources._36);
images.Add(pokerapp.Properties.Resources._37);
images.Add(pokerapp.Properties.Resources._38);
images.Add(pokerapp.Properties.Resources._39);
images.Add(pokerapp.Properties.Resources._40);
images.Add(pokerapp.Properties.Resources._41);
images.Add(pokerapp.Properties.Resources._42);
images.Add(pokerapp.Properties.Resources._43);
images.Add(pokerapp.Properties.Resources._44);
images.Add(pokerapp.Properties.Resources._45);
images.Add(pokerapp.Properties.Resources._46);
images.Add(pokerapp.Properties.Resources._47);
images.Add(pokerapp.Properties.Resources._48);
images.Add(pokerapp.Properties.Resources._49);
images.Add(pokerapp.Properties.Resources._50);
images.Add(pokerapp.Properties.Resources._51);
images.Add(pokerapp.Properties.Resources._52);
}
int hitButtonClickCount = 0;
public void button2_Click(object sender, EventArgs e)
{
Random random = new Random();
if (hitButtonClickCount == 0)
{
pictureBox3.Visible = true;
pictureBox3.Image = images[random.Next(0, images.Count - 1)];
//pictureBox3.Location = new Point(86, 130);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 1)
{
pictureBox4.Visible = true;
pictureBox4.Image = images[random.Next(0, images.Count - 1)];
//pictureBox4.Location = new Point(100, 140);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 2)
{
pictureBox5.Visible = true;
pictureBox5.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 3)
{
pictureBox6.Visible = true;
pictureBox6.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
}
else if (hitButtonClickCount == 4)
{
pictureBox7.Visible = true;
pictureBox7.Image = images[random.Next(0, images.Count - 1)];
// pictureBox5.Location = new Point(241, 197);
hitButtonClickCount++;
button2.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
pictureBox2.Image = null;
pictureBox3.Image = null;
pictureBox4.Image = null;
pictureBox5.Image = null;
pictureBox6.Image = null;
pictureBox7.Image = null;
pictureBox8.Image = null;
pictureBox9.Image = null;
pictureBox10.Image = null;
pictureBox11.Image = null;
pictureBox12.Image = null;
pictureBox13.Image = null;
pictureBox14.Image = null;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
pictureBox8.Visible = false;
pictureBox9.Visible = false;
pictureBox10.Visible = false;
pictureBox11.Visible = false;
pictureBox12.Visible = false;
button1.Enabled = true;
button2.Enabled = false;
hitButtonClickCount = 0;
}
public void Points()
{
if (pictureBox1.Image == images[0])
{
label3.Text = "1/10";
}
}
private void button4_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button4.Enabled = false;
}
}
}
First off, try and do some reading up on arrays and Lists. This will help shorten your code and make it easier for you to read.
With your question.
Try putting the cards in an array. and then pick a random number instead of a random image. Use this random number to select the correct corresponding card image and card value. You will only need to ensure that they are correctly lined up with each other.
You can access the addresses of a list the same way you would an array.
List.Items[random_int]

Categories