How can I display embedded images when the listView1 SelectedIndexChanged, I need to pull the image name based on the listview item and display it on the picturebox?
My code:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
label1.Text = listView1.SelectedItems[0].SubItems[0].Text + " " +
textBox2.Text;
var myimage = "_" + listView1.SelectedItems[0].SubItems[0].Text;
// new code tryout
// When I try to do it like this Code runs fine
// but no image is displayed on picturebox
object O = Resources.ResourceManager.GetObject("myimage");
pictureBox1.Image = (Image)O;
//no image displayed and no errors
}
else
{
label1.Text = string.Empty;
}
}
The problem here is that you are confusing the string "myimage" with the content of myimage variable.
You should use the variable's content and not the string as the GetObject parameter:
object O = Resources.ResourceManager.GetObject(myimage);
Based on the fact that I don't know how you did fill your ListView, I took the idea of yours and it works fine. Here's a modified code with a screenshot of the result :
private void View_SelectedIndexChanged(object sender, EventArgs e)
{
if (View.SelectedItems.Count > 0)
{
object O = Resources.ResourceManager.GetObject(View.SelectedItems[0].Text);
PicBox.Image = (Image)O;
}
}
Related
The problem:
When changing what image shows up in the cell of a DataGridViewImageColumn, the previous image is still there behind the new image:
(Note the red error behind the green checkmark)
What I have:
The image is showing status on connectivity to a machine. When the machine's status is updated an event is raised and the image is updated.
The declaration of the DataGridViewImageColumn:
DataGridViewImageColumn imc = new DataGridViewImageColumn
{
HeaderText = "C$",
Name = "imc",
Width = 25,
ImageLayout = DataGridViewImageCellLayout.Stretch,
ValuesAreIcons = true
};
Defaults are set:
sDGView.Columns["imc"].DefaultCellStyle.NullValue = null;
((DataGridViewImageCell)sDGView.Rows[0].Cells["imc"]).Value = null;
Event when a row is added:
private void SDGView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
((DataGridViewImageCell)sDGView.Rows[LastRow].Cells["imc"]).Value = null;
}
Method called when online status changed:
private void SetStatusImage(int ri)
{
var status = Core.Machines[ri].OnLine;
//sDGView.Rows[ri].Cells["imc"].Dispose();
var image = (DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"];
if (status is null)
{
image.Value = StatusImg[0];
}
else if (status is true)
{
image.Value = StatusImg[1];
}
else
{
image.Value = StatusImg[2];
}
}
The images:
public Icon[] StatusImg { get; private set; } = new Icon[]
{
Properties.Resources.Minus_Grey,
Properties.Resources.Tick_Green,
Properties.Resources.Error_Red
};
What I've tried...
I've tried setting the image.Value to null - no change
I've called the Dispose() method on the cell itself and then created a new cell to take its place and set the value to the current image -- the previous image still shows up behind!
It seems each time I change the value of the cell, another image is simply added on top. I can verify by watching the memory size of the program increase as the image changes. The old one never actually goes away even with calling the Dispose() method.
Edit:
I tried having the main form call the Refresh() method in case it just needed to be redrawn. - no change
I tried removing the image variable and setting the image directly:
private void SDGView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
((DataGridViewImageCell)sDGView.Rows[LastRow].Cells["imc"]).Value = StatusImg[0];
}
private void SetStatusImage(int ri)
{
var status = Core.Machines[ri].OnLine;
//sDGView.Rows[ri].Cells["imc"].Dispose();
//var image = (DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"];
if (status is null)
{
//image.Value = StatusImg[0];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[0];
}
else if (status is true)
{
//image.Value = StatusImg[1];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[1];
}
else
{
//image.Value = StatusImg[2];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[2];
}
}
--same result - previous image still persists behind the newly selected image.
(Note the red error behind the green checkmark)
I tested such scenario with this simple code:
private void PlanningDayPlans_Load(object sender, EventArgs e)
{
DataGridViewRow r = new DataGridViewRow();
this.DataGridView1.Rows.Add(r);
}
private void Button1_Click(object sender, EventArgs e)
{
this.DataGridView1.Rows(0).Cells(0).Value = My.Resources.todo2;
}
private void Button2_Click(object sender, EventArgs e)
{
this.DataGridView1.Rows(0).Cells(0).Value = My.Resources.cross;
}
And it works without issues:
The pictures are PNG with transparent backgrounds (would show the problem), I can switch them back and forth and it works well.
This is for un-bound and DataSource-less scenario.
I think I know where your problem probably is created. You set the ValuesAreIcons property, which affects alpha channel of the background so that it is "correct" for icons. I would try to ommit this settings and use PNG icons, it will be fine.
I have a project with SQlite database. I've saved images in folder " pics " ( into debug folder) & their name in the database ( column "docpic") .I set the label10 text as image name . how can I use label.text as image name (that saved in database) and show image in a picturebox ?
label.text = image.jpg and image name=image.jpg
infect I want to use label10.text as a imagename .
label10 click event :
private void label10_Click(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("\\scan\\" + label10.Text + ".jpg");
}
Gridview click event :
private void Grid_Click(object sender, EventArgs e)
{
i = Convert.ToInt32(DT.Rows[Grid.CurrentRowIndex]["id"]);
btnDel.Enabled = true;
btnEdit.Enabled = true;
label10.Text = DT.Rows[Grid.CurrentRowIndex]["docpic"].ToString();
}
You can try this:
picturebox.Image = Image.FromFile(#"C:\...\" + label10.text, true);
Just replace the "C:\...\" with the path to the image.
A better approach will be to use the value from the database, you don't really need to put the image as a text label:
picturebox.Image = Image.FromFile(#"C:\...\" + <value from DB>, true);
Or in your case:
picturebox.Image = Image.FromFile(#"C:\...\" + DT.Rows[Grid.CurrentRowIndex]["docpic"].ToString(), true);
I have a form that allows a user to add players to a roster, by entering the player name and selecting, from a combo box, the division to which the player belongs.
When time comes to add the player to my TreeView control, the node that should display the division selected displays this text instead: System.Data.DataRowView
I got the code to implement this through MSDN here: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx
Here's the code in the load function of the form, to fill the combo box:
private void frm_add_players_Load(object sender, EventArgs e)
{
Divisions divs = new Divisions();
Players players = new Players();
DataTable dtDivisions = divs.GetActiveDivisions(); //divisions combo box
DataTable dtPlayers = players.GetPlayersByTourID(this.tourID);
//set the forms datatable
this.dt_players = dtPlayers;
//fill the combo box
this.cmbo_divisions.DataSource = dtDivisions;
this.cmbo_divisions.DisplayMember = "title";
this.cmbo_divisions.ValueMember = "ID";
this.cmbo_divisions.SelectedIndex = -1;
this.cmbo_divisions.Text = "Select a Division";
//set treeview imagelist
this.tview_roster.ImageList = tview_imagelist;
this.tview_roster.ImageIndex = 1; //division icon
//fill treeview
foreach (DataRow dr in dtPlayers.Rows)
{
FillPlayerTreeview(dr);
}
//expand treeview
this.tview_roster.ExpandAll();
this.ActiveControl = this.txt_player_name;
}
Here I call the function to add the player to the TreeView:
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, selItem.ToString());
}
And here is the function that adds the player:
private void AddPlayerToTreeView(string playerName, string division)
{
TreeNode[] tns = this.tview_roster.Nodes.Find(division, false); //try to find the division, if exists
TreeNode tn = new TreeNode();
if (tns.Length > 0) //division exists - add player
{
tn = this.tview_roster.Nodes[tns[0].Index].Nodes.Add(playerName, playerName);
tn.ImageIndex = 0; //player icon
}
else //division doesn't exist - add division, then add player
{
tn = this.tview_roster.Nodes.Add(division, division);
tn.ImageIndex = 1; //division icon
AddPlayerToTreeView(playerName, division);
}
}
And the result is this:
I'm not sure why it won't work.. and I'm at a loss. Any help would be appreciated.
Well, well... maybe something like the following.
Access the combo's data source, which is a DataTable, and extract selected row and column value using selected index. Maybe add some error handling, too.
private void btn_add_Click(object sender, EventArgs e)
{
var data = cmbo_divisions.DataSource as DataTable;
var row = data.Rows[cmbo_divisions.SelectedIndex];
var selected = row["title"].ToString();
AddPlayerToTreeView(txt_player_name.Text, selected);
}
Try this :
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.SelectedItem as string);
}
ToString() will get the type name, but in that case the SelectedItem is a string.
Try with:
private void btn_add_Click(object sender, EventArgs e)
{
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.Items[cmbo_divisions.SelectedIndex].Text);
}
EDIT: Updated to a better way
I'm trying to implement an application in C# which generates me a QR Code. I've managed to do this, but I don't know how to call CreateQRImage(string inputData) inside the genButton_Click(object sender, EventArgs e).
I inside the windows form I have a textbox and a button, and I think that the function CreateQRImage must be called with the name of the textbox
Here is the code:
private void genButton_Click(object sender, EventArgs e)
{
}
public void CreateQRImage(string inputData)
{
if (inputData.Trim() == String.Empty)
{
System.Windows.Forms.MessageBox.Show("Data must not be empty.");
}
BarcodeWriter qrcoder = new ZXing.BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
Height = 250,
Width = 250
}
};
string tempFileName = System.IO.Path.GetTempPath() + inputData + ".png";
Image image;
String data = inputData;
var result = qrcoder.Write(inputData);
image = new Bitmap(result);
image.Save(tempFileName);
System.Diagnostics.Process.Start(tempFileName);
}
This should do the trick:
private void genButton_Click(object sender, EventArgs e)
{
// Assuming your text box name. Also assuming UI disables button until text is entered.
this.CreateQRImage(myTextBox.Text);
}
I want to do an if stament on an image
if (SortName.Image == Properties.Resources.RadioEmpty)
{
SortName.Image = Properties.Resources.Radio;
}
else
{
SortName.Image = Properties.Resources.RadioEmpty;
}
but its on working any idea what I'm doing wrong ?
o.k additional information
1.
//SortName = A picture box
//Properties.Resources.RadioEmpty = Resources\RadioEmpty.png
//Properties.Resources.Radio = Resources\Radio.png
2.
Nope no errors
3.I wanted to use a custom image for a radio button. A have a picture box with the above code on click. RadioEmpty is the default so I check to see if the picturebox's image is the same as image form the Resources folder is so do code.
i advise you use tag for this issue see this code
private void Form1_Load(object sender, EventArgs e)
{
//in form load the radio is checked or unckecked
//here my radio is unchecked at load
pictureBox1.Image = WindowsFormsApplication5.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//after pictiurebox clicked change the image and tag too
if (pictureBox1.Tag.ToString() == "Checked")
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
else
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Delete;
pictureBox1.Tag = "Checked";
}
}
compare the names. Something like this (unverified)
if (SortName.Image.Name.Equals(Properties.Resources.RadioEmpty.Name))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm1;
Bitmap bm2;
private void button1_Click(object sender, EventArgs e)
{
bm1 = new Bitmap(Properties.Resources.firegirl1);
bm2 = new Bitmap(Properties.Resources.Zemli2);
pictureBox1.Image = bm1;
pictureBox2.Image = bm2;
if (pictureBox1.Image==pictureBox2.Image)
{
MessageBox.Show("Some");
}
else
{
MessageBox.Show("Differ");
}
}
}