I am in need of assistance, I've been looking all day in Google and so far I have not found an article for what I'm trying to do. I am working on a little project in C# using SharpDevelop as my IDE, in the user interface for my project I have several Labels, 11 of which indicate what the field is about (i.e. "Name:" , "E-mail:") and the other 11 which will auto fill with information from a DB after pressing the Search button and typing in a keyword.
What I need to do is copy all of the labels onto the Clipboard so that the copied information may be used in another program - I have this very same application in Excel and it does what I want but I need a little more versatility thus I decided to give it a go on C#.
Is there any way to accomplish this in C#? I have come accross ListView and DataGrids and I've thought about copying the labels to ListView (as an alternative and if possible) so that I may copy the information from ListView but with format for example:
Name: Tim Turner
Place all of the output controls in a panel (or identify them however you think is best) and then you can use the following code:
StringBuilder clipboard = new StringBuilder();
foreach (Label label in outputPanel.Controls.OfType<Label>())
clipboard.Append(label.Text + "\n");
Clipboard.SetText(clipboard.ToString());
Update
It was my understanding that you only wanted to copy the values of a series of check boxes and that would suffice. All you need to do if you would rather not iterate over a collection of controls but append values to the clipboard manually all you need to do is well, exactly that.
private void SetClipboard()
{
StringBuilder clipboard = new StringBuilder();
clipboard.Append(label1.Text + "\n");
clipboard.Append(label2.Text + "\n");
clipboard.Append(textBox1.Text);
Clipboard.SetText(clipboard.ToString());
}
IF you are using Windows Forms you can use ContextMenuStrip , by naming an option "copy", then applying named ContextMenuStrip to each label option that says ContextMenuStrip
private void copyUserInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
string UserInfo = $"{lblFirstName.Text}\n" +
$"{lblLastName.Text}\n" +
$"{lblEmailAddress.Text}\n" +
$"{lblPhysicalAddress.Text}\n" +
$"{lblCountry.Text}\n" +
$"{lblCompany.Text}\n" +
$"{lblStatus.Text}\n" +
$"{lblFirstContact.Text}\n" +
$"{lblLastContact.Text}\n" +
$"{lblNotes.Text}\n ";
Clipboard.SetText(UserInfo);
}
IF you want to select FOR a single label use a second option on the ContextMenuStrip and use the following:
Clipboard.SetText(labelContextMenuStrip.SourceControl.Text);
See the following:
https://stackoverflow.com/a/53263702/7444103
C# How to copy text from different labels using only one context menu when right click
That was with help from #CoolBots and #Jimi.
Related
I am currently developing a program in C# that contains a combo box and a button to add a new item to the combo box when said button is pressed. I need it so that when the button is pressed and the item is added, it gradually builds up a list of items that have a number that increases by one for each item.
For example when the button has been pressed twice, the combo box will contain the following two items:
New Profile 1
New Profile 2
Etc.
I am making it so that the items are set to "New Profile" with the number by default so that the end-user isn't confused as to which profile is which but the user can change the items name later if they wish to, but I am struggling making this concept work in code.
I had the generic code to add a item to my combo box:
private void AddProfileButton_Click(object sender, EventArgs e)
{
ProfileList.Items.Add("New Profile");
}
So, I tried drafting some code to see if I could accomplish my ideas on my own. This is my code:
int a = 0;
var b = a + 1;
var NewProfileName = "New Profile " + b;
ProfileList.Items.Add(NewProfileName);
However; when I tried this when the form was running, I kept getting items with the same name of "New Profile 1" repeatedly. So it sort of works - just that it doesn't increase the integer how I want it to.
I think part of the problem is the:
int a = 0;
part of the code however my attempt(s) to fix this (pasting this line of code under the Form_Load event) have been a failure/inconclusive.
I would greatly appreciate someone's help and all suggestions are welcome.
Josh
Thanks to #LarsTech for the answer.
The code is:
ProfileList.Items.Add(string.Format("New Profile {0}", ProfileList.Items.Count + 1));
I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
I want to create filter insert text in my simple image editor program
based on this nice article. I found out how to do it, but I need some configure.
I make insert text more dynamic, just like Photoshop or Notepad. User click add text icon, cursor change and when user click on canvas that will be position for text. I already found this solution by mouse down event to get x & y screen coordinate.
I wanna make after (1), program will create something like textbox (minus background & border) so user can type text on it where in tutorial text is static.
I don't have idea how to do this, can anyone give me suggestion or example maybe?
Update
i really don't have any idea how to make this question more clear, spesific, or etc
i already try to divided what i want to 2 problem and solved one of them
i know if there is not just single method to archive what i want...but i don't know if there will be triple many (many many many) way to do it, so i must write spesific method i wanna use...
how can i know which one i will use if i even don't have any idea one of them....
in my mind just come dumb way create custom textbox with no background & border
Code
//set flagText active or not
private void InsertText(){
if (flagText){
flagText = false;
imageBoxCamera.Cursor = Cursors.Default;
}
else {
flagText = true;
imageBoxCamera.Cursor = Cursors.IBeam;
}
}
//in mouse down event i wanna create something like textbox (minus label & border) with coordinat x & y from mouse down
private void imageBoxCamera_MouseDown(object sender, MouseEventArgs e)
if (flagText) {
MessageBox.Show(Cursor.Position.X.ToString() + " " + Cursor.Position.Y.ToString());
}
}
Take a look on OpenPDN source code(fork for Paint.NET).
As I know there is a similar function there
https://code.google.com/p/openpdn/source/browse/#hg%2Fsrc
I am working on a Windows application where I am showing logs using a treeview as shown below, here nodes are created dynamically on the basis of daily logs
Logs -
+ 12-02-2001
+ 12-02-2001
+ 12-02-2001
+ 12-02-2001
but I want to add delete button with each node as shown below
Logs -
+ 12-02-2001 Delete
+ 12-02-2001 Delete
+ 12-02-2001 Delete
+ 12-02-2001 Delete
Thanks.
You can make it easier using ContextMenuStrip.
http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx
//event handler for menuItem Click
private void mnuDelNode_Click(object sender,EventArgs e)
{
//better confirm before delete using a message box
DeleteRecursive(listView.SelectedNode);
}
private void DeleteRecursive(TreeNode root)
{
//your delete logic here
}
If you are using WindowsForms, you would need to implement custom drawing of the TreeView and do hit testing on the Click event to see if the button was clicked. The TreeView was not really designed to add in buttons, so you may wish to consider an alternative design, say adding in a right menu, a toolbar, and/or right click menu with the Delete command on it, as that would be significantly less work and more in line with how the standard Windows controls work (you don't see a bunch of buttons behind folder names in Windows Explorer's TreeView for instance).
I'm trying to change the z-order for some docked panel in the VS2010 designer. However, everytime I save the designer, close it down and re-open it, the z-order has reverted back to how it was before I changed it.
I've tried using the document outline, and the SendToBack context menu but both behave in the same way. I've also noticed that in another solution where it does work, the .designer.cs file doesn't actually change (I assumed control adding order would dictate z-order).
Is there any other way I can do this? I really don't want to do this at runtime...
EDIT
this.mainPanel.BackColor = System.Drawing.SystemColors.Control;
this.mainPanel.Controls.Add(this.pnlRangeSelector);
this.mainPanel.Controls.Add(this.headerAndFooterRowCounts);
pnlRangeSelector has Dock.Top
headerAndFooterRowCounts has Dock.Right
when this renders however, headerAndFooterRowCounts takes the entire right side of it's parent panel, while pnlRangeSelector takes the Left portion.
That suggests to me that the pnlRangeSelector isn't correctly at the back like it's supposed to be.
UPDATE
int i = 0;
String output = String.Empty;
foreach (var c in this.mainPanel.Controls)
{
if (c == pnlRangeSelector) { output += "RangeSelector at : " + i.ToString() + "\r\n"; }
else if (c == this.headerAndFooterRowCounts) { output += "HeaderAndFooter at : " + i.ToString() + "\r\n"; }
i++;
}
MessageBox.Show(output);
Seems the order is always RangeSelector = 0, HeaderAndFooter = 1. Even if I call the following just prior to this:
this.mainPanel.Controls.SetChildIndex(this.pnlRangeSelector, 1);
this.mainPanel.Controls.SetChildIndex(this.headerAndFooterRowCounts, 0);
The order in which the controls are added to the Controls collection of the parent determines the z-order. So the best way to solve this (AFAIK) is to make a backup of the designer file and then edit the order in which the controls are ADDED to the collection
Post the code if you need help.
I had a similar problem and found the problem was in the Designer code. In my base form, caseHeader was docked to the top. Then, I added formInstructions and docked it to the top. Therefore, my base had formInstructions below the caseHeader. This is the desired appearance.
In derived forms, the appearance was reversed. I researched about docking, autosize, z-orders, etc. - all to no avail.
Finally, I found Designer was changing the child indices of various controls. This is what I did in the Designer code to fix the problem:
//this.Controls.SetChildIndex(this.caseHeader, 0);