6 Replies - 674 Views - Last Post: 06 October 2015 - 04:17 PM Rate Topic: -----

#1 achieving   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 15

If Statement with Combo Box Mishaps

Posted 05 October 2015 - 07:14 PM

So long story short Im trying to get a combo box to verify that one of the emails addresses that I have supplied is there and when it does I want it to take the name and domain from the string and output it to the message box. So I have hit a brick wall: I can't even get the combo box to verfiy that I have selected a email so when I click Parse it does nothing. I have tried everything from ComboBox.SelectedText to SelectedItem but it never actually goes through. Help lol
Here's the code(Its probably super ugly I am relatively new to C#) Thanks.
private void btnParse_Click(object sender, EventArgs e)
        {
            InitializeComponent();
            MailAddress addr = new MailAddress("[email protected]");
            MailAddress addr1 = new MailAddress("[email protected]");
            MailAddress addr2 = new MailAddress("[email protected]");
            MailAddress addr3 = new MailAddress("[email protected]");

            string username = addr.User;
            string host = addr.Host;
            string username1 = addr1.User;
            string host1 = addr1.Host;
            string username2 = addr2.User;
            string host2 = addr2.Host;
            string username3 = addr3.User;
            string host3 = addr3.Host;

            cmbEmail.Items.Add(addr);
            cmbEmail.Items.Add(addr1);
            cmbEmail.Items.Add(addr2);
            cmbEmail.Items.Add(addr3);

            if (cmbEmail.Items.Equals(addr))
            {
                MessageBox.Show("Name:");
            }  



Is This A Good Question/Topic? 0
  • +

Replies To: If Statement with Combo Box Mishaps

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7105
  • View blog
  • Posts: 24,124
  • Joined: 05-May 12

Re: If Statement with Combo Box Mishaps

Posted 05 October 2015 - 07:43 PM

Why would you expect the reference to the items collection of a combobox to be equal to a reference to a an email address?
Was This Post Helpful? 2
  • +
  • -

#3 achieving   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 15

Re: If Statement with Combo Box Mishaps

Posted 05 October 2015 - 08:14 PM

No idea hence why I'm asking. All I need it to do it when I select the email address out of the combo box it displays the name and domain. It's just getting to that point that has me stumped.

This post has been edited by Skydiver: 05 October 2015 - 08:26 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours. Use the Reply button or the Fast Reply area.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7105
  • View blog
  • Posts: 24,124
  • Joined: 05-May 12

Re: If Statement with Combo Box Mishaps

Posted 05 October 2015 - 08:40 PM

There's a couple side issues not directly related that you need to deal with first...
Issue 1: Why are you using WinForms? (And using it poorly at that.)
You really should be using WPF at this point in time. WinForms is headed towards its end-of-life. Taking time to learn it now is a just a waste of your time unless you are planning to work in a niche role of maintaining old WinForms software.

Issue 2: Why are you calling InitializeComponent() in your button click handler?
InitializeComponent() should only be called when a form or custom user control is first being initialized -- normally in the constructor.

Issue 3: Why are you populating the combobox when the user clicks the Parse button? How would the user have had a chance to select an item out of the combobox before the Parse button was pressed if the combobox is empty?

Now back to the combobox issue. Assuming that you populate it before the Parse button is clicked, then all you need to do is set the style of the combobox to be a DropDownList to force the user to pick one of the items. After that, it's just a matter of getting the SelectedItem property to get back the chosen MailAddress. No need to do any if's to check if the user selected one of the choices.
Was This Post Helpful? 1
  • +
  • -

#5 achieving   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 15

Re: If Statement with Combo Box Mishaps

Posted 06 October 2015 - 08:13 AM

All that code up until the if statement was in the form load I have just been moving things around to try to get it to work.
Was This Post Helpful? 0
  • +
  • -

#6 achieving   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 15

Re: If Statement with Combo Box Mishaps

Posted 06 October 2015 - 12:44 PM

I figured it out thanks for you help.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: If Statement with Combo Box Mishaps

Posted 06 October 2015 - 04:17 PM

View Postachieving, on 06 October 2015 - 09:13 AM, said:

All that code up until the if statement was in the form load I have just been moving things around to try to get it to work.


Please forgive the adult, peer-to-peer, talking to you like a fellow grown-up comment I'm about to make. (or don't)

What you just said was:

Quote

I haven't a f*cking clue what I'm doing and I'm just banging on the keyboard in the dark hoping something magically makes it work.


So stop. Just stop. Talk to your teacher. Tell them you are lost. If you don't do it now you'll just get more lost. This is not history class where chapter 1 is on Rome and chapter 2 is on the Nazis, giving you a chance to start fresh. Programming builds on the previous concepts. Chapter 2 builds on chapter 1. Chapter 19 builds on the 18 chapters before it. If you don't man-up and tell your teacher you need some help getting caught up you never will, and you will be throwing away thousands of dollars in tuition.


My standard beginner resources post - Updated June 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs, commonly text adventures or DnD or some other game. Games are truly the worst thing to start with.
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.


Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?

WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.




I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1