473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Winforms User COntrol

Hello there,

Iam creating a user control with 2 buttons, And i have a public
static variable i'll be changing the value of the variable in the
button click events.
When i consume the user control in the winforms application how to
automatically get the value of the public variable on click of the user
control.

user control code

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;

namespace WindowsControlL ibrary2
{
public partial class UserControl1 : UserControl
{
public static string msButtonClicked ;

public UserControl1()
{
InitializeCompo nent();
}

public UserControl1(De legate p)
{
InitializeCompo nent();
}
private void Button1_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val1";
}

private void Button2_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val2";
}

private void Button3_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val3";
}

private void Button4_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val4";
}

}
}
consuming page
i want value to be popped up in userControl11_c lick event
private void userControl11_L oad(object sender, EventArgs e)
{

MessageBox.Show (WindowsControl Library2.UserCo ntrol1.msButton Clicked);
//EventHandler handler = new EventHandler(a) ;
//userControl11.C lick += handler;
}
Regards
Saravanan K

Jul 29 '06 #1
2 1847
wrong newsgroup, try the C# newsgroup:

http://msdn.microsoft.com/newsgroups...nguages.csharp

they can prob help you out
--
-iwdu15
Jul 30 '06 #2
It sounds like you are breaking encapsulation. As an alternative, I would
recommend that you have a single public property against which you program
your consuming classes. Perhaps you could just override .Text and return
a private string msButtonClicked (which should not be static). Public fields
are typically considered bad OOP practice.

Alternatively, you could just have the .Text accessor check the value of
the internal boxes and return the appropriate string, thereby eliminating
the need for msButtonClicked as a string anyway. On property Set, you can
set the private string and click the appropriate internal button. All of
the internal buttons should be scoped private (or protected), not public
to maintain the encapsulation.

For your messaging back to the consuming form, your control should raise
a custom event (perhaps "Changed") whenever an internal button is clicked.
The form would add a handler to the user control's Changed event rather than
any internal event handlers. Once you make these changes, your form will
only need to be concerned with the following: .Text (property) and .Changed
(event). Everything else will be encapsulated inside of the user control.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Hello there,

Iam creating a user control with 2 buttons, And i have a public
static variable i'll be changing the value of the variable in the
button click events.

When i consume the user control in the winforms application how to
automatically get the value of the public variable on click of the
user control.

user control code

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;
namespace WindowsControlL ibrary2
{
public partial class UserControl1 : UserControl
{
public static string msButtonClicked ;
public UserControl1()
{
InitializeCompo nent();
}
public UserControl1(De legate p)
{
InitializeCompo nent();
}
private void Button1_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val1";
}
private void Button2_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val2";
}
private void Button3_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val3";
}
private void Button4_Click(o bject sender, EventArgs e)
{
msButtonClicked = "Val4";
}
}
}
consuming page
i want value to be popped up in userControl11_c lick event
private void userControl11_L oad(object sender, EventArgs e)
{
MessageBox.Show (WindowsControl Library2.UserCo ntrol1.msButton Clicked);
//EventHandler handler = new EventHandler(a) ;
//userControl11.C lick += handler;
}
Regards
Saravanan K

Jul 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3378
by: David Adams | last post by:
Hi, I am afraid I am asking a very basic question, or just something that is not possible. I have a WinForm app that contains a Form (Form A), and then 20-30 UserControls/WinForms that are created by Form A. Form A calls a class to populate a strongly typed DataSet (contains about 20 tables) that is to be shared amongst the UC's and other Forms. Right now, I am passing this DataSet to each and every UserControl/Form via the...
10
2416
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
5
4095
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in both projects. Both controls(dlls) have been signed using SN.exe and I've set up the appropriate .Net assembly permissions using those Strong Names The DLL's have been copied to the /bin directory in both web virtual directories.
2
6899
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app (which is the UI used to adjust the actions taken by, and the schedule of the service), then a privileged user thread should be used in the UI - no service required. But... "A windows service enables the creation of long-running executable
5
3829
by: brian.wilson4 | last post by:
Our group is currently comparing winforms vs webforms.....app is Corp LAN based - we have control of desktops.....Below is pros and cons list we have come up with - if anything strikes you as untrue or you would like to add - please comment - thanks..... Rich Client PROS 1. User experience (* indicates feasible on web with AJAX) - a. Single, unified application experience b. Windows/Office-like look and feel - i.Tabs - drag and drop...
0
2491
by: jonathan.beckett | last post by:
I have been working on a client project recently that is using winforms ..NET user controls within web pages in Internet Explorer, and need to find out how to make the user control communicate back to the webpage (typically to tell it that it has finished doing something). I started digging around and found out that I needed to implement a COM interface for the .NET user control, and pick up the events from that in Javascript on the...
2
10103
by: kumarpappu | last post by:
hello - I am newbie in winforms and c#. i have a mainform.cs and it has 2 user controls - leftView and rightView. In leftView.cs i am instantiating a new clientLeftView(another user control) and also the clientRightView(another user control) Now clientLeftView has a lstClient - list box.
23
4558
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I memorize and/ or familiarize myself in WinForms will disappear in WPF? I did note in one early version of C#, the non-generic "ArrayList" was replaced by the generic and template <based List, but that kind of change is not a big deal. If WPF...
0
2130
by: amdrit | last post by:
Hello Everyone, I am having a hard time understanding how to implement MVP in winforms with custom controls. I am attempting to make use of TDD using nUnit for my tests. Do any of you have a list of links to direct me to? It seems to me that custom controls are mini views that are used in a larger composite view. Since control creation occurs on during form initialization, how does the view get hooked into place?
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.