Building a GUI in C#

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 15812 Views - Last Post: 21 January 2011 - 07:56 AM Rate Topic: -----

#1 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Building a GUI in C#

Posted 17 January 2011 - 02:31 PM

Hi

I want to implement a windows form application that can choose randomly among the different names in the List. The problem is that I don't know where to start. I have tried using the C# documentation which I find messy compared to the Java API, and I'm just a bit lost.
So anyone who can give any clues to what I can use?

If I was to write it in java I probably would use a JFrame with JTextField, JButton and set some specific layout etc.
What would be the corresponding things in C#?

Thanks in advance

This post has been edited by macosxnerd101: 17 January 2011 - 02:39 PM
Reason for edit:: Title renamed to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Building a GUI in C#

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8964
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: Building a GUI in C#

Posted 17 January 2011 - 02:41 PM

Quote

If I was to write it in java I probably would use a JFrame with JTextField, JButton and set some specific layout etc.
What would be the corresponding things in C#?


Create a 'windows app'.
In the designer open up your toolbox.
Click on a tool and either drag it on to your app or click on your app to place an instances.

Fill to hearts content.

This post has been edited by modi123_1: 17 January 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Building a GUI in C#

Posted 17 January 2011 - 02:42 PM

Here's a great little tutorial in the MSDN library that should teach you all you need to know about using Controls for Windows Forms applications:

http://msdn.microsof...y/360kwx3z.aspx
Was This Post Helpful? 3
  • +
  • -

#4 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Building a GUI in C#

Posted 17 January 2011 - 02:54 PM

Hi again

I know how to create a windows form application, the doubt is on the code. For example if I was using JTextField, I can just go to the java API and see the options for the JTextField. For example I can see that it has the method setText() which is very used with JTextField.

Lets say that I have created the windows form, and need to add code to it. The question is, how do I find out what methods that follow with the TextBox forexample? That's where I think C# doc is a little bad. It doesn't have the same easy system as java doc.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8964
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: Building a GUI in C#

Posted 17 January 2011 - 02:58 PM

You can check either the properties (on the designer), or intellisense when you type the variables name in the code. Or highlight your variable and hit 'f1' to go to the documentation of that type.
Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Building a GUI in C#

Posted 17 January 2011 - 02:58 PM

MSDN library is the place for anything you could ever need to know about C#. For instance, the following link shows all the methods that apply to the TextBox control: http://msdn.microsof...ox_methods.aspx
Was This Post Helpful? 4
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Building a GUI in C#

Posted 17 January 2011 - 03:00 PM

Make use of IntelliSense! It's a great tool, and will show you all the options available to you.

Plus, here is the link to the documentation on the TextBox class:

http://msdn.microsof...ls.textbox.aspx

It lists all the members available to you...What don't you like about it?

EDIT: Got beaten to it :)

EDIT: On both counts ;)

This post has been edited by CodingSup3rnatur@l-360: 17 January 2011 - 03:39 PM

Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Building a GUI in C#

Posted 17 January 2011 - 03:16 PM

View PostMorRomio, on 17 January 2011 - 02:31 PM, said:

Hi

I want to implement a windows form application that can choose randomly among the different names in the List. The problem is that I don't know where to start. I have tried using the C# documentation which I find messy compared to the Java API, and I'm just a bit lost.
So anyone who can give any clues to what I can use?

If I was to write it in java I probably would use a JFrame with JTextField, JButton and set some specific layout etc.
What would be the corresponding things in C#?

Thanks in advance


I hate to sound rude, but what you're really asking is "How do I build my first C# application?"

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

  • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
  • Arrays: Shows how to use arrays.
  • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
  • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
  • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
  • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
  • Commandline: Demonstrates simple command-line processing and array indexing.
  • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
  • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
  • Events: Shows how to declare, invoke, and configure events in C#.
  • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
  • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
  • Hello World: A Hello World application.
  • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
  • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
  • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
  • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
  • Nullable: Demonstrates value types, such as double and bool, that can be set to null
  • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
  • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
  • Operator Overloading: Shows how user-defined classes can overload operators
  • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
  • PInvoke: Shows how to call exported DLL functions from C#
  • Properties: Shows how properties are declared and used; also demonstrates abstract properties
  • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
  • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
  • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
  • Structs: Shows how to use structs in C#.
  • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
  • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
  • User Conversions: Shows how to define conversions to and from user-defined types
  • Versioning: Demonstrates versioning in C# by using the override and new keywords
  • XML Documents: Shows how to document code by using XML
  • Yield: Demonstrates how to use the yield keyword to filter items in a collection

Was This Post Helpful? 1
  • +
  • -

#9 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Building a GUI in C#

Posted 17 January 2011 - 03:24 PM

Thanks both of you for the time and effort. I'll take a look at the links and suggestions you've made :)
Was This Post Helpful? 0
  • +
  • -

#10 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • Posts: 548
  • Joined: 07-July 10

Re: Building a GUI in C#

Posted 17 January 2011 - 04:03 PM

As a convert from Java, I agree that the MSDN docs are not as good as Java docs. Java docs list ever single method for an object. It includes methods for all base classes. Java docs are more Wikipedia, MSDN is more Yahoo Directories. MSDN is great, if you know what you are looking for. You can waste a lot of time just clicking around on Javadocs and finding stuff you didn't know you wanted. MSDN has all of the information, it just doesn't seem to be at your fingertips.

