|
The code works fine if I create the components via VS.NET but I cannot get it to work when I create the controls dynamically. I am sure an item is selected because it gets highlighted.
Here is the way I am doing it
\\code for button to create components
ListBox lB1 = new ListBox();
lB1.Location = new Point(8,24);
lB1.Items.Add("A");
lB1.Items.Add("B");
lB1.Items.Add("C");
ListBox lB2 = new ListBox();
lB2.Location = new Point(8,24);
\\button
Button btn1 = new Button();
btn1.Location = new Point(152,30);
btn1.Text="->";
btn1.Size = new System.Drawing.Size(40,23);
btn1.Click += new System.EventHandler(btn1_Click);
\\when bnt1 clicked
private void btn1_Click(object sender, System.EventArgs e)
{
string curItem = listBox1.SelectedItem.ToString();
listBox2.Items.Add(curItem);
}
|
|
|
|
|
Hi!
What's so difficult about the exception?
The runtime tells you that you're trying to access a null reference, so you better believe it!
I guess it's thrown on the line string curItem = listBox1.SelectedItem.ToString(); simply because you're creating ListBox es named lB1 and lB2 but are accessing listBox1 and listBox2 in your event handler.
mav
|
|
|
|
|
What about trying ?
private void btn1_Click(object sender, System.EventArgs e)
{
lB2.Items.Add(lB1.SelectedItem);
}
Also you have naming error, in lb1 and listBox1, and the same in lb2 // In the btn1_Click code.
"I am a lair" Is this statement true or false ?
|
|
|
|
|
I made a mistake when I posted the code. All references are made to lB1 and lB2 (not listBox1, listBox2).
Just like I said, code works fine when components are created using the visual interface.
I also tried
private void btn1_Click(object sender, System.EventArgs e)
{ lB2.Items.Add(lB1.SelectedItem);
}
still an error.
*****************************************
The code works fine if I create the components via VS.NET but I cannot get it to work when I create the controls dynamically. I am sure an item is selected because it gets highlighted.
Here is the way I am doing it
\\code for button to create components
ListBox lB1 = new ListBox();
lB1.Location = new Point(8,24);
lB1.Items.Add("A");
lB1.Items.Add("B");
lB1.Items.Add("C");
ListBox lB2 = new ListBox();
lB2.Location = new Point(8,24);
\\button
Button btn1 = new Button();
btn1.Location = new Point(152,30);
btn1.Text="->";
btn1.Size = new System.Drawing.Size(40,23);
btn1.Click += new System.EventHandler(btn1_Click);
\\when bnt1 clicked
private void btn1_Click(object sender, System.EventArgs e)
{
string curItem = lB11.SelectedItem.ToString();
lB2.Items.Add(curItem);
}
|
|
|
|
|
try :
private void btn1_Click(object sender, System.EventArgs e)
{
string curItem = lB11.SelectedItem.ToString();
lB2.Items.Insert(curItem);
}
"I am a lair" Is this statement true or false ?
|
|
|
|
|
When I step thru the code. it fails on this line
"string curItem = listBox1.SelectedItem.ToString();"
but I am 100% sure that an item is selected before coninueing.
-I tried the isert, same deal.
How do I trigger an event by just clicking on one of the items, may be I can try this instead of selecting item then click a buton.
THis %$^*)#@#@ is kicking my rear.
thnanks for all good adviced.
|
|
|
|
|
picasso2 wrote: How do I trigger an event by just clicking on one of the items
See the SelectedIndexChanged event of the listbox.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
Hello,
I have created a form and want to extend/duplicate it for another use (in the same program). Some of the radio buttons and text boxes have been disabled so the user can't edit them. I would like to create a new form just like this one except this time I will enable these areas. How can I do this? I know how to change the Enabled functions but I don't know exactly how to recreate or extend the current form. Basically I want to have a button on the first form that when clicked, will hide the current form and open the new one with the areas enabled for editing.
Thanks.
|
|
|
|
|
Overload your Form's constructor and do the enabling of radio buttons there. Something like
class MyForm : Form
{
public Form()
{
...
}
public Form(bool enableControls) : this()
{
}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
S. Senthil Kumar wrote: public Form(bool enableControls) : this()
So... , I saw many constructors, some of them with :this() , and many wasn't with it.
So what is the function of it ?
"I am a lair" Is this statement true or false ?
|
|
|
|
|
It just means that also another constructor is called before this one:
public MyForm() {
}
public MyForm(bool foo) : this() {
}
Calling the second constructor will first do something and then something else . Leaving this() away from the second constructor would result in just doing something else .
|
|
|
|
|
Nice, thanks very much.
"I am a lair" Is this statement true or false ?
|
|
|
|
|
Hi,
I want to develop an application with menus. I want to make only one event handler method which handles a whole group of menu items by firstly identifying which MenuItem sends the click event and then performing the appropriate action. But I don't find any way to retrieve any info telling me which specific MenuItem sends a click event at the moment. The sender and e parameters seem to carry some very general information, not from a specific MenuItem sending the click event.
This info must exist somewhere, because Windows knows which menu shall be performed at the moment…, but C# seems to hide it for the programmer!
Is there a way to get that info?
Regards,
M Shafaat
|
|
|
|
|
M Shafaat wrote: But I don't find any way to retrieve any info telling me which specific MenuItem sends a click event at the moment. The sender and e parameters seem to carry some very general information, not from a specific MenuItem sending the click event.
No prob. You just have to cast the sender parameter to a MenuItem to get the info you want.
this.mnuFileSave.Click += new System.EventHandler(this.MenuHandler);
this.mnuFileSaveAs.Click += new System.EventHandler(this.MenuHandler);
.
.
.
private void MenuHandler(object sender, EventArgs e) {
MenuItem selectedMenu = (MenuItem)sender;
switch (selectedMenu.Name) {
case "mnuFileSave" :
MessageBox.Show(this, "Save selected");
break;
case "mnuFileSaveAs" :
MessageBox.Show(this, "SaveAs selected");
break;
}
}
Share and enjoy.
Sean
|
|
|
|
|
This is principally right but I would do it this way:
if (sender == mnuFileSave)
MessageBox.Show(this, "Save selected");
else if (sender == mnuFileSaveAs)
MessageBox.Show(this, "SaveAs selected");
else
MessageBox.Show(this, "'Unknown' selected");
This way removing/renaming MenuItems will immediatly result in an compilation error so that you have the chance changing it accordingly.
|
|
|
|
|
Thnaks a lot
Regards
M Shafaat
|
|
|
|
|
I'm using mciSendString to play an .avi file.
The problem is if i play the file in fullscreen when i click on the screen it came back in windowed mode but it stop the movie.
Why?
How can i avoid to make came back in windowe mode after click when it is fullscreen?
Or how can i handle a function when it happens so to continue to play the movie?
I hope to be explained good, in contrary case please ask me.
Regards.
|
|
|
|
|
Hi all,
I've encountered a strange behaviour of one of my objects. After it updates itself in a method(updates some private fields) the changes happen when the method is being executed but when execution returns to the line where the method was called, the object is the same as before. Let me show you the code which does this.
<pre>
public enum DConfMode
{
Normal,
NoInit
}
public class DConf
{
private String hostname;
private String database;
private String username;
private String password;
public DConf(DConfMode Mode)
{
switch (Mode)
{
case DConfMode.Normal:
this.Init();
break;
case DConfMode.NoInit:
break;
}
}
public void Init()
{
XmlTextReader reader = new XmlTextReader(DGlobals.ConfFile);
while (reader.Read())
{
switch (reader.Name)
{
case "hostname":
this.hostname = reader.Value;
break;
case "database":
this.database = reader.Value;
break;
case "user":
this.username = reader.Value;
break;
case "password":
this.password = reader.Value;
break;
}
}
reader.Close();
}
public String DbHostname
{
get
{
return this.hostname;
}
}
public String DbDatabase
{
get
{
return this.database;
}
}
public String DbUsername
{
get
{
return this.username;
}
}
public String DbPassword
{
get
{
return this.password;
}
}
}
</pre>
<pre>
DConf conf = new DConf(DConfMode.NoInit);
conf.Init();
Console.WriteLine(conf.DbHostname);
</pre>
So DConf does the problem. In the second code excerpt, echoing conf.DbHostname doesn't do anything, but when I've debugged it, in conf.Init(); the values are clearly populated. So I'm quite confused why this happens. Here's some sample xml which you could use:
<code>
<configuration>
<user>username</user>
<password>samplepass</password>
<hostname>localhost</hostname>
<database>dbname</database>
</configuration>
</code>
As a sidenote, is it safe calling Init(); in DConf's constructor? I suppose no, since it changes fields and the object hasn't been guaranteed to have been fully set up, am I correct? Any help is greatly appreciated.
Regards,
gamehack
-- modified at 19:29 Saturday 29th October, 2005
|
|
|
|
|
I've found my mistake. It's in my xml handling code. Thanks for reading.
Regards
|
|
|
|
|
Hi!
I have a binary file that contains strings one after the other and I want to replace a string at offset x. The existing string is, let's say 10 bytes long, but the new one is 12 bytes long. How do I replace the old string at position x without overwriting the first 2 bytes of the next string and without rewriting the entire binary file from offset x.
Thanks already in advance!
|
|
|
|
|
You don't.
---
b { font-weight: normal; }
|
|
|
|
|
You can't. You MUST rewrite the entire file. Read the the part up to the point where you are replacing the string, write it to a new file. Append the new string, then read the file from the point after the string your replacing and append that to the new file.
There is NO other way to do this.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I see. Thanks for your help guys!
|
|
|
|
|
Hi All,
I'm being good and moving all my hardcoded strings into my applications resources, but I've run into difficulties.
I'm using "\r\n" in my hardcoded strings to represent a newline, and this is fine as the compiler converts this to 0x0a, 0x0d for me. If I embed this in a string resource, the "\r\n" is kept as a 4 character literal - not the two control character I'm after. So, how do I represent a newline in a resource string?
Anyone know a simple solution to this?
Thanks
Rob Philpott.
|
|
|
|
|
First, it's recommended you use Environment.NewLine instead of \r\n.
To answer your question, you should be able to paste in your string in the Visual Studio app resources string table. To add a new line, just hit CTRL+Enter when editing the string resource, and it will add the new line for you.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Morality Apart from God
Judah Himango
|
|
|
|