|
Abelinda81 wrote: What I am doing wrong here?
For a start, do you really want to select everything in the list box also? (You're thinking about how a user might do this, not how to do it programmatically)
Also, what do you actually want to appear in the clipboard? The list box has no idea about the clipboard so you have to format the data for it.
What you might want to do is create a StringBuilder and build up a list of the items with an Environment.NewLine at the end of each item, rather than selecting all the items in the list box.
|
|
|
|
|
Yes, I want to select everything in the listbox as I gathered all the info I need from other listboxes in this final one(lstAll). and now I want to select all items in listbox and copy to clipboard:
private void btnCopy_Click(object sender, EventArgs e)<br />
{<br />
StringBuilder strAll = new StringBuilder();<br />
<br />
for (int j = 0; j < lstAll.Items.Count; j++)<br />
{ <br />
lstAll.SetSelected(j, true);
But I don't now what should I put here before:
Clipboard.SetDataObject(strAll.ToString(), true);
Thank you in advance for your answer(s)!
|
|
|
|
|
You haven't answered the question: Do you really want to select everything in the list box. You are selecting the contents of the list box with apparently no purpose.
foreach(object item in lstAll)
{
strAll.Append(item.ToString());
strAll.Append(Environment.NewLine);
}
|
|
|
|
|
Hi people...
I'd like to do this:
myButton.Click = null;
But with events I can only use += and -= ... The problem is that I don't know which handlers are associated with this button...
Any Idea?
|
|
|
|
|
Which ever classes do the += should also being doing the -=.
All you need to do is use the exact same line as the += but with -=
|
|
|
|
|
hello,
I wrote the following program. it should be able to make changes in active directory. but it doesn't do it.
can you look at my code and say to me what does not go with?
THANK YOU
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
namespace fichier
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private Button btnWriteToLDAP;
private DataTable m_dt;
//private System.Windows.Forms.TextBox textBox1;
//
// required designer variable.
private System.ComponentModel.Container components = null;
public Form1()
{
// required for windows Form Designer support
InitializeComponent();
// TODO Add any constructor code after InitializeComponent call
}
// clean up any resources being used
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
// required method for designer support - do not modify
// the contents of this method with the code editor.
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnWriteToLDAP = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(400, 400);
this.dataGrid1.TabIndex = 0;
//
// btnWriteToLDAP
//
this.btnWriteToLDAP.Location = new System.Drawing.Point(415, 40);
this.btnWriteToLDAP.Name = "btnWriteToLDAP";
this.btnWriteToLDAP.Size = new System.Drawing.Size(75, 23);
this.btnWriteToLDAP.TabIndex = 1;
this.btnWriteToLDAP.Text = "WriteToLDAP";
this.btnWriteToLDAP.UseVisualStyleBackColor = true;
this.btnWriteToLDAP.Click += new System.EventHandler(this.btnWriteToLDAP_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 425);
this.Controls.Add(this.btnWriteToLDAP);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
// the main entry point for the application
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// on crée une table
m_dt = new DataTable("test");
// on crée des colonnes
m_dt.Columns.Add("TelephoneNumber", System.Type.GetType("System.Int32"));
m_dt.Columns.Add("User", System.Type.GetType("System.String"));
StreamReader fichier = File.OpenText(@"h:\\export.txt");
while (fichier.Peek() >= 0)
{
// on lit une ligne et on ajoute
string ligne = fichier.ReadLine();
string[] vals = ligne.Split(';');
DataRow dr = m_dt.NewRow();
try
{
dr["TelephoneNumber"] = int.Parse(vals[0]);
dr["User"] = vals[1];
m_dt.Rows.Add(dr);
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString());
Console.ReadLine();
}
}
// on genere le tableau
dataGrid1.DataSource = m_dt;
}
private void btnWriteToLDAP_Click(object sender, EventArgs e)
{
{
DirectoryEntry deUser = new DirectoryEntry("LDAP://ou=User, ou=User Office, ou=User, DC=hte,DC=intra", "don", "mamanetpapa");
DirectorySearcher searchEmploye = new DirectorySearcher(deUser);
searchEmploye.Filter = "(objectClass=user)";
// Display all Employe
foreach (SearchResult unResultat in searchEmploye.FindAll())
{
DirectoryEntry unEmploye = unResultat.GetDirectoryEntry();
}
// Modification of the telephoneNumber
foreach (SearchResult unResultat in searchEmploye.FindAll())
{
DirectoryEntry unEmploye = unResultat.GetDirectoryEntry();
foreach (DataRow row in m_dt.Rows)
{
/*if (row[0].ToString().Contains(" "))
{
string[] str = row[0].ToString().Split(" ");
}*/
MessageBox.Show(unEmploye.Properties["SAMAccountName"].Value.ToString());
if (unEmploye.Properties["SAMAccountName"].Value == row[1])
{
string temp = string.Format("{0} {1}", row[1], row[0]);
MessageBox.Show(temp);
unEmploye.Properties["telephoneNumber"].Value = row[0].ToString();
unEmploye.CommitChanges();
}
}
}
}
}
}
}
Dorine
|
|
|
|
|
dorine82 wrote: it should be able to make changes in active directory. but it doesn't do it.
Boy, that's specific. Handing someone your code and saying "My program doesn't work!" isn't going to get you any useful responses.
Care to explain what you expect this code to do and what it's doing/not doing? Any error messages??
|
|
|
|
|
int Num = (((1024 / 8) / 3) * 4) + 4;
Returns 172 when it should return 175. Google calculator[^]
Please if any one could help me out.
VB Code returns 175 which is correct as followed:
Dim Num As Integer = (((1024 / 8) / 3) * 4) + 4
|
|
|
|
|
Nothing is wrong with it cuz it's dropping the fractions, if you want to keep the fractions, you should treat the figures as floats not integers.. Try to recalculate it and each time you have a fraction drop it and see the result.
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
Close ... the fractions arn't dropped, they are rounded.
|
|
|
|
|
if you rounded 42.66 to 43 the result is 176!
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
see my reply to your other post ... theres more than one type of rounding!
|
|
|
|
|
After further investigation ... your right!
I was wrong ... it uses truncation and not bankers rounding ... just goes to show it's always good to do a little follow up investigation on what info you find
|
|
|
|
|
C# uses bankers rounding where decimals are rounded to the nearest even number.
1024 / 8 = 128
128 / 3 = 42.66 recurring. Since you are dealing with int's here this is rounded, and rounded to the nearest even number i.e. 42
42 * 4 = 168
128 + 4 = 172
|
|
|
|
|
Thanks to all! this was very helpful!
|
|
|
|
|
Don't forget the 5's all round then
|
|
|
|
|
originSH wrote: 128 / 3 = 42.66 recurring. Since you are dealing with int's here this is rounded, and rounded to the nearest even number i.e. 42
42.49 = 42 (-)
42.50 = 43 (+)
This's rounding, that was dropping!
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
No it uses bankers rounding ... which is to the nearest even number.
i.e.
5.6 will round to 6
6.6 will round to 6
|
|
|
|
|
After further investigation ... your right!
I was wrong ... it uses truncation and not bankers rounding ... just goes to show it's always good to do a little follow up investigation on what info you find
|
|
|
|
|
Really!!!!
originSH wrote: bankers rounding...
6.6...
.. Thanks a million origin!
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
never.reply wrote: Returns 172 when it should return 175.
No, it shouldn't. You use integer division, so 128 / 3 = 42.
never.reply wrote: VB Code returns 175 which is correct as followed:
Dim Num As Integer = (((1024 / 8) / 3) * 4) + 4
That's because the / operator in VB is always a floating point operator. It converts both operands to double before doing the division.
The equivalent of x / y in VB is (double)x / (double)y in C#.
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi, sorry for the basic question but I am new to C# and finding some of it confusing. Basically I want to fill lots of combo boxes with items when the form is loaded with the information coming from a mysql database. My question is what's the best method to do this so it does not freeze the program whilst it is retrieving the information and then adding them to the combo boxes. I have looked at background workers but that leads to cross threading.
Thank you for your time...
Karlos
|
|
|
|
|
if u r using datasets to bind the combo boxes then u should try to fill datasets on constructor of form the bind it to combo boxes on form load.
rahul
|
|
|
|
|
I havent been using datasets but i shall look at them now. I have a MDI so the user may have multiple forms open, whilst they are loading a form they may want to work on another. Will using datasets in the mentioned way although this?
Thanks
|
|
|
|
|
If you don't want to freeze the UI then you'll need another thread. If you do lots of work o nthe UI thread then it can't do anything else ... like update the UI
Thats why the BGW is handy ... it encapsulates alot of the threading stuff for you and makes things a bit easier.
As long as you keep things simple the only extra work needed for the threading is making sure you use control.InvokeRequired and control.Invoke.
Heres a quick example:
When ever you want to set a property or call a method you need to check InvokeRequired. If InvokeRequired is true then you need to use the Invoke method.
If if your original code is this:
<br />
comboBox1.Items.Add(item)<br />
your new code will be this:
<br />
if (comboBox1.InvokeRequired)<br />
comboBox1.Invoke(comboBox1.Items.Add(item));<br />
else<br />
comboBox1.Items.Add(item);<br />
THis will protect your code for use with threading.
Whilst this code looks pretty simple it's actually using some of the new features added in .net 2.0 to simplify it. It's using something called "Anonymous Methods".
What actually going on here is that delegates are being used.
An easy way to imagine delegates are as definitions of the method you want to call i.e. they define the parameters and return value of the method.
This allows you to pass a method to be called i.e. I want you to call MyMethod(string someParam) when your done.
Now rather than having to make the method and the delegate for Invoke to call we use "Anonymous Methods" to say, well you know we are going to have a delegate and a method here and it's going to be simple, so how about we just give you the method body, in this case 'comboBox1.Items.Add(item)', and you can work out the rest.
Hopefully I haben't used any concepts you haven't yet learnt about, and I haven't just confused you even more :P
Feel free to reply with any more questions and I'll do my best to explain and unconfuse.
|
|
|
|