I second the IntelliSense. Once you get used to it, it will become your friend.
Was This Post Helpful? 0
  • +
  • -

#11 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Building a GUI in C#

Posted 18 January 2011 - 11:36 AM

Hi

I found out how to do it thanks to you guys.

Now I have a question regarding code
I have this code:

 
   private void button1_Click(object sender, EventArgs e)
     {
        listBox1.Items.Add(comboBox2.Text + " " + textBox1.Text + " " + textBox2.Text);
        listBox1.Items.Add("Age: " + comboBox4.Text);
        listBox1.Items.Add("My country is in: " + comboBox3.Text);
            
     }



So I have this listBox1 that I add items to, given by a person once the button is pressed.
But now I want to do so that, if the button is pressed, and the user have not given any info in the combo box or text box, he should get a message telling him to fill the empty spaces. I can't find a method that allows me to do that using intellisens (nor google example).
So shortly, my idea is:

         private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.isEmpty()) //Note that there is no a message called isEmpty, it's just example
            {
                listBox1.Items.Add("You must fill in the empty spaces");
            }
            else
            {
                listBox1.Items.Add(comboBox2.Text + " " + textBox1.Text + " " + textBox2.Text);
                listBox1.Items.Add("Age: " + comboBox4.Text);
                listBox1.Items.Add("My country is in: " + comboBox3.Text);
            }
        }


Was This Post Helpful? 0
  • +
  • -

#12 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • Posts: 548
  • Joined: 07-July 10

Re: Building a GUI in C#

Posted 18 January 2011 - 03:57 PM

The combobox and the textbox both have a property called Text. This is a simple string. Check to see if the string is not what you want. string.IsEmptyOrNull(combobox1.text);

If you are checking to see if the listbox is empty, that's a different technique. ListBox.Items is a collection (List) of items. Check the Count property to see if it's empty.listBox1.Items.Count == 0
Was This Post Helpful? 1
  • +
  • -

#13 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Building a GUI in C#

Posted 19 January 2011 - 01:15 PM

Thanks man, I made it work.

Do you know why the Count property isn't in the libray under listbox?

Not that this question is so important, so if you don't feel like answering it, it's okay, but I'm just wondering.
Also, if anyone does answer, maybe you can explain what the properties are compared to methods. I have not seen anything similar in java, so I'm wondering what the advantage of the properties is?

Fx, you write:
combobox.Items.Add("Something"); Why is this better than just making it:
combobox.Add("Something"); This seems easier and smarter imo. So what's the difference?
Why is the Item property there? In fact, why are all the properties there?
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Building a GUI in C#

Posted 19 January 2011 - 01:28 PM

Quote

Do you know why the Count property isn't in the libray under listbox?


Because there's nothing to count on the ListBox itself. You can count the items it's holding, which would be listBoxName.Items.Count (or Length, I can't remember which off the top of my head).

You use properties in C# for the same reason you use "getter" and "setter" methods in Java.

Properties add flexibility over methods, though. You can treat a property as if it were a field, but when you get or set it, a hidden "accessor method" is called. This basically combines the concept of "getter" and "setter" methods, all into a value that can be used as if it were a simple.

A simple example:

private string someString;
public string SomeString{
  get{
    if(someString == null)
      return string.Empty;
    else return someString;
  }
  set{
    if(value == null)
      someString = string.Empty;
    else someString = value;
  }
}


This way, I can treat the property SomeString as if it were a field like any other. I can do this:

SomeString = "test";
string s = SomeString.Trim();
SomeString = null;


But on the last call, SomeString's value will be set to "", rather than null.


Quote

combobox.Items.Add("Something"); Why is this better than just making it:
combobox.Add("Something"); This seems easier and smarter imo. So what's the difference?


The reason is that the ComboBox doesn't hold things itself. It holds a collection that holds things. It would have been possible for them to add a method on to ComboBox to do that, but there's no point when the method is already there on its property.

And the reason that Items is exposed is so I can deal with all the items in the ComboBox if I want. If all we had was .Add, then how would I know what was in there in code later?
Was This Post Helpful? 1
  • +
  • -

#15 MorRomio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-September 10

Re: Building a GUI in C#

Posted 20 January 2011 - 08:45 AM

Hi once more guys

I'm working with some Lists and wanting to return some specific value. I have used some tutorials on the net, but I have not found some that cover my problem

So I made the list List<String> with some names, and I want to run through that list and randomly return a name from that list.
I have tried different solutions, but no good. The solutions I came to would work, but is really bad since I have many string variables (names)

I could say:
if (Random().NextDouble() < 0.1) {return name1}
if (Random().NextDouble() < 0.2) {return name2} and so on.

But like I said, I have many names in the List. How would you handle this? I found a good example through google, but there they used List<E> and that one wont take List with String as argument.

Also, is it possibe to make it so you can see when it iterates through the names, first fast, then slower, and in the end stop chosing 1 of the names?

Thanks

private List<String> names;       

        public Form1()
        {
            InitializeComponent();

            names = new List<string>();
            chosenNames = new List<string>();

            names.Add("Andy");
            names.Add("Marc"); //I have more names, but deleted them to save some space
        }

       private void button1_Click(object sender, EventArgs e)
        {          
            Random rdm = new Random();
            // I erased the code as it doesn't work, and to save space
            listBox1.Items.Add(names[0]);
            listBox1.Items.Add(names[1]);
        }




Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2