473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

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

public UserControl1()
{
InitializeComponent();
}

public UserControl1(Delegate p)
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
msButtonClicked = "Val1";
}

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

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

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

}
}
consuming page
i want value to be popped up in userControl11_click event
private void userControl11_Load(object sender, EventArgs e)
{

MessageBox.Show(WindowsControlLibrary2.UserControl 1.msButtonClicked);
//EventHandler handler = new EventHandler(a);
//userControl11.Click += handler;
}
Regards
Saravanan K

Jul 29 '06 #1
2 1827
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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace WindowsControlLibrary2
{
public partial class UserControl1 : UserControl
{
public static string msButtonClicked;
public UserControl1()
{
InitializeComponent();
}
public UserControl1(Delegate p)
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
msButtonClicked = "Val1";
}
private void Button2_Click(object sender, EventArgs e)
{
msButtonClicked = "Val2";
}
private void Button3_Click(object sender, EventArgs e)
{
msButtonClicked = "Val3";
}
private void Button4_Click(object sender, EventArgs e)
{
msButtonClicked = "Val4";
}
}
}
consuming page
i want value to be popped up in userControl11_click event
private void userControl11_Load(object sender, EventArgs e)
{
MessageBox.Show(WindowsControlLibrary2.UserControl 1.msButtonClicked);
//EventHandler handler = new EventHandler(a);
//userControl11.Click += 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
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...
10
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) ...
5
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...
2
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...
5
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...
0
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...
2
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)...
23
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.